I don't know what is going wrong in my code.I have already implemented login and signup using ajax calls. Now, I am trying to implement remember me using cookies in php. My code for that goes like this
<?php require 'database.php';
session_start();
if(!empty($_POST["login"])) {
#$conn = mysqli_connect("localhost", "root", "", "blog_samples");
$sql = "Select * from login where Username = '" . $_POST["member_name"] . "' and Password = '" . ($_POST["member_password"]) . "'";
$result = mysqli_query($conn,$sql);
$user = mysqli_fetch_array($result);
if($user) {
if(!empty($_POST["remember"])) {
setcookie ("member_login",$_POST["member_name"],time()+ (10 * 365 * 24 * 60 * 60));
setcookie ("member_password",$_POST["member_password"],time()+ (10 * 365 * 24 * 60 * 60));
} else {
if(isset($_COOKIE["member_login"])) {
setcookie ("member_login","");
}
if(isset($_COOKIE["member_password"])) {
setcookie ("member_password","");
}
}
header("location:private.php");
}
}
?>
<!DOCTYPE html>
<html >
<body>
<div class="container-fluid">
<div class="form-body-login col-md-6 col-xs-6">
<form class="Login" method="post" action="private.php">
<div class="row buttons">
<div type="submit" class="col-md-6 col-xs-6 login">Login</div>
<div type="submit" class="col-md-6 col-xs-6 sign_up">Sign up</div>
</div>
</div>
<div class="login_body">
<div class="row user_name">
<i class="fa fa-user fa-2x col-md-1 col-xs-1" aria-hidden="true"></i>
<input type="text" class="col-md-10 col-xs-10 username" id="username_login" placeholder="abc@xyz.com" name="member_name" value="<?php if(isset($_COOKIE["member_login"])) { echo $_COOKIE["member_login"]; } ?>" />
</div>
<div class="row pwd">
<i class="fa fa-key fa-2x col-md-1 col-xs-1" aria-hidden="true"></i>
<input type="password" class="col-md-10 col-xs-10 password" id="password_login" placeholder="Password" name="member_password" value="<?php if(isset($_COOKIE["member_password"])) { echo $_COOKIE["member_password"]; } ?>"/>
</div>
<div class="row remember col-md-11 col-xs-11">
<label><input type="checkbox" name="autologin" id="checkbox" name="remember" <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?>/> Remember Me </label>
</div>
<div class="row col-md-11 col-xs-11">
<button type="submit" id="button" name="login">Login</button>
</div>
</div>
<div class="form-body-signup">
<div class="row username_signup">
<input type="text" id="username_signup" class="col-md-10" placeholder="Enter user name"/>
</div>
<div class="row password_signup">
<input type="password" id="password_signup" class="col-md-10 col-xs-10" placeholder="Enter password here" />
</div>
<div class="row phone">
<input type="text" id="phone" class="col-md-10 col-xs-10" placeholder="enter phone number" />
</div>
<div class="row signup_button">
<button type="submit" id="check">Create New User</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
On login or signup, after doing all the necessary validations, the page directs to private.php. That page looks something like this:
<?php
echo"Hello";
session_start();
#$_SESSION["member_id"] = "";
session_destroy();
#header("Location: ./");
?>
But somehow, the cookies are not getting stored if I check the remember me check box. I am doing this for the first time and don't really know what is going wrong here.