Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Vanishing PHP variables

Posted on 2007-03-28
8
241 Views
Last Modified: 2011-10-03
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,first,last,email,stat,region,phone,job,password,advisory,popup,sound) values ('$identifier','$IPaddress','$time','$_POST[first]','$_POST[last]','$_POST[email]', '$_POST[station]','$region','$_POST[phone]','$_POST[job]','$_POST[password1]','1010101010','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?
0
Comment
Question by:marcparillo
  • 5
  • 3
8 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 18812448
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.
0
 
LVL 3

Author Comment

by:marcparillo
ID: 18812482
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
0
 
LVL 27

Expert Comment

by:yodercm
ID: 18812511
exactly :)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 27

Expert Comment

by:yodercm
ID: 18812516
function register() {
  global $time, $IPaddress;
  $create = "INSERT into registration  (identifier,ip,timestamp,first,last,email,stat,region,phone,job,password,advisory,popup,sound) values ('$identifier','$IPaddress','$time','$_POST[first]','$_POST[last]','$_POST[email]', '$_POST[station]','$region','$_POST[phone]','$_POST[job]','$_POST[password1]','1010101010','ON','OFF')";
mysql_query($create);
mysql_close();
}
0
 
LVL 3

Author Comment

by:marcparillo
ID: 18812702
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.
0
 
LVL 27

Accepted Solution

by:
yodercm earned 125 total points
ID: 18812718
Nope, declare them global IN the function.
0
 
LVL 3

Author Comment

by:marcparillo
ID: 18812738
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.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 18847172
You're welcome, good luck :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question