Results 1 to 7 of 7

Thread: Creating Session IDs on php pswd protected pages

  1. #1
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Creating Session IDs on php pswd protected pages

    Okay, I've been looking around the internet trying to figure out how to create a "session ID" on a php password protected page. I looked this over too http://us3.php.net/session and I am lost, of course.

    My sample page http://www.propheciesofrevelation.org/test.php

  2. #2
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    PHP Code:
    <?php
    if (!isset($_SERVER['PHP_AUTH_USER'])) {
        
    header('WWW-Authenticate: Basic realm="Realm"');
        
    header('HTTP/1.0 401 Unauthorized');
            echo 
    'Unauthorized';
        exit;
    } else {
        
    session_name("mysession");
        
    session_start();
        
    $_SESSION["username"] = $_SERVER["PHP_AUTH_USER"];
        
    $_SESSION["password"] = md5($_SERVER["PHP_AUTH_PW"]);
    }
    ?>
    Is this what you're looking for?

  3. The Following User Says Thank You to Leafy For This Useful Post:

    Benedizione (03-08-2008)

  4. #3
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Leafy View Post
    PHP Code:
    <?php
    if (!isset($_SERVER['PHP_AUTH_USER'])) {
        
    header('WWW-Authenticate: Basic realm="Realm"');
        
    header('HTTP/1.0 401 Unauthorized');
            echo 
    'Unauthorized';
        exit;
    } else {
        
    session_name("mysession");
        
    session_start();
        
    $_SESSION["username"] = $_SERVER["PHP_AUTH_USER"];
        
    $_SESSION["password"] = md5($_SERVER["PHP_AUTH_PW"]);
    }
    ?>
    Is this what you're looking for?
    I am not sure. How does that work with a password protected page?

    I am not sure if you mean this "Session ID" code password protects the page, or what I am wanting is protection from being able to click on the browsers "back" button and being taken back into a password protected page.

    The message I posted in the "other" section here someone said that using "session ids" was better than just using a javascript code to achieve this because javascript can be turned off and worked around whereas using "session ids" cannot be worked around.

    You think this code is what I need?

  5. #4
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    I inserted that code in the body of my page but received an error message - something about the password, authentication error.

    Where is that code supposed to be placed?

  6. #5
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Make sure you're placing this at the VERY top of the page before anything is echoed, specifically.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  7. The Following User Says Thank You to alexjewell For This Useful Post:

    Benedizione (03-08-2008)

  8. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    That code needs to be placed before any output to the browser. Example:

    Code:
    <?php
    if (!isset($_SERVER['PHP_AUTH_USER'])) {
        header('WWW-Authenticate: Basic realm="Realm"');
        header('HTTP/1.0 401 Unauthorized');
            echo 'Unauthorized';
        exit;
    } else {
        session_name("mysession");
        session_start();
        $_SESSION["username"] = $_SERVER["PHP_AUTH_USER"];
        $_SESSION["password"] = md5($_SERVER["PHP_AUTH_PW"]);
    }
    ?> 
    <html>
    <head>
    <title>Test</title>
    </head>
    <body>
    
    This is outputted to the browser after the above script is ran!
    
    </body>
    </html>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. The Following User Says Thank You to thetestingsite For This Useful Post:

    Benedizione (03-08-2008)

  10. #7
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    I placed the code at the very top. It works great that way and is definitely a code I want to keep on hand. I might be able to use it for something else.

    However, that code is asking for the password to my .org account. It is overriding my prior password and question for the page.

    Is there a way to use "sessions" so that a person can log into my page the way it is and prevent the page from being "cached" or rolled back into from the browser back button?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •