This blog will explain you how cookies can be created and retrieve using CodeIgniter 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

$this->input->set_cookie(cookie_name, cookie_value, time);

Example

<?php

    $cookie_name  = "User";

    $cookie_value = "Tarun Vaghela";

    $this->input->set_cookie($cookie_name, $cookie_value, time() + (86400)); /* 86400 = 1 day */

?>

I’m assuming that you created database and table for tbluser (Columns: UserId,UserName,UserEmail,UserPassword,UserGender,UserHobbies,UserProfile). From this table I use UserEmail and UserPassword columns for login.

CodeIgniter 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 "uemail" and "upassword". 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.

Controller

Create a controller in application-> controllers folder. Write a code to login user, store and remove cookies.

crudController.php

<?php

    defined('BASEPATH') OR exit('No direct script access allowed');

    class crudController extends CI_Controller

    {

        function __construct()

        {

            parent::__construct();

            $this->load->helper('url'); /* load base url */

            $this->load->model('crudModel'); /* load model default which you create */

            $this->load->helper('cookie'); /* load cookie default form helper */

        }

        

        /* Set index page – Load you Login page here */

        public function index()

        {

            $this->load->view('login');

        }

        

        /* Function for user login – called when user click on login button */

        public function UserLogin()

        {

            if ($this->input->post("btnlogin"))

            {

                $uemail = $this->input->post("txtuemail");

                $upass  = $this->input->post("txtpassword");

                

                $success = $this->crudModel->userlogin($uemail, $upass);

                if ($success)

                {

                    if ($this->input->post("chkremember"))

                    {

                        $this->input->set_cookie('uemail', $uemail, 86500); /* Create cookie for store emailid */

                        $this->input->set_cookie('upassword', $upass, 86500); /* Create cookie for password */

                        

                        echo "<script>alert('Login successfully..!!');window.location='http://localhost/CRUDOperationCI/index.php/crudController/DisplayCookieData'</script>";

                    }

                    else

                    {

                        delete_cookie('uemail'); /* Delete email cookie */

                        delete_cookie('upassword'); /* Delete password cookie */

                        echo "<script>alert('Login successfully without create cookies..!!');window.location='http://localhost/CRUDOperationCI/index.php/crudController/DisplayCookieData'</script>";

                    }

                }

                else

                {

                    echo "<script>alert('Error while login..!!');window.location='http://localhost/CRUDOperationCI/index.php/crudController'</script>";

                }

            }

        }

        

        /* Fuction for display cookies */

        public function DisplayCookieData()

        {

            $this->load->view('displaylogincookiedata'); /* load displaylogincookiedata.php file form views folder */

        }

    }

?>

Model

Create model in application->models folder. Write query to login user.

crudModel.php

<?php

    class crudModel extends CI_Model

    {

        /* Login query */

        public function userlogin($uemail, $upass)

        {

            $this->db->select('*');

            $this->db->from('tbluser');

            $this->db->where('UserEmail', $uemail);

            $this->db->where('UserPassword', $upass);

            $query = $this->db->get();

            return $query->row();

            

        }

    }

?>

Views

After that you have to create login.php view for login and displaycookiedata.php view for displaying created cookie data in application->views folder. Below the code for login.php file.

login.php

<!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" action="<?php echo base_url(); ?>index.php/crudController/UserLogin">

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

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

            <tr>

                <td>Email Address:</td>

                <td><input type="email" name="txtuemail" value="<?php if (get_cookie('uemail')) { echo get_cookie('uemail'); } ?>"></td>

            </tr>

            <tr>

                <td>Password:</td>

                <td><input type="password" name="txtpassword" value="<?php if (get_cookie('upassword')) { echo get_cookie('upassword'); } ?>"></td>

            </tr>

            <tr>

                <td><input type="checkbox" name="chkremember" value="Remember me" <?php if (get_cookie('uemail')) { ?> checked="checked" <?php } ?>>Remember me</td>

            </tr>

            <tr>

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

            </tr>

        </table>

    </form>

</body>

</html>

displaylogincookiedata.php

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Display Data</title>

    <style>

        body{

            margin: 10% auto 0;

        }

        td{

            text-align: center;

        }

    </style>

</head>

<body>

    <center><h1>Display Cookie Data</h1></center>

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

        <?php

        if (get_cookie('uemail'))

        {

?>

    <tr>

                <td><h2>Welcome user: <?php echo get_cookie('uemail'); ?></h2></td>

            </tr>

        <?php

        }

        else

        {

?>

           <tr>

                <td><h2>No cookies created or available</h2></td>

            </tr>

        <?php

        }

?>

   </table>

</body>

</html>

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 to ask query.. !!