All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Use Session In PHP For Login Form

Last Updated : Mar 11, 2024

How To Use Session In PHP For Login Form

In this article we will show you the solution of how to use session in PHP for login form, an entire website is accessed through a session. There are temporary directories created by sessions on the server.

These variables are used to store session variables and their values.

Throughout this small project, sessions are used to access website page data. Data from sessions is accessed through pages.

The pages cannot be accessed by unknown persons after the session is destroyed. Using PHP sessions has this major advantage.

Let's begin by creating some pages.

The login process allows users access to all pages. In the case of an uncreated or destroyed session, you will be redirected to the login page. PHP page.

There are a lot of pages on a website. Accessing sessions on website pages is explained on these pages.

You can insert data into the table using the registration form or insert query. The data must be fetched when you create a login form.

Your login system cannot be created unless there are data in the database.

Step By Step Guide On How To Use Session In PHP For Login Form :-


<?php session_start ();
header ("Location: home.php");
} if(isset($_POST['login']))
$user = $_POST['user'];
 $pass = $_POST['pass'];
if ($user "TalkersCode" && $pass "1234")
// username is set to "TalkersCode" and Password
echo '<script type="text/javascript"> window. open("home.php","_self");
} else
echo "Invalid UserName or Password";
 } } ?>
 <title> Login Page </title> </head>
<body> <form action="" method="post">
<table width="200" border="0">
<tr> <td> UserName</td>
 <td> <input type="text" name="user" > </td>
 </tr> <tr> <td> PassWord </td>
 <td><input type="password" name="pass"></td> </tr>
 <tr> <td> <input type="submit" name="login" value="LOGIN"></td>
<td></td> </tr> </table> </form> </body>


<?Php session_start ();
 <head> <title> Home </title> </head>
<?php if (! isset($_SESSION['use']))
header ("Location: Login.php");
} echo $_SESSION['use'];
echo "Login Success";
 echo "<a href='logout.php'> Logout</a> ";
</body> </html>


<?php session_start ();
echo "Logout Successfully ";
 session_destroy ();
  1. The short tags start with "<?" and end with "?>". Short style tags are only available when they are enabled in the php.ini configuration file on servers.
  2. session starts with the help of this function if(isset($_SESSION['use'])) Checking whether the session is already there or not if true then the header redirects it to the home page directly.
  3. If isset it checks whether the user clicked the log-in button or not.
  4. Then the username is set to "Ank" and the Password
  5. Then Successful Login redirects to home.php
  6. In our first step, we write <HTML>, which tells the browser what HTML version we're using. Documents in HTML begin with tags.
  7. Using the <head> tag, we will explain the project's heading.
  8. <title> are open and </title> and then head closed using </head>
  9. To define the body of the webpage, the <body> tag is used. This is where we write all the content on our website.
  10. Then the form is open and the main body is created.
  11. Then the table is created using the table tag.
  12. Then table row and table draw are used for creating a username, password, and login creation.
  13. Then </td> </tr> </table> </form> </body> </html> tags are closed for executing program.

Conclusion :-

Web developers face several interesting challenges when creating APIs with PHP code.

The PHP framework community does not have a lot of frameworks that cover this area and its various aspects, such as accessing databases, performing CRUD operations, controlling access to data, changing environment variables, and operating on databases.

The JSON-API is worth considering for its many advantages, which include defining API endpoints, solving HTTP requests, and much more.

I hope this article on how to use session in PHP for login form helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪