I am just getting into sessions and came across this one.
(first off, I do have
<?php session_start();?>
at the top of every page)
I have a header file like this (included from index.php):
if(isset($_SESSION['user'])){
echo "You are logged in as ".$_SESSION['user'];
echo " | ";
echo "<a href=\"tc_logout.php\">Logout</a>";
echo " | ";
echo "<a href=\"tc_tasks.php\">Tasks</a>";
echo " | ";
$tc_user = $_SESSION['user'];
echo "<a href=\"users.php?user=$tc_user\">My Page</a>";
}else{
echo "<a href=\"tc_login.php\">Login</a>";
}
I have the user login and set a session
$_SESSION['user'] = 'test1';
then go back to the main page. This tehn shows me who is logged it, etc and that works fine.
So, when the user goes to "My Page", the URL passes their user name into the next page and I have a _GET to grab it.
mysite\users.php?user=test1
include 'header.php';
include 'db_con.php';
$user = urldecode($_GET['user']);
I then use that $user variable to do a bunch of queries to retrieve data to display regarding that user. It all works GREAT! No worries, all is good
Until...
I change the URL and hit the page again with another valid user
mysite\users.php?user=test2
I have figure out (by using print_r ($SESSION); after each step in the code) that once the get happens and grabs that passed variable, it resets the session "user" to the _get value.
Why is this happening? I havent reset it anywhere.
And just to show you what I have done to track this down:
print_r ($_SESSION);
include 'header.php';
include 'db_con.php';
$user = urldecode($_GET['user']);
echo "<br>";
print_r ($_SESSION);
echo "<br>";
if(isset($_SESSION['user'])){
echo $user;
echo "<br>";
echo $_SESSION['user'];
}
gives me this when I hit the page the first time
Array ( [user] => test1 )
Array ( [user] => test1 )
test1
test1
and this after I change the URL to the second user:
Array ( [user] => test1 )
Array ( [user] => test2)
test2
test2
and then this when I just refresh the page:
Array ( [user] => test2 )
Array ( [user] => test2 )
test2
test2
So, please point me in the right direction. this is driving me nuts! :-)