Google Ads

Session Works and How to create it PHP, MySQLi PHP Script


Session Works and How to create it PHP, MySQLi PHP Script

This tutorial will teach you basic knowledge about php session and an example on how to create one.

A session is a method used to store data in a variable that can be used in all pages in a website/php program. Most of the time, sessions are used to determine the user that access that system. In this tutorial, I will give you an idea on how to create a session to determine the user upon login.
Creating our Database

First, we’re going to create a database that will store our data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as “login”.
3. After creating a database, click the SQL and paste the below code to create a table. 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;

 

 

 

Inserting Data into our Table

Next, we insert data into our database for us to determine the user. In this tutorial, we are going to insert 2 rows in our table to differentiate both users and for us to further understand how session works.
1. Click the database “login” that we created earlier.
2. Click SQL and paste the below code to insert the data.

 

INSERT INTO `user` ( `username`, `password`, `fullname`) VALUES
('user1', 'user1', 'neovic devierte'),
('user2', 'user2', 'lee ann');

 

Creating our Connection

Next, we create a database connection and save it as “conn.php”. This file will serve as our bridge between our form and our database.

 

<?php
$conn = mysqli_connect("localhost","root","","login");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

 

Creating our Login Form

Next step is creating our login form and name it “index.php”. In this form, the user will input his/her username and password and we’re going to determine that user upon submission with the use of session.
To create the form, open your HTML code editor and paste the code below after the tag.

 

<?php session_start(); ?>

<!DOCTYPE html>
<html>
<head>
<title>Session Example</title>
</head>
<body>
<h2>Login Form</h2>
<form method="POST" action="login.php">
<label>Username:</label> <input type="text" name="username"><br><br>
<label>Password:</label> <input type="password" name="password"><br><br>
<input type="submit" name="submit">
</form>
<br>
<?php

if (isset($_SESSION['message'])){
echo $_SESSION['message'];
}
unset($_SESSION['message']);
?>
</body>
</html>

 

Notice that we have declared a function “session_start()”. This function determines that the session has started and a session has been created. Without this function, sessions won’t work.
Creating our Login Script

Next step is to create our login script and name it “login.php”. This script will validate the user’s input and determine whether the user exists in our table or not. If the user is found, the script will store the id of the user through session. To create the script, open your HTML code editor and paste the code below after the tag.

 

<?php
session_start();
include('conn.php');

$username=$_POST['username'];
$password=$_POST['password'];

$query=mysqli_query($conn,"select * from `user` where username='$username' && password='$password'");
$numrows=mysqli_num_rows($query);

if ($numrows==0){
$_SESSION['message']="User not found!";
header('location:index.php');
}
else{
$row=mysqli_fetch_array($query);
$_SESSION['id']=$row['userid'];
header('location:success.php');
}
?>

 

Creating our Destination Page

Lastly, we create a destination page if the user existed in our table and name it “success.php”. This page will also show to details of the user found. To create the script, open your HTML code editor and paste the code below after the tag.

 

<?php
session_start();
include('conn.php');
$userid=$_SESSION['id'];

$userq=mysqli_query($conn,"select * from `user` where userid='$userid'");
$userrow=mysqli_fetch_array($userq);
?>
<!doctype html>
<html>
<head>
<title>Session Example</title>
</head>
<body>
<h2>User Found! </h2>
Welcome, <?php echo $userrow['fullname']; ?>
</body>
</html>

 

 


Related Post

Stock Manager Advance with Point of Sale Module v3... Stock Manager Advance with Point of Sale Module v3.0.2.23Stock Manager Advance with Point of Sale Module v3.0.2.23Stock Manager Advance with POS (Poin...
Indrajith FTP Manager PHP Script Indrajith FTP Manager PHP Script Indrajith FTP Manager is PHP based FTP File manager. It works with PHP FTP functions. PHP has wide variety of b...
School Management System 2018 PHP Script Download School Management System 2018 PHP Script Download The latest school school management system. -classic login module -appealing homepage ...
GIF Generator Using PHP Script GIF Generator Using PHP Script This project will teach you how to create a GIF animation generator using PHP. Upload an image as many as you want, ...
Transporter Script – Online Fleets Booking S... Transporter Script - Online Fleets Booking SystemTransporter Script - Online Fleets Booking SystemTransporter Script provide you ability to manage you...

Have any Question or Comment?

Leave a Reply

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