How to Create Login Form With Session in PHP

How to create login form with session in php - Session is quite important in web based applications. Sessions allow the programmer to store user information semi-permanently, meaning that during certain periods information is stored. Storage of the contents of the session variable resides on the server, so it can not be accessed directly by the client.
In web-based applications, sessions are widely used as login authentication. Sessions allow the programmer to manage who can access a page. For example, to see the mailbox page in the email, we must login first. In the login process among others will occur making a session that will be brought by the user on each page. On the mailbox page, the session will be checked. If the session is correct then the user is welcome to open the mailbox page, but if wrong then the user can not open the mailbox page and usually will be prompted to login first. That is, the user can not access the mailbox page directly without logging in.
In the session handling there are several processes that need to be considered:
  • Session creation process
  • Session inspection process
  • Session removal process
Next how the session itself is run? In order for the storage process in the session to run, PHP does the following:
1. PHP generates an ID session.
This session ID is a random number of random numbers for each user and almost impossible to guess. The session ID is stored by PHP inside the PHP system variable named PHPSESSID
2.PHP stores the value you want to store in a session inside a file located on the server.
The filename for the session storage matches (same) with the session ID. Files are stored in a directory indicated by session.save_path in the php.ini file.
3. PHP throws an ID session into every page.
4. PHP retrieves session values from session files for each session page.
Here is an example of a program to create login form using session in php.

How to Create Login Form With Session in PHP

Program 1
File Name: session01.php
Description: The program creates session.
Type the following php code and html code into notepad.
<?php
/****************************************************
File name: session01.php
This page is a sample session creation page.
The session_start() command must be placed in the first command
Without spaces in front of it. The session_start() command must exist
On every page associated with the session
*****************************************************/
session_start();
if (isset ($_POST['Login'])) {
$user = $_POST['user'];
$pass = $_POST['pass'];
//check login
 if ($user == "johans" && $pass = "123") {
//create session
$_SESSION['login'] = $user;
  //goes to the session proofing page
  echo "<h1>You have successfully LOGIN</h1>";
  echo "<h2>Click <a href='session02.php'>here (session02.php)</a>
  To go to the session proofing page";
    }
} else {
?>
<html>
<head>
<title>Login here...</title>
</head>
<body>
 <form action="" method="post">
    <h2>Login Here...</h2>  
    Username : <input type="text" name="user"><br>  
    Password : <input type="password" name="pass"><br>  
    <input type="submit" name="Login" value="Log In"> 
 </form>
</body>
</html>
<?php } ?>

How to Create Login Form With Session in PHP
html and php code

Save the php code with the name session01.php in the htdocs folder.
Program Explanation 1
In the above program there is a function session_start() located on the 9th row. The session_start() function works to start a session. The function must first be called in a PHP page, meaning that the session_start() function must be called before something appears on the screen, even if it is just a space.
Next, program 1 will display a simple login form on the screen. The form consists of a username form, password and a login button. If the login button is pressed then check condition on line 10 will be TRUE and the command in block if will be executed. Lines 11 and 12 are commands to retrieve values on the username and password input form. The username and password value is checked on line 14. Checking this username and password, for web applications that already use the database, can be replaced by the username and password checking into the database table.
At line 16, there is a command as follows:
$_SESSION['login'] = $user;
The command line is a command to create a new session (create session) where the session name is "login" and the contents of the session are $user. $_SESSION is a global array variable defined by PHP, so this variable should be written in upper-case. Next, on line 18-20 will display a message that the login was successful and also a link to the session proofing page (session02.php, program 2).
Program 2
File Name: session02.php
Description: Session inspection program.
Type the following php code in notepad.
<?php
/*************************************************************
This page is an example of a session check page.
Session checks are usually performed if a page has
Limited access. For example must login first.
**************************************************************/
session_start();
//session check
if (isset($_SESSION['login'])) { //if it is logged
 //displays session contents
 echo "<h1>Welcome ". $_SESSION['login'] ."</h1>";
 echo "<h2>This page is only accessible if you are already
 logged in</h2>";
 echo "<h2>Click <a href='session03.php'>here
(session03.php)</a> For LOGOUT</h2>";
} else {
 //session has no meaning yet login
 die ("You are not logged in! You are not eligible to enter this page.
Please login <a href='session01.php'>here</a>");
}
?>

How to Create Login Form With Session in PHP
php code

Save the php code with the name session02.php in the htdocs folder.
Program Explanation 2
Program 2 above begins with a function call session_start(). This means that in this program page there is a session operation. In line 10 of the above program, there is an examination of the existence of the $_SESSION['login'] variable by using the isset() function. Checking this condition will be TRUE if $_SESSION ['login'] is already formed. If the value is TRUE then block the program line 11-14 to be executed, and vice versa, if FALSE then block the program line 16-17 to be executed. Line 11-14 program blocks can only be accessed when the user is logged in correctly. In other words, the section is part of a private (limited).
To display the contents of session variable can be done as in line 12 program above. Simply by echoing the $_SESSION['login'] variable.
If the user has not logged in (did not pass the login page) or directly access this page, it will display a message that the user is not entitled to enter this page. The die() function on line 17 is used to display a message as well as terminate the program, meaning that a command other than die() will be ignored.
Program 3
File Name: session03.php
Description: The program deletes session.
Type the following php code in notepad.
<?php
/********************************************************
This page is a logout page, where we delete
the existing session.
*********************************************************/
session_start();
if (isset($_SESSION['login'])) {
unset ($_SESSION);
session_destroy();
//
 echo "<h1>You have successfully LOGOUT</h1>";   echo "<h2>Click <a href='session01.php'>here</a> To LOGIN back</h2>";
 echo "<h2>You can not now enter the page  <a href='session02.php'>session02.php</a> again</h2>";
}
?>

How to Create Login Form With Session in PHP
php code

Save the php code in the htdocs folder named session03.php
Program Explanation 3
Program 3 is an example of a session deletion program. Session deletion is usually used when the user logs in. The session deletion process is performed by calling the unset() function and the session_destroy() function. The unset() function is used to remove or remove a variable. While session_destroy() is used to delete all data associated with the session.
Try running the program. Open the browser then type http://localhost/session01.php in the address bar, then enter. It will look like this.
How to Create Login Form With Session in PHP
Display form

How to Create Login Form With Session in PHP
Form in the contents of username and password

How to Create Login Form With Session in PHP
Successfully login

How to Create Login Form With Session in PHP
Welcome view

How to Create Login Form With Session in PHP
Successfully logout

How to Create Login Form With Session in PHP
Please login again

That is the tutorial How to create login form with session in php which I can share. Hopefully these tutorials can help you and improve our ability in learning the php programming language. Learn also this tutorial: How to Upload File Using PHP.

Comments

Post a Comment