0

I have created a page that allow for logging in. There are a couple of other pages used for connecting to database and redirting to another page after a successful login, etc. Anyway, there is a variable I have used called "$rememberme" which checks if the remember me checkbox is checked or not. Problem is, when I enter a wrong password, it says, "Notice: Undefined index: rememberme in /home/stud/1/1520621/public_html/Car_Club_Task2/index.php on line 30". I checked line 30 and that is where the $rememberme variable is declared.

Any suggestions? Probably something really simply I'm missing!

<!DOCTYPE HTML>


<html>
    <head>
      <title>Midlands Car Club</title>
      <link rel="stylesheet" type="text/css" href="styles.css" media="screen">
    </head>

    <div id="header">
      <h1>Midlands Car Club Log-In</h1>
    </div>    

    <?php
    include 'connect.php';

    //if logged in equals true (running loggedin function from connect.php page)
    if(loggedin())
    { 
      //redirects to welcome.php if logged in
      header("Location: welcome.php");
      //for browsers that don't allow redirect headers
      exit();
    }

    // Has submit button been pressed?
    if (isset($_POST['login']))
    {
      //get data
      $username = $_POST['username'];
      $password = $_POST['password'];
      $rememberme = $_POST['rememberme'];

      //if username AND password have been entered
      if ($username&&$password)
      {
        //select all from Login table
        $login = mysql_query("SELECT * FROM Login WHERE Username='$username'");
        // can refer to particular coloumns. Creating array of all data found in above query
        while($row = mysql_fetch_assoc($login))
        {
          //row equal to array - where it finds username in db, it finds password in that row because of column name
          $db_password = $row['Password'];
          if ($password==$db_password)
          {
            //create variable with value true (boolean)
            $loginOK = TRUE;
          }
          else
          {
            //create variable with value false (boolean)
            $loginOK = FALSE;
          }

          if ($loginOK==TRUE)
          {
            //if remember me selected
            if ($rememberme=="on")
              // set cookie with current time plus 48 hours (write time in seconds)
              setcookie("username", $username, time()+7200);
            // if remember me not selected
            else if ($rememberme=="")
              //value of session is equal to value of username
              $_SESSION['username']=$username;

            //redirect
            header("Location: welcome.php");
            //if header redirect is disabled in browser
            exit();     
          }
          else
          {
            die("Incorrect Username or Password");
          }
        }        
      }
      else
      {
        // kill script
        die("Please enter username and password");
      }

    }

    ?>    
    <body>
      <div class="add">          
        <form method="POST" action="index.php">
        Username: <input type="text" name="username"><br>
        Password: <input type="password" name="password"><br>
        <input type="checkbox" name="rememberme">Remember Me<br>
        <input type="submit" name="login" value="Log In">
        </form>
      </div>
    </body>
  </html>
  • If the checkbox **rememberme** is not checked, it does not submit any value, so `$_POST['rememberme']`is NOT set – swidmann Dec 07 '15 at 16:07
  • I got that far, but I don't want it to set anything, as if you try to log in with the wrong details, and remember me isn't selected, I don't want remember me to do anything. How do I go about doing this? –  Dec 07 '15 at 16:34
  • You can use [isset()](http://php.net/manual/en/function.isset.php) – swidmann Dec 07 '15 at 17:17
  • So "$rememberme = if(isset($_POST['rememberme']));" ? –  Dec 07 '15 at 18:13
  • Yes (almost), take a look [here](http://php.net/manual/en/language.operators.comparison.php) and search for the ternary operator – swidmann Dec 07 '15 at 21:33

0 Answers0