Table of Contents
Tutorial: How to Create a Session in PHP with Free Source Code
This tutorial will teach you the basic knowledge about PHP session and an example of 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 meeting to assess the user upon login.
Creating a 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 a 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 a 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 a 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 a 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 text will store the id of the user through the 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 a 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>
Happy Coding!
Download Here