SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why is my session variable not working?

    I have created the following page which once a successful login has happened 2 session variables are created and then on the next page I try to call these 2 session variables but it just doesn’t seem to work.

    I am stuck as to what to do now as it looks to be coded correctly (I think)



    Thank You for any help

    Code:
    <?PHP
    $username = ($_POST['username']);
    $password = ($_POST['password']);
    $encrypted_password=md5($password);
    
    //check that the user is calling the page from the login form and not accessing it directly
    //and redirect back to the login form if necessary
    if (!isset($username) || !isset($password)) {
    header( "Location: index.php" );
    }
    //check that the form fields are not empty, and redirect back to the login page if they are
    elseif (empty($username) || empty($password)) {
    header( "Location: index.php" );
    }
    else{
    //create or open database
    $db = sqlite_open("C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/swiftdist/website/database/distributorlogin.db") or die("failed to open the database");
    //get info
    $result = sqlite_query($db, "SELECT * FROM tbllogin where username = '$username' and password = '$encrypted_password'");
    //check that at least one row was returned
    $rowCheck = sqlite_num_rows($result);
    if($rowCheck > 0){
    while($row = sqlite_fetch_array($result)){
    //start the session and register a variable
    session_start();
    $_SESSION["username"] = $result['username'];
    $_SESSION["timestamp"] = $result['timestamp'];
    header( "Location: loginsuccess.php" );
    }
    }
    else {
    //if nothing is returned by the query, unsuccessful login code goes here...
    $loginfailure = "Sorry you have neterd an incorrect username or password. Please try agaian or contact us for your login details.";
    }
    }
    ?>


    Code:
    <?php
    session_start();
    if(!isset($_SESSION["username"])){
    header ( 'location: index.php' );
    exit;
    }
    
    else {
    $logintext = "Welcome to the site " . $_SESSION["username"] . " The last time you were here was " . $_SESSION["timestamp"] . "";
    }
    ?>

    When i login correctly and view the page the only outcome i get is
    Welcome to the site test The last time you were here was
    It doesn't seem to output the timestamp session variable.

    Cheesr

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it returning the timestamp from the database?. In session initalisation echo the $result['timestamp'] and see if it outputs also comment out header when doing this so you can actually see the results.

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    dump the entire SESSION array onto the screen to see if there is anything in it involving the timestamp.

    PHP Code:
    echo '<pre>';
    print_r($_SESSION);
    echo 
    '</pre>'
    If it's not in there then check the $result['timestamp'] value and/or dump the $result array in the same fashion
    PHP Code:
    echo '<pre>';
    print_r($result);
    echo 
    '</pre>'
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if i use this i get

    Code:
    echo '<pre>';
    print_r($result);
    echo '</pre>';
    Resource id #4

    Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0

    If i use this

    Code:
    echo '<pre>';
    print_r($_SESSION);
    echo '</pre>';
    I get

    Array
    (
    [username] =>
    [timestamp] =>
    )

    Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0
    Cheers for the help

  5. #5
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Struggling View Post
    $result = sqlite_query($db, "SELECT * FROM tbllogin where username = '$username' and password = '$encrypted_password'");
    //check that at least one row was returned
    $rowCheck = sqlite_num_rows($result);
    if($rowCheck > 0){
    while($row = sqlite_fetch_array($result)){
    //start the session and register a variable
    session_start();
    $_SESSION["username"] = $result['username'];
    $_SESSION["timestamp"] = $result['timestamp'];
    header( "Location: loginsuccess.php" );
    }
    ?>
    your problem is here... it looks like you want to use
    PHP Code:
    $_SESSION['username'] = $row['username'];
    $_SESSION['timestamp'] = $row['timestamp']; 
    INSTEAD OF $result

    Also, i'm not familiar with sql_lite, but it probably has a function to return a row instead of an array of items. If you only need 1 row from the db, there is no need for a loop. Tiny improvement for this script, but important later on.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  6. #6
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers wheeler am annoyed with myself for such a schoolboy error. Sometimes the more you work on a problem the easier the solution the harder it is to find.

    Thank You

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Good spot wheeler
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •