In my previous post, I created a Simple Login with Validation, so I’ve decided to create another tutorial to show how to set up a cookie upon user login. But in this tutorial, I have created a simple login since the focus of this tutorial is to give you knowledge on how to set up the cookie.
Cookies are a small amount of data that has been stored in the user’s computer; this is used so that when the same network access the website, some data are already available to use. Data usually saved by this method are ids.
Table of Contents
Creating a Database
First, we’re going to create a database that contains our data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as “cookie”.
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
CREATE TABLE `user` ( `userid` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(30) NOT NULL, `password` VARCHAR(30) NOT NULL, `fullname` VARCHAR(60) NOT NULL, PRIMARY KEY (`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Creating a Connection
Next step is to create a database connection and save it as “conn.php”. This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.
<?php $conn = mysqli_connect("localhost","root","","cookie"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?>
Creating a Login Form
Next is to create our login form. To create the form, open your HTML code editor and paste the code below after the tag. We name this as “index.php”.
<!DOCTYPE html> <html> <head> <title>Setting Up Cookie on User Login</title> </head> <body> <h2>Login Form</h2> <form method="POST" action="login.php"> <label>Username:</label> <input type="text" name="username"> <label>Password:</label> <input type="password" name="password"><br><br> <input type="checkbox" name="remember"> Remember me <br><br> <input type="submit" value="Login" name="login"> </form> <span> <?php session_start(); if (isset($_SESSION['message'])){ echo $_SESSION['message']; } unset($_SESSION['message']); ?> </span> </body> </html>
Creating a Login Script
Next step is creating our login script. We name this script as “login.php”. If the user checks the “Remember me” checkbox, it will store user information via cookie.
<?php if(isset($_POST['login'])){ session_start(); include('conn.php'); $username=$_POST['username']; $password=$_POST['password']; $query=mysqli_query($conn,"select * from `user` where username='$username' && password='$password'"); if (mysqli_num_rows($query) == 0){ $_SESSION['message']="Login Failed. No user Found!"; header('location:index.php'); } else{ $row=mysqli_fetch_array($query); if (isset($_POST['remember'])){ //set up cookie $name_cookie = "user"; $value_cookie = $row['userid']; setcookie($name_cookie, $value_cookie, time() + (86400 * 30)); // cookie will expire in a month, 86400 = 1 day } $_SESSION['id']=$row['userid']; header('location:success.php'); } } else{ header('location:index.php'); $_SESSION['message']="Please Login!"; } ?>
Creating a Login Success Page
Lastly, we create a go to page if login is successful. This page will show the user that login successfully. We name this page as “success.php”.
?php session_start(); if (!isset($_SESSION['id']) ||(trim ($_SESSION['id']) == '')) { header('index.php'); exit(); } include('conn.php'); $query=mysqli_query($conn,"select * from user where userid='".$_SESSION['id']."'"); $row=mysqli_fetch_assoc($query); ?> <!DOCTYPE html> <html> <head> <title>Setting Up Cookie on User Login</title> </head> <body> <h2>Login Success</h2> <?php echo $row['fullname']; ?> <br> <a href="index.php">Back</a> </body> </html>
Happy Coding!
Download Here