OK, so you're pulling your hair out, because you just setup a very simple form which POSTs to a PHP script, but it doesn't seem to work. This is the code you're using:
<form name="test" action="script.php"
method="post">
Your Name: <input name="name" value="" size="5"
/>
</form>
<?php
print('Your name is '.$name);
?>
Theoretically, that should work, as long as your form field is called
name
. Most likely, if that's not working, you're using a
recent version of PHP (4.2 and above) with a certain setting set to off
:
register_globals
. This pesky little setting, which
can be changed in the php.ini file (if you're on a shared hosting
server you probably won't have access to it), should never have been set
to on
in the first place. Everyone got used to assuming that
if you submit a form with a field called foo
in it, then
the variable $foo
will automatically be created. But things
should never have been done that way. Unfortunately it's too late to change
that. All I can do is educate you to stop assuming.
To
find out what version of PHP you're using and if register_globals
is on or off, simply create a phpinfo()
page. At the top of the page will be the PHP version number, and under
"Configuration - PHP Core" you will find the setting of register_globals.
By default, in PHP 4.2 and above, the register_globals
setting
is off
. However, before that version, it always used to be
set to on
. In practice, what this means is that if a form
field is called foo
, $foo
will no longer be
automatically created. This may seem a terrible shame, but it was turned
off for a reason: it can lead to terrible security problems if used lazily.
The PHP Group decided to turn it off
by default for your
security. So, you're saying, What am I supposed to do? Don't worry,
there is another way to get your form fields' values: $_GET
or $_POST
. These magical variables will contain all
the data which was sent to the PHP script via GET
or POST
. If you don't know how your data is going to be submitted,
then there is a variable which contains both of these together: $_REQUEST
.
Now, let's give you an example. Let's say you're using the same form as above to send someone's name to a PHP script. Here's what you would have:
<?php
print('Your name is '.$_POST['name']);
?>
Now that wasn't so difficult was it? Here's what you would do if you
changed the form's method to GET
:
<?php
print('Your name is '.$_GET['name']);
?>
Here's one last one, to be used if you're not sure whether the form has
been submitted using GET
or POST
(for example,
if you want to allow people to either use a form or just enter script.php?name=Daniel
in the address bar):
<?php
print('Your name is '.$_REQUEST['name']);
?>
There are some other variables like $_GET, $_POST
and $_REQUEST
in PHP: $_COOKIE
and $_SESSION
. These would
be used if the data is stored in a cookie, or if the data was stored using
a PHP session. Here's an example:
<?php
setcookie ('YourName', 'Daniel');
?>
<?php
print('Your name is '.$_COOKIE['YourName']);
?>
In this example, you would call up the first script in your browser, and it would create a cookie called YourName. This cookie would contain the text "Daniel". Then you would call up the second script, which would output "Your name is Daniel".
© 4WebHelp and Daniel
Page URL: http://www.4webhelp.net/tutorials/php/register_globals.php
Back to the "pretty" page!
© 2025, 4WebHelp Team.
Any help would be great
http://www.articlesdb.net
otherwise i am getting the this type of error pls hlep me.
-------------------------------------------------------
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
-----------------------------------------------------
I have found this tutorial helpfull but I would extend it.
I have a simple trick to display all the variables with their names posted. I have used post method while sending form but you should change it to your needs.
Look at this code:
$keys =array_keys($_POST); // store all variable names into a numericaly indexed array begining at 0
for ($i =0; $i <count($keys); $i++) // go trought the created array and do....
{
echo $keys[$i] . ": " . $_POST[$keys[$i]] . "<br>\n"; // print data in the format var_name: var_value
}
?>
Any help would be great
<?php
print('Your name is '.$_POST['name']);
?>
$orderid = "$stamp-$REMOTE_ADDR";
$orderid = str_replace(".", "", "$orderid");
This is a unique order ID I want to create, then use it on the next page. But when I try to print it out, I get nothing.
print('Your ID is '.$_REQUEST['orderid']);
Any ideas?
Your tutorial really help me!
Thanks again.
20/ago/2003.
Here's my code:
Your Name: <input name="userid" value="leo" size="5" />
</form>
<?
print('Your userid is '.$_POST['userid']);
?>
The output is "Your userid is " Shouldn't it say, "Your userid is leo"?
should be:
Tell me if it works
Thank you. Now to find my php.ini file and fix it.
Add a new comment