marcparillo
asked on
Vanishing PHP variables
Here's a pretty simple problem:
I am writing a registration page in PHP and I can't seem to figure out why my database doesn't record the IP address of the registered user and the timestamp indicating when they registered.
I declare the variables at the top of the PHP page:
$time = time();
$time+=(3*60*60);
$IPaddress = $_SERVER['REMOTE_ADDR'];
// some more code
.. and then, down the page, I refer to the variables in the function register() that writes the information to the database:
function register() {
$create = "INSERT into registration (identifier,ip,timestamp,f irst,last, email,stat ,region,ph one,job,pa ssword,adv isory,popu p,sound) values ('$identifier','$IPaddress ','$time', '$_POST[fi rst]','$_P OST[last]' ,'$_POST[e mail]', '$_POST[station]','$region ','$_POST[ phone]','$ _POST[job] ','$_POST[ password1] ','1010101 010','ON', 'OFF')";
mysql_query($create);
mysql_close();
}
All of my other variables get recorded to the database correctly -- but for some reason the $IPaddress and $time variables don't. Any reason why?
I am writing a registration page in PHP and I can't seem to figure out why my database doesn't record the IP address of the registered user and the timestamp indicating when they registered.
I declare the variables at the top of the PHP page:
$time = time();
$time+=(3*60*60);
$IPaddress = $_SERVER['REMOTE_ADDR'];
// some more code
.. and then, down the page, I refer to the variables in the function register() that writes the information to the database:
function register() {
$create = "INSERT into registration (identifier,ip,timestamp,f
mysql_query($create);
mysql_close();
}
All of my other variables get recorded to the database correctly -- but for some reason the $IPaddress and $time variables don't. Any reason why?
Either pass the variables to the function as parameters, or declare them as global in the function. Right now, the function code can't see them.
ASKER
Thanks, I knew it was something simple like that.
If I don't want to pass variables like this:
function register($time,$IPaddress) {
//code
}
.. how do I declare them as global? like this?
global $time, $IPaddress;
Thanks
If I don't want to pass variables like this:
function register($time,$IPaddress)
//code
}
.. how do I declare them as global? like this?
global $time, $IPaddress;
Thanks
exactly :)
function register() {
global $time, $IPaddress;
$create = "INSERT into registration (identifier,ip,timestamp,f irst,last, email,stat ,region,ph one,job,pa ssword,adv isory,popu p,sound) values ('$identifier','$IPaddress ','$time', '$_POST[fi rst]','$_P OST[last]' ,'$_POST[e mail]', '$_POST[station]','$region ','$_POST[ phone]','$ _POST[job] ','$_POST[ password1] ','1010101 010','ON', 'OFF')";
mysql_query($create);
mysql_close();
}
global $time, $IPaddress;
$create = "INSERT into registration (identifier,ip,timestamp,f
mysql_query($create);
mysql_close();
}
ASKER
Don't I have to declare the global variables where they're created ... at the top of the page? In your example above, you're creating the global variables inside the function.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks vodercm --
My background is in Perl, so declaring variables as "global" inside functions in PHP is backward from what I learned in Perl, where "global" variables are declared with the "my" reference anywhere outside of functions.
My background is in Perl, so declaring variables as "global" inside functions in PHP is backward from what I learned in Perl, where "global" variables are declared with the "my" reference anywhere outside of functions.
You're welcome, good luck :)