Saturday, September 30, 2006

I get an "Warning: Unknown:" error message at the bottom of the page

Question:
I get an "Warning: Unknown:" error message at the bottom of the page
when I log in to the page.

"Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless
register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0"

Solution:

Below is the solution to this problem:

The session_register() function references global variables itself.
Instead set the session variable directly to use the SUPER GLOBAL
"$_SESSION" variable.

Example:

session_register('loginName');

should be re written as:

$_SESSION['loginName'] = $loginName;

Note: You can still use the session_is_registered() and
session_unregister() functions when you set the variable directly in the $_SESSION
super global.

Problem executing PHP code.

Question: i have a problem in PHP executing the following code:

when i make a small program lets say, test.html having the below code:

[Remember to start & end the HTML tags at the position marked as "#" ]

#form method="post" action="test1.php"#

insert any text : #input type=text name="text1"#
#form#

Now, create a simple php page "test1.php" having the below code:

echo "$text1";

when u run it the text that u entered in TEXT box appear in the
test1.php.
in my case it's dosn't work ... i don't know what's wrong ...

Solution:

There are 2 solutions to the problem:

First Approach:

Please check your php.ini config file for the "register_globals"
configuration set to On, if its "Off" try to Set it "On" then
restart your web server, then try running your script.

Note: Setting "register_globals" to "On" is a potential security issue.


Second Approach:

In your test1.php file write the code:

echo $_POST['text1' ];

and it sould work fine.

Friday, September 22, 2006

How can I send variables from a PHP script to another URL using POST method without using forms and hidden variables?

Question :
How can I send variables from a PHP script to another URL using POST method without using forms and hidden variables?

Answer :
You can do this by opening an HTTP socket connection and send HTTP POST commands.
Example:

// Generate a request header
$RequestHeader =
"POST $URI HTTP/1.1\n".
"Host: $Host\n".
"Content-Type: application/x-www-form-urlencoded\n".
"Content-Length: $ContentLength\n\n".
"$RequestBody\n";

// Open the connection to the host
$socket = fsockopen($Host, 80, &$errno, &$errstr);
if (!$socket)

$Result["errno"] = $errno;
$Result["errstr"] = $errstr;
return $Result;
}
$idx = 0;
fputs($socket, $RequestHeader);
while (!feof($socket))

$Result[$idx++] = fgets($socket, 128);
}
//-------------------------------------------
?>


Or you can use the php cURL functions. this is mostly used for sending the Credit Card details over the secure channel.

$URL="www.testsite.com/test.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://$URL");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Data1=blah&Data2=blah");curl_exec ($ch);
curl_close ($ch);
?>

This will have an effect of posting your data to the $URL site, without any header hacking.

To use cURL you need to recompile PHP with cURL support.

How do I check whether a string contains HTML tags?

Question :
How do I check whether a string contains HTML tags?

Answer :
There are two ways you can do it, one is using a regular expression and the other is comparing strings.

First the regular expression approach:

if (preg_match("/([\<])([^\>]{1,})*([\>])/i", $string)) {
echo "string contains html";
}

Second a comparing string approach:

if(strlen($string) != strlen(strip_tags($string)){
echo "string contains HTML";
}

How do I set the browsers timeout?

Question :
How do I set the browsers timeout?

Solution :
If your script is too complex to finish within the standard 30 seconds time limit you can set a new value using the function:

set_time_limit(600);

this sets the timeout too 600 seconds / 10 minutes.

How to display a number with leading zero's?

Question :
How to display a number with leading zero's?

Answer :

$number = 15;
printf("%05d", $number);
?>

How do I generate a random number using php script?

Question :
How do I generate a random number using php script?

Answer :
To generate a random number say, between 0 and 100 use the following code:

srand((double)microtime()*1000000);
echo rand(0,100);
?>

How do I get a users IP address?

Question :
How do I get a users IP address?

Answer :
The users ip address is stored in the environment variable $REMOTE_ADDR, if you want to resolve the domain name for that ip use:

Thursday, September 21, 2006

What are the differences between Get and post methods in form submitting?

The HTML specifications technically defines the difference between "GET" and "POST" methods, so that former means that form data is to be encoded (by a browser) into a URL while the latter means that the form data is to appear within a message body. The specifications also give the usage recommendation that the "GET" method should be used when the form processing is "idempotent" (i.e. it has no lasting observable effect on the state of the world), then the form method should be GET. Many database searches have no visible side-effects and make ideal applications of query forms, and in those cases only. As a simplification, we might say that "GET" is basically for just getting (retrieving) data whereas "POST" may involve anything, like storing or updating data, or ordering a product, or sending E-mail.

If the service associated with the processing of a form has side effects (i.e. modification of a database or subscription to a service), the method should be POST.

Maximum URL length in GET is 2083 characters in IE.