Hello Friends, In this blog post Login Form: Simple PHP Login Form With Session Using PHP, I am going to discuss how to develop a registration and login form in PHP and MY SQL. Here you can easily find the detailed script of the Login page in PHP with database source code.
It is always necessary to have a login form(Simple PHP Login Form With Session Using PHP) on your website if you are making any product and service-based business with your website. If you are seeking login system PHP source code then you have come up to the right place to fulfill your needs.
We have explained here a complete login system(Simple PHP Login Form With Session Using PHP) PHP MySQL, which will be a great help for you to make an amazing login form for your website you can also customize this form by changing some code in the file as per your desire.
Within this blog(Simple PHP Login Form With Session Using PHP), we will explore Session in PHP example for login and logout, PHP login session with the database, login, and logout using session in PHP and Mysqli, How to create a login page in PHP, and MySQL with the session, Registration, and login form in PHP and MySQL.
You can easily learn how to set the Session in PHP example for login and logout using the code given below. And easily play around with creating the session in PHP(PHP Login Form With Session).
How to make a Login form in PHP with session and validation? / PHP Login Form With Session?
Login(PHP Login Form With Session) is a very important facility in any kind of login form.
it provides authentication and authorization to both the user and the owner.
If you are selling a product or running a subscription-based service then it becomes very important to have a login form so that…
we could maintain our user profile and users can explore the domain of their account without touching other’s data.
In this function, you will see a login form and when you enter the username and password then you will be redirected to your account or profile with a session created.
In your account, you will get the option to log out. When you click the logout option then you will come out of your profile with a session destroyed. See the below image for the same.
What is the procedure to make a simple PHP Login Form With Session?/ Login form in PHP with session and validation
You just need to follow the below step-by-step procedure to build a beautiful PHP login form.
First, you should know the software used for developing this functionality. You can use either the same or similar software for the same.
Install NetBeans IDE
Install Xampp Server
Start the Apache server and SQL server using your XAmpp.
If you don’t know how to start the Xampp server then you can go with this link.
Now open PHP MyAdmin create a database name as a test and then create a table login. You can also use the below script to create the database.
Now insert one record as username and password in the database so that it can be used while you will make a login into your form.
Open the NetBeans IDE and create a project named Login _Form, though you can keep any other name as per your wish.
Now create a few files as shown in the below image in the source folder of your project.
Once you create all the files then copy and paste the code one by one into the respective files which are given below.
index.php:
[code php] <?PHP include('login.PHP); // Here we are including login script if(ISSET($_SESSION['login_user'])){ header("location: myprofile.php"); } ?> <!DOCTYPE html> <html> <head> <title>Login Form in PHP with Session</title> <link href="style.css" rel="stylesheet" type="text/CSS"> </head> <body> <div id="main"> <h1>PHP Login Session Example</h1> <div id="login"> <h2>Login Form</h2> <form action="" method="post"> <label>UserName :</label> <input id="name" name="username" placeholder="username" type="text"> <label>Password :</label> <input id="password" name="password" placeholder="**********" type="password"> <input name="submit" type="submit" value=" Login "> <span><?PHP echo $error; ?></span> </form> </div> </div> </body> </html> [/code]
login.php: [code php] <?php session_start(); // Here we are starting a session $error=''; // This variable is to store the error message. if (ISSET($_POST['submit'])) { if (empty($_POST['username']) || empty($_POST['password'])) { $error = "Username or Password is invalid"; } else { // Here define username and password. $username=$_POST['username']; $password=$_POST['password']; // Here we are establishing Connection with Server by passing server_name, user_id and password as a parameter $connection = mysql_connect("localhost", "root", "); // This is to protect MySQL injection for security purposes. $username = strip slashes($username); $password = strip slashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); // Selecting Database $db = mysql_select_db("test", $connection); // This SQL query will fetch the registered user and will match the login user. $query = mysql_query("select * from login where password='$password' AND username='$username'", $connection); $rows = mysql_num_rows($query); if ($rows == 1) { $_SESSION['login_user']=$username; // Here we are Initializing Session header("location: myprofile.php"); // Redirection To Other Page } else { $error = "Username or Password is invalid"; } mysql_close($connection); // Closing Connection } } ?> [/code]
session.php: [code php] <?php // Establishing the server connection bypassing the server id and password $connection = mysql_connect("localhost", "root", "); // Selecting Database $db = mysql_select_db("test", $connection); session_start();// Starting Session // Storing Session $user_check=$_SESSION['login_user']; // This SQL query will fetch all the user information. $ses_sql=mysql_query("select username from login where username='$user_check'", $connection); $row = mysql_fetch_assoc($ses_sql); $login_session =$row['username']; if(!is set($login_session)){ mysql_close($connection); // Closing Connection header('Location: index.php'); // Redirection To Home Page } ?> [/code]
myprofile.php: [code php] <?php include('session.PHP'); ?> <!DOCTYPE html> <html> <head> <title>Your Home Page</title> <link href="style.css" rel="stylesheet" type="text/CSS"> </head> <body> <div id="profile"> <b id="welcome">Welcome : <i><?PHP echo $login_session; ?></i></b> <b id="logout"><a href="logout.php">Log Out</a></b> </div> </body> </html> [/code]
logout.php: [code php] <?php session_start(); if(session_destroy()) // Here we are destroying all the sessions. { header("Location: index.php"); // Redirecting To Home Page } ?> [/code]
style.css: [code css] @import URL('https://fonts.googleapis.com/css?family=Spectral'); /*---------------------------------------------- CSS Settings For HTML Div ExactCenter ------------------------------------------------*/ #main { width:960px; margin:50px auto; font-family: 'Spectral', serif; } span { color: red } h2 { background-color:#e4e3ea; text-align: center; border-radius:10px 10px 0 0; margin:-10px -40px; padding:15px; color: Rebecca purple; } hr { border:0; border-bottom:1px solid #ccc; margin:10px -40px; margin-bottom:30px } #login { width:300px; float: left; border-radius:10px; font-family: railway; border:2px solid #ccc; padding:10px 40px 25px; margin-top:70px; background-color: #d7d6dc; } input[type=text],input[type=password] { width:99.5%; padding:10px; margin-top:8px; border:1px solid #ccc; padding-left:5px; font-size:16px; font-family: 'Spectral', serif; } input[type=submit] { width:100%; background-color:#999aca; color:#fff; border:2px solid #a7a6d2; padding:10px; font-size:20px; cursor: pointer; border-radius:5px; margin-bottom:15px } #profile { padding:50px; border:1px dashed grey; font-size:20px; background-color:#DCE6F7 } #logout { float: right; padding:5px; border: dashed 1px gray } a { text-decoration: none; color:#6495ed } I { color:#6495ed } [/code]
You can also go through a few related blog links:
User Registration Form Using PHP & HTML…
PHP Email Verification Script For Downloading E-Book…
Login Form: Simple PHP Login Form With Session…
How to Show Progress Bar On Form Submission Using JQuery…
How to install XAMPP Server on your local computer…
Conclusion: PHP Login Form With Session
In this blog post(Simple PHP Login Form With Session Using PHP), we have learned about simple registration and login forms in PHP and MYSQL. Here you get a login page in PHP with database source code. You have learned a detailed study on the login system PHP source code. This blog also provides a complete login system PHP MYSQL code which can be tried at your end with some customization. You can see how to manage sessions in PHP example for login and logout.
Within this blog(Simple PHP Login Form With Session Using PHP), we will explore Session in PHP example for login and logout, PHP login session with the database, login, and logout using session in PHP and MySQL, How to create a login page in PHP, and MySQL with the session, Registration, and login form in PHP and MySQL, How to pass data from one page to another in PHP session? What does the session start to do in PHP?
In case of any queries, you can write to us at a5theorys@gmail.com we will get back to you ASAP.
Hope! you would have enjoyed this post-Simple PHP Login Form With Session Using PHP.
Please feel free to give your important feedback in the comment section below|Simple PHP Login Form With Session Using PHP|
Have a great time! Sayonara!