Page 1 of 1

problem with session

Posted: Fri May 23, 2003 3:30 pm
by jiehuang001
I have a page Admin.php, at the top is the following code. it is supposed to direct the user to the Login.html page if he is not authorized first. However, when I type http://localhost/Admin.php, I can still view this page, not being directed to the login page instead. Please advise.
Of couse, in the authorization page, I have some code like this:
if (ocifetch($stmt)){
session_start();
session_register('username');
header("Location: Main.php");
}
else {
header("Location: index.html");
}


------------------Admin.php----------------
<?php
session_start();
if(!(session_is_registered('username'))){
header ("Location Login.html");
} else {
?>

<html>blah....</html>

The file name is "Main.php"

Posted: Fri May 23, 2003 3:32 pm
by jiehuang001
I said "Admin.php". Actually its name is "Main.php"

Posted: Fri May 23, 2003 3:38 pm
by JPlush76
well first off session_start has to be at the VERY top of your page before anything else

Posted: Fri May 23, 2003 3:44 pm
by jiehuang001
As you can see, in the "Main.php" page, i did put "session_stat()" at the VERY top. I do be able to retrive the value of "$_SESSION['usernmae']" in the following pages.

In the authorization page, I think I even don't need the code "session_stat()" since I already said "session_register()", right?

Posted: Fri May 23, 2003 3:45 pm
by JPlush76
you HAVE to have session_start in EVERY page you want to use session vars and it has to be at the top of every page.

Posted: Fri May 23, 2003 3:53 pm
by jiehuang001
Well, let's say, now I only have one page "Main.php". If the user type http://myurl/Main.php directly, I will send him to http://www.cnn.com. If the user login first, he will see this page. Can you tell me how to do that?
The following doesn't work. Also, I put "session_start()" at the VERY top of the authorization page, it still doesn't work.

<?php
session_start();
if(!(session_is_registered('username')))
header ("http://www.cnn.com");
?>

Posted: Fri May 23, 2003 3:56 pm
by JPlush76
<?php
session_start();
if(!isset($_SESSION['sessionvar']))
{
header ("http://www.cnn.com");
}
?>[/quote]