4

On the same page I have

$hello = 'Hello!';
$_POST['hello'] = '123';

If I echo $hello, instead of getting 'Hello!' I get '123'. How should I handle variables and $_POST variables with the same name?

This is an example of the real problem:

I have a signup form that looks like this (here's a minified sample of fields). Each input field has a label and the string variable in the label has the same name as the input.

<form id="form1" action="post.php">
  <span class="label"><?=$fullname?></span>
  //$fullname='Please enter your name';
  <input name="fullname" id="fullname" type="text">

  <span class="label"><?=$email?></span>
  //$email='Please enter your email';
  <input name="email" id="email" type="text">

  <input name="button1" id="button1" type="submit">
</form>

When I submit the form I post it to the same page and I display the values the user had filled out. Only that now, instead of $fullname displaying the value of the variable $fullname, it displays the value of $_POST['fullname']. Why does this happen?

5 Answers 5

5

probably you have register_globals turned on which is something that has been advised against for years already :) see here for details: http://php.net/manual/en/security.globals.php

Sign up to request clarification or add additional context in comments.

Comments

3

The problem probably lies with register_globals in php's .ini file. Turn this off, restart php and it should be fixed.

Try this to check the setting at the moment of execution of the code:

echo ini_get("register_globals");

10 Comments

It's actually set to off. That's the first thing I checked.
try the ini_get method i added to see the value at the moment of executing your code.
yeah, it should be register_globals. I know of nothing else that produces this behavior,
Like I said, register_globals is off. img203.imageshack.us/img203/1341/clipa.jpg
It's possible to set it while running the code (ini_set("register_globals", "true")). That is why I advised to check it at runtime. Maybe some library of some sort is doing it without you noticing.
|
1

You must to set method="POST" attribute in form declaration. And may be you have register_globals option is enabled.

Comments

1

Check your php.ini for the register_globals setting. It is most likely set to on, you should turn it off.

1 Comment

It's actually set to off. That's the first thing I checked.
1

Well if register_superglobals is off then you are doing similar in your script

like

foreach($_REQUEST as $key => $val) // or $_POST or $_GET
    $$key = $val;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.