Jump to content
xisto Community
Sign in to follow this  
shadow-x

Reading Data From Sessions

Recommended Posts

Hello everyone!

Before I start let me just say that ive read many threads on here about php and sessions, several tutorials and the php manual but I still cant find a solution to my problem.

My problem is that when i try to read data from a session it just comes up blank, and an if statement to see if the variable is null returns true. The code im using is for a login script which checks the input from a user and compares it with the data in a database to log the user in, this works fine, also reading the data from the session works perfectly on the page which the session is generated by so the problem is when the user navigates to another page the variables dont seem to travel with them. The code I'm using to generate the sessions is this:

//define initial variables$user = $_POST['username'];
$password = $_POST['password'];
session_register(name,$user);
session_register(status,"0");
//set variables
$session['name'] = $user;
$_session['status'] = "0";


to try and read the variables from another page ive used an included file with thise code:

session_start();if($_session['name'] == NULL){
echo "no session is set :lol: <BR><BR>";
echo $_session['name'];
} else {
echo "sesion is set for:";
echo $_session['name'];
};

This returns "no session is set" which means that the variable is null.
the data inside the session file is "name|N;chris|N;status|N;0|N;" I dont know if that helps to see what the problem is.

So can anyone tell me why the variables aren't being read by the pages?

Share this post


Link to post
Share on other sites

The superglobal for sessions is $_SESSION not $_session or let alone $session. Latter two are just normal arrays created when you assign values to them.

So change the lines

$session['name'] = $user;$_session['status'] = "0";

To
$_SESSION['name'] = $user;$_SESSION['status'] = "0";


And your script should work.

Also you do not need to use session_register() function. Simply assigning values to the superglobal array ($_SESSION) suffices. Session_register() works but it's usage is not recommended anymore.

Share this post


Link to post
Share on other sites

[sHAME]Oh deer, deer, me!I spent hours reading tutorials and everything else and forgot to realise it was case-sensitive.Well thank you so much atlast I can get somewhere now![/sHAME] :lol: Well I am blonde!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.