Question about session in php

hi,

can anyone give me a idea on why the session variable cannot pass to another page…

at login page i declare a session variable equal to my username. and i can echo it to the first page,but on another page i cant echo the same variable that is equal to my username.

can anyone correct me on my understanding. if i store a session variable the variable store it on the memory?

Thanks.

Are you calling session_start() at the start of every page?

yes sir…
<? session_start();?>
this is the code that i post on the very top of the page.

Show us your code…

this the login script.


<?php
session_start();


$uname=stripslashes($_POST['username']);
$password=md5($_POST['password']);

if ($uname&&$password)
{
include_once("conn.php");
$login=mssql_query("SELECT * from tblUser where UserName='$uname' and Pass='$password'");

$numrow=mssql_num_rows($login);

	if ($numrow!=0)
	{
		while($row=mssql_fetch_assoc($login))
		{		
			$username=$row['UserName'];
			$unamepass=$row['Pass'];
			$userlevel=$row['UserLevel'];

		}
			if ($uname==$username&&$password==$unamepass)
			{
				if ($userlevel=='1')
				{	
					$_session['username']==$username;
					header('Location: home.php');
				}
				elseif($userlevel=='2')
				{
					$_session['username']==$username;
					header('Location: Support.php'); 
				}
				elseif($userlevel=='3')
				{
					$_session['username']==$username;
					header('Location: User.php'); 
				}
			}
			else
				echo "Incorrect Password";
			
			
		
	}
	else
		die("Username does't exist");
}
else
	header('Location: login-failed.php');

?>


this is where my script goes if it is a valid username and password for a User


<?php
session_start();
if(isset( $_session['username']));
echo "Welcome"." ".$_session['username'];

?> 


<html>
<head>
	<title>User Acces</title>
</head>
<body>
<table border='1'>
<tr>
	<td>
		<a href='usercreateticket.php'>Create Ticket</a>
	</td>
</tr>
<tr>
	<td>
		<a href=''>Manage Ticket</a>
	</td>
</tr>

</table>
</body>
</html>

Global arrays are case sensitive.

$_session is NOT the same as $_SESSION (which you should be using).

Change to uppercase you should find things start working.

Still the same…
they still don’t echo out.

Show your updated code please.

I just change the $_session to the uppercase ($_SESSION) as you mention but still it does not echo out

No, you’ve just quoted your previous post and changed things to uppercase.

Can you please post the code you have running on the server. Also SESSION_START() is wrong. I only said to use capitals to access the global $_SESSION array. Not for the session functions. You’ve gone above and beyond my suggestion and that will only cause more problems.

Also please change this:

echo "Welcome"." ".$_SESSION['username'];

to this:

echo "Welcome $_SESSION[username]";

Thanks man it works!!!
and i just change the code here:


$_SESSION['username']==$username;
header('Location: home.php');

in to this


$_SESSION['username']=$username;
header('Location: home.php');

Glad you got it working. Also you realise you don’t need to do that 3 times - once in each case don’t you?

You can do it outside of the switch and then just use the cases for the header (saves cpu resources).

Yup your right.
i declare it outside the case.
Thanks again man.

Can i have another question?

Of course.

Either continue in this topic or start a new one.

Thanks.
New topic.i will make a new tread…