Google Ads

Setting up Cookie upon Login using PHP/MySQLi PHP Script


Setting up Cookie upon Login using PHP/MySQLi PHP Script

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 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 cookie.

Cookies are small amount of data that has been stored in user’s computer. This is used so that when the same computer access the website, some data are already available to use. Data usually saved by this method are id’s.
Creating our 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=

 

 

Creating our 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 our 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 our 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 our 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>

 

 


Related Post

Updating Multiple Rows in MySQL using PHP Script Updating Multiple Rows in MySQL using PHP Script There are so many tutorials on how to update one (1) record at a time. But only few tutorial on ho...
PHPFox v3.0.1 PHP Script Free Download PHPFox v3.0.1 PHP Script Free Download Create a beautiful Social Network like Facebook, Twitter, Myspace etc. This is a popular PHP Script in th...
Status Checker v1.1.0 – PHP Site Server Stat... Status Checker v1.1.0 - PHP Site Server StatusStatus Checker v1.1.0 - PHP Site Server StatusStatus Checker is a PHP script that allows you to post upd...
Dynamically Get the Value of a Select Tag from MyS... Dynamically Get the Value of a Select Tag from MySQL Table PHP Script This tutorial will help you on how to dynamically get the value of a select t...
Youtube Video Upload PHP Script Youtube Video Upload PHP Script Learn how to create youtube upload video without refreshing the page tutorials.This tutorial is to teach you on how...

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *