Jack
Jack

Reputation: 95

Cookies Remember me issue PHP

I am at an absolute pickle. I am wanting to create a remember me cookie. Following guides on the net, I am assuming I need to add:

if($_POST['remember']){
    setcookie('remember_me', $_POST['username'], $year);
}
elseif(!$_POST['remember']){
    if(isset($_COOKIE['remember_me'])){
        $past = time() - 100;
        setcookie(remember_me, gone, $past);
    }
}

$year = time() + 31536000;
setcookie('remember_me', $_POST['username'], $year);

to my authentication.php file to set the if statement and create the cookie.

and then setting the value of the username to <?php echo $_COOKIE['remember_me']; ?>

which I have done:

<form action="../controller/authentication.php" method="post">
       <div class="form-group">
         <label>Username</label>
        <input class="form-control" placeholder="Username" name="username" type="text" value="<?php echo $_COOKIE['remember_me']; ?>">>

    </div>
    <div class="form-group">
      <label>Password</label>
      <input class="form-control" placeholder="Password" name="password" type="password" value="">
    </div>
    <div class="checkbox">
        <label>
          <input name="remember" id="remember" type="checkbox" value="1">Remember Me
        </label> <br>
        <small id="signup" class="form-text text-muted">Don't have an account yet? <a href='signup.php'>Sign Up</a>.</small>
      </div>
             <button type="submit" class="btn btn-primary">Login</button>
     </form>

Now the issue is saying that Notice: Undefined index: username in C:\Users\Bob\Desktop\Xammp\htdocs\Shopping_List\view\index.php on line 7 so it doesn't seem to be reading my cookie statement within the form correctly and I don't know if the cookie statement is set up correctly...

Not sure if this helps but I will show you my authentication file as well

    <?php
//start session management
    session_start();
//connect to the database
require('../model/connection.php');
//retrieve the functions
require('../model/functions_users.php');

//retrieve the username and password entered into the form
$username = $_POST['username'];
$password = $_POST['password'];



if($_POST['remember']){
    setcookie('remember_me', $_POST['username'], $year);
}
elseif(!$_POST['remember']){
    if(isset($_COOKIE['remember_me'])){
        $past = time() - 100;
        setcookie(remember_me, gone, $past);
    }
}

$year = time() + 31536000;
setcookie('remember_me', $_POST['username'], $year);

//call the retrieve_salt() function
$result = retrieve_salt($username);

//retrieve the random salt from the database
$salt = $result['salt'];
//generate the hashed password with the salt value
$password = hash('sha256', $password.$salt);

//call the login() function
$count = login($username, $password);

//if there is one matching record
if($count == 1)
{
    //start the user session to allow authorised access to secured web pages
    $_SESSION['user'] = $user;
    //if login is successful, create a success message to display on the products page
    $_SESSION['success'] = 'Hello ' . $username . '. Have a great day!';


    //redirect to products.php
    header('location:../view/products.php');
}
else
{
    //if login not successful, create an error message to display on the login page
    $_SESSION['error'] = 'Incorrect username or password. Please try again.';
    //redirect to login.php
    header('location:../view/index.php');
    }
?>

Any help or tips would be much appreciated.

Upvotes: 1

Views: 1207

Answers (1)

Kingston Fortune
Kingston Fortune

Reputation: 957

If it says Notice: Undefined index: username then its an issue with initialization.

in your first file set $_COOKIE[] to an empty string first, because it has not been initialized yet, so putting it into your html view will definitely give you an error...

$_COOKIE['remember_me'] = "";

In the first part of your code, I wonder why you wrote it like that because i believe it would throw an error by declaring an empty year variable inside your if, instead you should have this

if($_POST['remember']){
  $year = time() + 31536000;
  setcookie('remember_me', $_POST['username'], $year);
}
elseif(!$_POST['remember']){
    if(isset($_COOKIE['remember_me'])){
        $past = time() - 100;
        setcookie(remember_me, gone, $past);
    }
}

Upvotes: 1

Related Questions