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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP sessions - can't transfer value from one page to antoher.

Status
Not open for further replies.

zlaws

Programmer
Oct 28, 2007
1
MT
Hi, I'm new to this forum, and it was suggested to me since there are many helpful people around :) I think i'm not the first one posting this kind of thread, but even though i surfed the net, did exactly how other suggested to identify the problem, it looks like i can't make this sessions work.

on the first page where i declare the session i tested it with the print command and the value appears to be correct. as soon as i go on the second page the value of the session is null

I tried it on 2 different servers and i got the same problem. can please someone help me coz i'm stack on my project here :(

BELOW ARE THE CODED I'M USING:
____________________________________________________________
LOGIN VERIFICATION PAGE

<?
session_start();


(HERE I HAVE THE CONENCTION TO DB CODE AND VERIFICATION OF THE UN and PW - which are working fine)

if($count==1)
{
$_SESSION[auth]= 'TRUE';
$test=$_SESSION[auth];
print "$test";
header("location:test.php");
}
else {
echo "Wrong Username or Password";
header("location:login.html");
}

?>
____________________________________________________________
THIS IS THE CODE FOR THE PAGE I WANT ONLY LOGGED IN USERS TO BE ABLE TO VIEW (BELOW)

<?php
session_start();


if(isset($_SESSION['auth']) == 'TRUE')
{
echo ("YES");
}

else
{
echo ("NO");
}

?>
____________________________________________________________

I would really appriciate any help. THANKS
 
two things to check.

1. make sure that php has read/write access to the folder in which the session data is being stored, if, that is, you are using file-based sessions. you can get the information on the directory from a call to phpinfo().

2. you may be suffering a race condition if you are using a localhost or LAN based environment to test the code. i.e. the redirected page may be trying to open the session store even before the sending script has written the new data to the session store. fix this by adding
Code:
session_write_close();
before the header() functions.

otherwise do not use booleans in string form. use them as booleans.

Code:
$_SESSION['auth'] = true; //not 'TRUE'

note also that this
Code:
print "$test";
header("location:test.php");

won't work. you cannot redirect using the header method once actual text (or even blank space) has been sent to the browser.

lastly, this

Code:
if(isset($_SESSION['auth']) == 'TRUE')

is a nonsense.
there are two separate tests here. is the session variable set and is it a certain value. these must be tested separately

Code:
if ( isset($_SESSION['auth']) && $_SESSION['auth'] === true){
 //do something
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top