All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Use Cookies In PHP For Login

Last Updated : Mar 11, 2024

How To Use Cookies In PHP For Login

In this tutorial we will show you the solution of how to use cookies in PHP for login, as we know that cookies are small files that server embeds on user’s computer.

It means that cookies are always stored on user’s computer. One thing to note here we use cookies in database that we store from user’s computer and create, retrieve them also.

Step By Step Guide On How To Use Cookies In PHP For Login :-

Here, we have to use cookies in login form. We can use cookies in two ways. like we can store the name that entered by user in login page and we can use them in next page after login to show his name for profile purpose.

Other way is that we can store cookies for login id and password purpose. Like once use logged in and after logout when use next time opens login page then his/her login id and password must be filled there already using cookies.

// login.php

<?php session_start();
    $connect=mysqli_connect("localhost","root","","index") or die("Connection Failed");
    if(!empty($_POST['button']))
    {
  Setcookie(“name”, $_POST['username'] );
Setcookie(“email”, $_POST['email’] );
Setcookie(“pass”, $_POST[‘password'] );
        $name=$_POST['username'];
        $email=$_POST['email'];
        $pass=$_POST['password'];
        if($email== "" && $pass == "" && $name=="")
        {
            echo "<script> alert('Please fill all fields.......')</script>";
        }
        else
        {
            $query= "select * from rl_form where email='$email' and pass='$pass' and user='$name'";
            $result=mysqli_query($connect, $query);
            $count= mysqli_num_rows($result);
            if($count>0)
            {
                setcookie("username",$name);
                header("location:welcome.php");
                // echo "<script> alert('Congratulations! you are logged in .......')</script>";
            }
            else
            {
                echo "<script> alert('Username/Email and Password does not match.......')</script>";
            }
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="main">
        <div class="inner">
            <div class="welcome">
                Login Form
            </div>
            <div class="login_options">
                <form action="" method="post">
                    <div class="lleft_body">
                        <label for="">
                            Enter your username here
                        </label>
                        <br>
                        <label for="">
                            Enter your email here
                        </label>
                        <br>
                        <label for="">
                            Enter your password here
                        </label>
                    </div>
                    <div class="lright_body">
                        <input type="text" name="username" id="" value="<?php if(isset($_COOKIE["name"])) { echo $_COOKIE["name"]; } ?>">
                        <br><br>
                        <input type="email" name="email" id="" value="<?php if(isset($_COOKIE["email"])) { echo $_COOKIE["email"]; } ?>">
                        <br><br>
                        <input type="password" name="password" id="" value="<?php if(isset($_COOKIE["pass"])) { echo $_COOKIE["pass"]; } ?>">
                    </div>
                    <div class="login_submit">
                        <input type="submit" value="Login" id="login" name="button">
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as you can see that we create three cookies having name: name, email and pass. Inside them we store the values that user fill inside inputs first time. If next time he/ she replace it with another value then that value get settled inside cookies. This all is for if we want to show his name, email and password after logout from webpage.
  6. After that we next again create a cookie named username inside this we store the value of name. This is because if we want to show his name after login on next pages of profile. We can also use the profile cookie named as name. But if someone only want to set cookie if all is done then he can set cookie like this way. We hope you understand the codes easily.
  7. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand what cookies are and how to use cookies in php for login.

I hope this tutorial on how to use cookies in PHP for login helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪