problem with 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
jiehuang001
Forum Commoner
Posts: 39
Joined: Mon May 12, 2003 12:53 pm

problem with session

Post 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>
jiehuang001
Forum Commoner
Posts: 39
Joined: Mon May 12, 2003 12:53 pm

The file name is "Main.php"

Post by jiehuang001 »

I said "Admin.php". Actually its name is "Main.php"
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

well first off session_start has to be at the VERY top of your page before anything else
jiehuang001
Forum Commoner
Posts: 39
Joined: Mon May 12, 2003 12:53 pm

Post 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?
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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.
jiehuang001
Forum Commoner
Posts: 39
Joined: Mon May 12, 2003 12:53 pm

Post 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");
?>
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

<?php
session_start();
if(!isset($_SESSION['sessionvar']))
{
header ("http://www.cnn.com");
}
?>[/quote]
Post Reply