Problem with PHP Session Variables

Discussion in 'Web Design and Development' started by Littleodie914, Oct 3, 2007.

  1. Littleodie914 macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #1
    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
     
  2. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #2
  3. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #3
    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?
     
  4. Littleodie914 thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #4
    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:
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    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.
     

Share This Page