PDA

View Full Version : Problem with sessions


DanielTech
07-20-2004, 07:06 PM
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>

anarchy3200
07-20-2004, 08:39 PM
Just to let you know-

Just tested and your script works fine for me so it is not a problem with the coding itself.

Nightfire
07-20-2004, 09:06 PM
No idea how that's workin to be honest. You should have a few errors mentioning that you can't modify headers as they're already sent. Your session_start and header should be put above anything is outputted to the browser, no html, no whitepsace, no nothing. You cannot set a session variable after the headers have been sent either.

<?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>


<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix

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

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

// Display the sssion information:

?>
<html>
<head>
<title>Login</title>
</head>
<body>

<strong>Step 2 - Register Session </strong><br />


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>


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

<strong>Step 3 - Test Session Part II </strong><br />
Print session: <strong><? echo $_SESSION['name']; ?></strong><br/><br/>

</body>
</html>

trib4lmaniac
07-20-2004, 09:21 PM
I think there is something in the php.ini file allowing you to send headers after output. This chap probably doesn't have that on though as it has very severe effects on performence!

anarchy3200
07-20-2004, 09:32 PM
with the session variables i've never had any trouble with putting them in the page itself rather than in the header. As long as i start the session before i try to assign values it works for me where ever i put it.

This is not a dig or anything just stating i've never had any trouble.