Page 1 of 1

Help With My First Session

Posted: Sun Aug 12, 2007 11:00 am
by dgny06
Thanks in advance for any help.....

I just made my first attempt at starting a session. I created a page that is supposed to lookup the username and password entered on a form and then take the first name of that user and both echo it and keep it as a session variable. I then created another page that should echo the first name session variable. The problem is that when I go the second page nothing is echoed. I know that I am probably creating my session incorrectly but after looking all over the place I can;t figure it out.

Code: Select all

<?PHP session_start(); ?>

<?PHP include("connection/connect.php"); ?>

<?PHP

$UserName = $_POST["UserName"];
$Password = $_POST["Password"];

$FindFirstNameQuery = "select * from tbUsers where UserName = '$UserName' and Password = '$Password'";
$GetFirstName = mysql_query($FindFirstNameQuery);
$FirstName = mysql_fetch_array($GetFirstName);

$_SESSION['FirstName'] = $FirstName['FirstName'];

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome to the Test Page</title>
</head>

<body>


<?PHP

echo $_SESSION['FirstName'];

?>

<br /><br />

Click <a href="test.php">here</a> to see if the session variable carries over...
Code from test.php is below

Code: Select all

<?PHP include("connection/connect.php"); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?PHP

echo $_SESSION['FirstName'];

?>


</body>
</html>

Posted: Sun Aug 12, 2007 11:05 am
by dirkr
as far as i know you also gotta put

session_start();

on the test.php page
guess it sorta reads in everything from an existing session when you do that ( if none exists it creates a new one )
this might not be the correct explentation but thats how i see it :)

basicly
on any page you wanna use sessions or sesssion variable you got to put session_start();

Posted: Sun Aug 12, 2007 11:13 am
by dgny06
thank you that worked.....I had actually tried that earlier....but I guess that because I never destroyed the original session, the change did not work as needed.

Posted: Mon Aug 13, 2007 6:33 pm
by califdon
Yup. Any page that needs access to session variables, whether it's to write them or read them, must begin with a session_start(); -- and be sure it comes before sending anything back to the browser.