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

Problems with sessions

Status
Not open for further replies.

DanielTech

Programmer
Mar 2, 2004
8
US
I am trying to make login page work using sessions. The problem is that once I have initialized the session and I try to retrieve it from another page (page3.php in my code) I don't get anything. When I try to print out the session is blank, Does anyone have any idea why? here is my code:

////////////////////////////////////////////////
//// login.html
///////////////////////////////////////////////
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
session_start();
header("Cache-control: private"); // IE 6 Fix.
?>
<FORM METHOD="POST" ACTION="page2.php">
Enter your Name: <input type="text" name="name">
<input type="SUBMIT" value="Submit">
</FORM>
</body>
</html>

///////////////////////////////////////////////
//// page2.php
//////////////////////////////////////////

<html>
<head>
<title>Login</title>
</head>
<body>
<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix

echo "<strong>Step 2 - Register Session </strong><br />";

// Get the user's input from the form
$name = $_POST['name'];

// Register session key with the value
$_SESSION['name'] = $name;

// Display the sssion information:
?>

Welcome to my website <strong><? echo $_SESSION['name']; ?></strong>!<br/>
Let's see what happens on the <a href="page3.php">next page.</a><br/><br/>
</body>
</html>


//////////////////////////////////////////////////////////////
/// page3.php
////////////////////////////////////////////////////////////
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
?>
<strong>Step 3 - Test Session Part II </strong><br />
Print session: <strong><? echo $_SESSION['name']; ?></strong><br/><br/>

</body>
</html>
 
session_start(); needs to happen BEFORE ANY HTML OUTPUT....

Your session is not being initiliazed as the session headers are not sent...

Code:
I am trying to make login page work using sessions. The problem is that once I have initialized the session and I try to retrieve it from another page (page3.php in my code) I don't get anything. When I try to print out the session is blank, Does anyone have any idea why? here is my code:

////////////////////////////////////////////////
//// login.html
///////////////////////////////////////////////
<?php 
session_start(); 
header("Cache-control: private"); // IE 6 Fix. 
?> 
<html>
<head>
<title>Login</title>
</head>
<body>

<FORM METHOD="POST" ACTION="page2.php"> 
Enter your Name: <input type="text" name="name"> 
<input type="SUBMIT" value="Submit"> 
</FORM> 
</body>
</html>

THis needs to be the same for every page that uses the session variables


Bastien

Cat, the other other white meat
 
I believe that the session_start() statement has to be issued as soon as possible, move the
Code:
<?php 
// start the session 
session_start(); 
header("Cache-control: private"); //IE 6 Fix 
?>
to before the start of your HTML code, i.e.:
Code:
<?php 
// start the session 
session_start(); 
header("Cache-control: private"); //IE 6 Fix 
?>
<html>
....
Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top