Jump to content

MySQL and SESSION var overlap - PHP Bug?


BenMo

Recommended Posts

I've run into a very weird error.  I found a way to work around the problem, but I don't understand why it was a problem in the first place.

 

I'm creating a login system using SESSION and MySql calls, and there is an overlap in the variables, meaning that it seems php thinks that a $_SESSION var and a $php var are the same variable.

 

session_start();

$myusername=$_SESSION['username'];
$mypassword=$_SESSION['password'];

$host="localhost"; // Host name
$username="BenMo"; // Mysql username
$password="something"; // Mysql password
$db_name="homeBase"; // Database name
$tbl_name="Users"; // Table name

// Connect to server and select databse.
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

 

What is strange is that the var $_SESSION['username'] gets set to the $username var after this code runs.  So when the page loads, $_SESSION['username'] is storing the value "BenMo", instead of what used to be in the session var. 

 

If I change $username to something else, like $username2, then there isn't a problem.  There isn't code here or anywhere else that assigns a value to the $_SESSION['username'] var. 

 

Does anyone know what's going on?

 

 

Link to comment
Share on other sites

register_globals would be the default problem if you haven't changed anything :)  It causes exactly the problem you describe.

 

When the script starts, it sets your standard variables from the values in $_SESSION, $_GET, $_POST and $_COOKIE according to a fixed set of rules (the rules can be changed also, if you're that keen).  Some people like it for the convenience, but it's safer to switch it off if you are able to.

 

And indeed they are the same variables.  If you modify $username, then you will be modifying $_SESSION['username'] (providing it existed in $_SESSION when you called session_start()).  A variable which hasn't been placed in $_SESSION will not magically appear in $_SESSION however, which makes sense.

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.