Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Undefined index

  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Undefined index

    I'm getting this error only when I click remember me for my log in, close my browser, and return to the index (which is directed right to the members page) for "HELLO, $username". Otherwise, when I click remember me and sign in, my index works fine.

    login.php:
    PHP Code:

    <?php

    include 'functions.php';


    $username $_POST['username'];
    $password $_POST['password'];
    $rememberme $_POST['rememberme'];

    if (
    $username&&$password)
    {

     
    $login mysql_query("SELECT * FROM users WHERE username='$username'");
     
    $numrows mysql_num_rows($login);
     
    if (
    $numrows!=0)
    {

     while (
    $row mysql_fetch_assoc($login))
     {
           
    $dbpassword $row['password'];
           
    $dbusername $row['username'];
           if (
    $username==$dbusername&&$password==$dbpassword)
           {
              
    $loginok TRUE;

      if (
    $loginok==TRUE&&$rememberme="on")
      {
            
    setcookie("username"$usernametime()+7200);
            
    $_SESSION['username']=$username;
            
    header("Location: members.php");
              exit();

        } else if (
    $loginok==TRUE&&$rememberme=="") {
              
    $_SESSION['username']=$username;

              
    header("Location: members.php");
              exit();
           }
         else
         
    $loginok FALSE;
         
    header("Location: incorrect.html");
         exit();
     }
         else
        
    header("Location: incorrect.html");
        exit();
    }
     }
    }
         else
        
    header("Location: incorrect.html");
        exit();



    ?>
    members.php:
    PHP Code:
    <?php
            
    include 'functions.php';

    if (!
    loggedin())
    {
       
    header("Location: index.php");
       exit();

    }

            if (
    $_SESSION['username'])
            {
            echo 
    "HELLO, ".$_SESSION['username']."!";
            }

            
    ?>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,512
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    PHP SESSIONS are only active until you close your browser.
    After that, they are discarded ... gone forever.

    You need to use cookies instead.

    But don't save usernames and passwords with cookies.
    Create a new column in your database called "tokenKey" and
    each time a person wants to be remembered (remember me on this PC),
    you generate a random 10 character key and save it BOTH in the cookie
    and in the row for that user.

    Any time that user visits the site, if a cookie exists and there is a tokenKey,
    it will find that user's row in the database, and log them in.

    That's why you always see the warning ...
    If your computer is shared by others, do not click "remember me on this computer".

    Once logged in, re-generate a new tokenKey and resave the cookie and update the column.

    ... and also, at the top of every script that uses SESSION,
    you must start out like this:
    <?php
    session_start();

    If your "function.php" does that already, you're covered.


    .


  •  

    Posting Permissions

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