This blog will explain you how cookies can be created and retrieve using php and where it could be store.

What is cookies?

A cookie is a small piece of information store in browser on client’s pc. The maximum size of cookie to store data is 4KB. Cookie can be accessed within entire website i.e in every page. Cookie can be used as following two ways,

  • We can store and retrieve cookies to browser as per our requirements. We can remove cookies directly from browser or by giving it expiry date.
  • At a time of http request and response occurs. Means when used request server for particular website it will store cookies to identify each user uniquely. If same user sends request these cookies are used to identify user.

Syntax

setcookie(cookie_name, cookie_value, time);

Example

<?php

    $cookie_name = "User";

    $cookie_value = "Tarun Vaghela";

    setcookie($cookie_name, $cookie_value, time() + (86400));  /* 86400 = 1 day */

?>

I am assuming that you have already created a database and table for login with columns UserId, UserName, UserEmail, UserPassword. Enter some dummy data into it or if you want to insert data using php code please read my blog CRUD Operation in PHP with example.

PHP Example

Here I have implemented Remember me feature while you login into website. This feature will help user not to remember their email and password when every time they login. Email and password will automatically filled if user has checked Remember me once at a time of login.

To store user’s email and password I have use cookies with name "useremail" and "userpassword". Below is my code to store and retrieve cookies. User has to check Remember me checkbox to store cookie and if user uncheck it cookie will be remove from browser.

login.php

<?php

    $con = new mysqli("localhost","root","","sample");   /* Connection code(Connect with database) */

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Login</title>

    <style>

        input[type=text],input[type=email],input[type=password]{

            width:100% !important;

        }

        table{

            border: 1px solid rgb(202,207,210);

        }

        form {

            margin: 10% auto 0;

        }

    </style>

</head>

<body>

    <form method="post">

       <center><h1>Login Form</h1></center>

        <table align="center" border="0">

            <tr>

                <td>Email Address:</td>

                <td><input type="email" value="<?php if(isset($_COOKIE["useremail"])) { echo $_COOKIE["useremail"]; } ?>" name="txtuemail"></td>

            </tr>

            <tr>

                <td>Password:</td>

                <td><input type="password" value="<?php if(isset($_COOKIE["userpassword"])) { echo $_COOKIE["userpassword"]; } ?>" name="txtpassword"></td>

            </tr>

    <tr>

<td><input type="checkbox" name="chkrememberme" value="Remember me" <?php if(isset($_COOKIE["useremail"])) { ?> checked="checked" <?php } ?>>Remember me</td>

    </tr>

            <tr>

                <td colspan="2" align="center"><input type="submit" value="SignIn" name="btnlogin"></td>

            </tr>

        </table>

    </form>

    <?php

/* Login Code Start */

        if(isset($_POST["btnlogin"]))   /* Login button click event */

        {

    $res=$con->query("select * from tbllogin where UserEmail='".$_REQUEST["txtuemail"]."' and UserPassword='".$_REQUEST["txtpassword"]."'") or die(mysqli_error($con));

    if(mysqli_num_rows($res)> 0)

{

  /* Checked that you checked Remember me checkbox or not */

  if(!empty($_REQUEST["chkrememberme"]))

  {

      setcookie ("useremail",$_REQUEST["txtuemail"],time()+ (86400)); /*86400 = 1day*/ 

      setcookie ("userpassword",$_REQUEST["txtpassword"],time()+ (86400)); 

  }

  else  

  {  

      if(isset($_COOKIE["useremail"]))   

{  

    setcookie("useremail","");  

}  

      if(isset($_COOKIE["userpassword"]))   

{  

    setcookie("userpassword","");  

}  

  }

  echo "<script>window.location='displaycookies.php'</script>";

}

    else

{

  echo "<script>alert('Invalid Email or Password')</script>";

}

        }

/* Login Code End */

    ?>

</body>

</html>

displaycookies.php

<?php

    if(isset($_COOKIE["useremail"]))

    {

echo "<center><h1>Cookie Value is: " . $_COOKIE["useremail"]."</h1></center>";

    }

    else

    {

echo "<center><h1>No cookies available..!!</h1></center>";

    }

?>

Path where cookies store: Browser-> Settings-> Advanced-> Content settings-> Cookies-> See all cookies and site data-> type localhost on search textbox-> click on localhost.

Hope you like the blog. Comment below if you have any questions..!!