Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
Hi guys, I have a very simple password set up on a webpage of mine, written in PHP. I have a "checkpass.php" page that checks to see if a $_POST value is the correct password. If so, it set the $_SESSION['logged'] variable to "YES", and then moves on to the next page. Every page from then on checks to see if $_SESSION['logged'] is YES, and so on. Here's the code for the main pages:

index.php:

Code:
<?php
session_start();
echo "Password: <form action='checkpass.php' method='post'><input type='text' name='pass' id='pass'><input type='submit' value='Login'></form>"
?>

checkpass.php:

Code:
<?php
session_start();
session_register('logged');
$pass = $_POST['pass'];
if ($pass == "123pass") {
    $_SESSION['logged'] = "YES";
    header("Location: http://www.craigotis.com/iou/current.php");
    }
else {
$logged = "NO";
header('Location: http://www.apple.com');
}
?>

and finally, current.php:

Code:
<?php
session_start();
if ($_SESSION["logged"] == "YES") {
echo "Login successful";
}
else
    header("Location: http://www.apple.com");
?>

So, index passes the user-inputted password to checkpass, which then sets the $_SESSION['logged'] variable to "YES", and redirects to current.php, which is the "main" page. For some reason though, even though logged was set to "YES," the current.php page redirects to apple.com, indicating a problem with the password. I've been reading about session variables for the past couple days, and I have no idea what I'm doing wrong!

Any help would really be appreciated! :D
 
Don't use session_register(). I don't know where you got that from, but it's deprecated. Always refer to the offical PHP manual.

http://us3.php.net/session_register

Also, for debugging purposes. Don't use a redirect. Just print out an error message. Right now you're redirecting to Apple.com...but you redirect there if the password is wrong, and you redirect there if the session variable is wrong. So when you get sent to Apple.com, how do you know which of those two events actually occurred?
 
Thanks for the replies, but it still won't work. Also, if it helps, this *was* working a few days ago, and I believe it just stopped on its own. Does that make any sense? :confused:
 
I would try some classic debugging techniques. In the checkpass.php add an else if statement that outputs $_POST['logged'] just so you can see if that value is being passed correctly, and don't worry about having the page redirect to apple.com right now. Do this and figure out exactly where things go awry.

Also, in your else statement for the checkpass.php file the "$logged = "NO";" isn't necessary because (1) you probably mean $_SESSION['logged'] = 'NO' and (2) you do a redirect right afterwards so it doesn't matter. Though this is of itself probably won't fix your problem, just something I noticed.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.