Jack
Jack

Reputation: 95

Why are my cookies not working PHP

I feel as if i have everything in the right place however my cookies just don't seem to be storing if anyone could spot my error would be much appreciated.

I can log in fine it just doesn't seem to save the cookie as when i log out nothing appears back in my log in field.

index page -

<form action="controller/authentication.php" method="post">
           <div class="text-danger"><?php if(isset($message)) { echo $message; } ?></div>
                <div class="form-group">  
                 <label for="login">Username</label>
                 <input name="member_name" type="text" value="<?php if(isset($_COOKIE["member_login"])) { echo $_COOKIE["member_login"]; } ?>" class="form-control" />
                </div>
                <div class="form-group">
                 <label for="password">Password</label>
                 <input name="member_password" type="password" value="<?php if(isset($_COOKIE["member_password"])) { echo $_COOKIE["member_password"]; } ?>" class="form-control" />
                </div>
                <div class="form-group">
                 <input type="checkbox" name="remember" <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?> />
                 <label for="remember-me">Remember me</label>
                </div>
                <div class="form-group">
                 <div><input type="submit" name="login" value="Login" class="btn btn-success"></span></div>
                </div>
                </form>

authentication page -

//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
$name = $_POST['member_name'];
$password = $_POST['member_password'];
if(isset($_POST["login"]))
{
 if(!empty($_POST["member_name"]) && !empty($_POST["member_password"]))
 {
   //call the retrieve_salt() function
   $result = retrieve_salt($name);

   //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($name, $password);

  if($count == 1)
  {
   if(!empty($_POST["remember"]))
   {
    setcookie ("member_login",$name,time()+ (10 * 365 * 24 * 60 * 60));
    setcookie ("member_password",$password,time()+ (10 * 365 * 24 * 60 * 60));
    $_SESSION["user"] = $name;
   }
   else
   {
    if(isset($_COOKIE["member_login"]))
    {
     setcookie ("member_login","");
    }
    if(isset($_COOKIE["member_password"]))
    {
     setcookie ("member_password","");
    }
   }
   header("location:../view/products.php");
  }
  else
  {
   $message = "Invalid Login";
  }
 }
 else
 {
  $message = "Both are Required Fields";
 }
}



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:../index.php');
    }

Login Function -

function retrieve_salt($username)
{
    global $conn;
    $sql = 'SELECT * FROM customer WHERE username = :username';
    $statement = $conn->prepare($sql);
    $statement->bindValue(':username', $username);
    $statement->execute();
    $result = $statement->fetch();
    $statement->closeCursor();
    return $result;
}

//create a function to login
function login($username, $password)
{
    global $conn;
    $sql = 'SELECT * FROM customer WHERE username = :username AND password = :password';
    $statement = $conn->prepare($sql);
    $statement->bindValue(':username', $username);
    $statement->bindValue(':password', $password);
    $statement->execute();
    $result = $statement->fetchAll();
    $statement->closeCursor();
    $count = $statement->rowCount();
    return $count;
}

Edited to add login functions

Upvotes: 2

Views: 1293

Answers (1)

Jack
Jack

Reputation: 95

So my issue was that i was calling files from other folders which require '/' in the setcookie statement.

so i changed -

setcookie ("member_login",$name,time()+ (10 * 365 * 24 * 60 * 60));

to -

setcookie ("member_login",$name,time()+ (10 * 365 * 24 * 60 * 60) , '/');

If you are having trouble understanding why you can't seem to view your cookie var_dump($_COOKIE);

Is a great feature

Hope this helps somebody down the track.

Upvotes: 1

Related Questions