Forum Moderators: coopster

Message Too Old, No Replies

using $_SESSION variable for different .php files

using this for page authorization

         

mhenderson

7:48 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



Hi all, still working on getting my page authorization to work for logged-in users:

login.php
// if successful user login
$_SESSION['authenticate'] = 1;
if (isset($_SESSION['authenticate'])) {
echo "<br>session variable is set and has the value:".$_SESSION['authenticate'];
}
echo "<script language=\"JavaScript\">document.location.href='"."http://www.exammple/quiz.php"."';</script>";

This works correctly and prints the session variable has been set to one, then goes to quiz.php

quiz.php
<?php
SESSION_START(); // tried with this line of code, and also ommiting it
if (isset($_SESSION['authenticate'])) {
echo "<br>session variable is set and has the value:".$_SESSION['authenticate'];
}
if (isset($_SESSION['authenticate'])) {
// display some html
}
else
{
echo "<script language=\"JavaScript\">document.location.href='"."http://www.exammple/quiz.php"."';</script>";
}
?>

This does not print that the session variable has value 1. Am I calling/linking this properly, why does session variable loose its value, and help would be great.
Thx in advance

mcibor

8:30 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the problem of session lost after of redirect with header location...

Try with this:
<?
session_start();
$_SESSION['mySession'] = "hello";

header ("Location: xpage.php");
exit(); //This sentence do the magic
?>

more at [pl2.php.net...]

Hope this helps

Michal Cibor

mhenderson

9:44 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



I checked out that link and this is what I found:

SESSION LOST ON HEADER REDIRECT (CGI on IIS 5.0)

I realize there are numerous scattered posts on this issue, but I would like to add my 2&#65533; since it took me a whole day and a download of the LiveHTTPHeaders Mozilla plugin to figure it out.

On the **CGI** version of PHP on IIS 5.0/Windows 2000, the following code will not work as expected:

/***** sess1.php *****/
session_start();
$_SESSION["key1"] = "testvalue";
header("Location: sess2.php");

/***** sess2.php *****/
session_start();
echo "key1 = '".$_SESSION["key1"]."'";

PROBLEM:
All session data is lost after a header redirect from the first page on which the session is initialized. The problem is, the PHPSESSID cookie is not being sent to the browser (ANY browser, IE or Mozilla) on the initial session page with the header("Location: ...") redirect. This is unrelated to client cookie settings - the set-cookie: header just isn't sent.

SOLUTION:
I was able to remedy the problem by switching to the ISAPI DLL version. This seems to be an MS/IIS bug, NOT a PHP bug - go figure. I hope this saves you some headaches especially with your user authentication scripts!

I am using Firefox web browser, or IE so I guess I cant' do this.