Jump to content

Global Variables not working as expected


Recommended Posts

Greetings all.  Im currently trying to learn PHP and have stumbled across this problem on numerous occasions already. 

I understand that when i submit a form in example1.php and with the ACTION arguement call another file such as example2.php.  That i cannot pass the values without [color=red]register_globals[/color] being set to ON in the php.ini file.  Unless i use the built in $HTTP_POST_VARS array.

But if im not calling another file and simply wish to submit a form back to its own document using global $PHP_SELF(or just be default it should do this anyways).  Having [color=red]register_globals[/color] set to OFF shouldnt matter.

The script im trying to get working is this:

[code]<html>
<head>
</head>
<body>
<?php

$PARAMS=$HTTP_POST_VARS;    ///  I ADDED THIS JUST TO SEE IF THE VARIABLES DID IN FACT EXIST
foreach ($PARAMS as $key=>$value){
  print "$key=$value <br>";
}


if (isset ($domain) && isset($sex) && isset ($mail)){
  // check user input here!
  $dberror="";
  $ret=add_to_database($domain, $sex, $mail, $dberror);
  if (!$ret)
  print "Error: $dberror<BR>";
else
print "Thank you very much";
} else {
  write_form();
}

function add_to_database($domain, $sex, $mail, &$dberror){
  $user = "raversnet";
  $pass = "5434697";
  $db = "test";
  $link=mysql_pconnect("myth3",$user, $pass);
 
if (!$link){
    $dberror="Couldnt connect to MySQL server";
return false;
}
if (!mysql_select_db($db, $link)){
  $dberror=mysql_error();
  return false;
}
$query="INSERT INTO domains (domain, sex, mail) values ('$domain','$sex','$mail')";

if (!mysql_query($query, $link)){
  $dberror=mysql_error();
  return false;
}
return true;
}

function write_form(){
  global $PHP_SELF;
  print "<form method=\"POST\">";
  print "<input type=\"text\" name=\"domain\">";
  print "The domain you would like<p>";
  print "<input type=\"text\" name=\"mail\">";
  print "Your mail address<p>";
  print "<select name=\"sex\">";
  print "\t<option value=\"M\"> Male";
  print "\t<option value=\"F\"> Female";
  print "</select>";
  print "<input type=\"submit\" value=\"submit!\"></form>";
}


?>
</body>
</html>[/code]
 

The script displays the variables whether the [color=red]register_globals[/color] are on or off.  Doesnt matter.  However the script Function ISSET doesnt work unless its set to ON. 

I can get the script working with [color=red]register_globals[/color] OFF if i cycle through the array but really as far as i know....i shouldnt have to.

Any help is appreciated,

Thanks,

Rob
Link to comment
Share on other sites

You should turn register_globals off. When this setting is off you'll have to use the $_POST superglobal array to access any POST'd data, for example to get the information from the domain field ion your form you use $_POST['domain'] rather than $domain. You change the string between [' and '] to the form field you want to access. If your form method is GET you use $_GET instead of $_POST. The same with cookies you use $_COOKIE or for sessions you use $_SESSION

YOu do not need register_globals to be on if you are submitting the form to another page. Also for the form to submit to itself
you use [b]action=\"" . $_SERVER['PHP_SELF'] . "\" [/b]

$HTTP_*_VARS are old and have been replaced by the newer superglobals which were explained above.
Link to comment
Share on other sites

Ok i removed the Post Vars loop and replaced the ISSET line with:

[code]if (isset ($_POST['domain']) && isset($_POST['sex']) && isset ($_POST['mail'])){[/code]

It works now which is fantastic.  Thing is i never had to replace all other references to $domain, etc.  They just work.  Why would ISSET be the only function affected.  If that makes any sense.

Thanks for the help btw.

R.
Link to comment
Share on other sites

Ok after i turned off the Register_Globals it quit working.  Shoulda did that in the first place.  After updating the line:

[code]$ret=add_to_database($_POST['domain'], $_POST['sex'], $_POST['mail'], $dberror);[/code]

It worked just fine with Globals off. 

Thanks again,

R.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.