Help With My First Session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dgny06
Forum Commoner
Posts: 25
Joined: Thu Jun 14, 2007 11:35 pm

Help With My First Session

Post 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>
dirkr
Forum Newbie
Posts: 20
Joined: Sat Jul 07, 2007 2:55 pm

Post 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();
dgny06
Forum Commoner
Posts: 25
Joined: Thu Jun 14, 2007 11:35 pm

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post 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.
Post Reply