Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sessions ?

Status
Not open for further replies.

ejwf

Technical User
Joined
Mar 31, 2004
Messages
2
Location
GB
I have a login form on the first page, from which I want to take the username and password and save them as session variables to take them forward from page to page.

the part of my code that does this is ;
Code:
if ($username && $password)	      // everything is OK
{							// query the database
$query = "SELECT UserId FROM tblUser                      
          WHERE CLogIn='$username'
          AND CPass=PASSWORD('$password')";
         $result = @mysql_query($query);
         $row = mysql_fetch_array ($result, MYSQL_NUM);
         
if ($row)  // username and password match database records   
 {
 // start the session, register the values and redirect
session_start();
         $_SESSION['password'] = $row[1];
         $_SESSION['username'] = $row[2];	 
         $_SESSION['UserId'] = $row[0];

I have been going over this for days now and I know the session variables are not working - on the next page I have
session_start ();
right at the top, but when I try to test the session variable with
print_r($_SESSION);
I get the output
array()
i.e. nothing in the session variables.

the other strange thing is that I am using the variable $username in several places in the code on the second page (as part of a welcome message and within queries to set other variables) this is working fine.
Where is it getting the information for this $username variable from if not from the session?

I am really confused by it all - if any can help I would be very grateful - thanks
 
A question: does every script on your site that will manipulate session variables invoke session_start()?


A comment: Invoke session_start() as early in your script as possible. session_start() manipulates HTTP headers, and if your script produces output, session_start() will likely fail.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Your SQL statement is suspect:
Code:
SELECT UserId FROM tblUser WHERE ...
You are only retrieving the UserId but later in the script set $row[1] etc. into session variables. According to the query there should be only one element in the result row, namely the UserId which you asked for. All other assignments will be to no avail.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top