How to Create Login and Logout System in PHP MySQL
In this post we are going to teach you How to create login and logout system in PHP MySQL. Login and Logout system is the most imortant thing for the user management, session management is one important thing for manage the user for whole time of login. We have to fetch same value for the user and check the session and Register the session value and make destroy for logout.
How to Create Login and Logout System in PHP MySQL
1-Creating Database
- Open Phpmyadmin in your Browser
- Click on Database Tab Display on Top side
- Give the Database name “login”.
- After Creating Database Open it.
- Click on SQL Tab on Top area
- Copy the Below Source Code and paste it.
- Then Click on Go.
-- phpMyAdmin SQL Dump -- version 4.2.7.1 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: May 02, 2016 at 09:12 AM -- Server version: 5.6.20 -- PHP Version: 5.5.15 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `login` -- -- -------------------------------------------------------- -- -- Table structure for table `users` -- CREATE TABLE IF NOT EXISTS `users` ( `user_id` int(11) NOT NULL, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `name` varchar(50) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; -- -- Dumping data for table `users` -- INSERT INTO `users` (`user_id`, `username`, `password`, `name`) VALUES (2, 'admin', 'admin', 'admin'), (4, 'Deepak', 'deepak', 'Deepak'); -- -- Indexes for dumped tables -- -- -- Indexes for table `users` -- ALTER TABLE `users` ADD PRIMARY KEY (`user_id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `users` -- ALTER TABLE `users` MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
OR Import DB File
After Downloading the source code extract it in your root folder.
- Open Phpmyadmin in your Browser
- Click on Database Tab Display on Top side
- Give the Database name “login”.
- After Creating Database Open it.
- Click on Import Tab on Top area
- You can Find Db file in Downloaded source code Select it.
- Then Click on Go.
2- Creating Database Connection
After import Database File then next step is creating database connection using php copy the below code and save it is as “db.php”.
<?php $con = mysqli_connect("localhost","root","","login"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?>
3 – Creating Login Form using HTML
in this step we are going to create a login Form using HTML.
<form action="#" method="post"> <h3>Login</h3> <div class="form-item"> <input type="text" name="user" required="required" placeholder="Username" autofocus required></input> </div> <div class="form-item"> <input type="password" name="pass" required="required" placeholder="Password" required></input> </div> <div class="button-panel"> <input type="submit" class="button" title="Log In" name="login" value="Login"></input> </div> </form>
4 – Styling the Login Form using CSS
in this step we are going to create a file named “style.css” and in this file we are make form attractive using css.
body { color: #fff; font: 87.5%/1.5em 'Open Sans', sans-serif; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;} .form-wrapper { width:300px; height:370px; position: absolute; top: 50%; left: 48%; margin: -184px 0px 0px -155px; background: pink; padding: 15px 25px; border-radius: 5px; box-shadow: 0px 1px 0px rgba(0,0,0,0.6),inset 0px 1px 0px rgba(255,255,255,0.04); } .form-item { width:100%; } h3{ font-size: 25px; font-weight: 640; margin-bottom: 10px; color: white; padding:6px; font-family:'sans-serif','helvetica'; } .form-item input{ border: none; background:transparent; color: #fff; margin-top:11px; font-family: 'Open Sans', sans-serif; font-size: 1.2em; height: 49px; padding: 0 16px; width: 100%; padding-left: 55px; } input[type='password']{ background: transparent url("img/pass.jpg") no-repeat; background-position: 10px 50%; } input[type='text']{ background: transparent url("img/user.jpg") no-repeat; background-position: 10px 50%; } .form-item input:focus { outline: none; border:1.4px solid #cfecf0; } .button-panel { margin-bottom: 20px; padding-top: 10px; width: 100%; } .button-panel .button { background: #00b6df; border: none; color: #fff; cursor: pointer; height: 50px; font-family: 'helvetica','Open Sans', sans-serif; font-size: 1.2em; text-align: center; text-transform: uppercase; transition: background 0.6s linear; width: 100%; margin-top:10px; } .button:hover { background: #6eb7cb; } .form-item input, .button-panel .button { border-radius: 2px }
5 -Checking The Database For Valid username and password using PHP
in this step we are going to create PHP script for checking the database for valid username OR password.
<?php if (isset($_POST['login'])) { $username = mysqli_real_escape_string($con, $_POST['user']); $password = mysqli_real_escape_string($con, $_POST['pass']); $query = mysqli_query($con, "SELECT * FROM users WHERE password='$password' and username='$username'"); $row = mysqli_fetch_array($query); $num_row = mysqli_num_rows($query); if ($num_row > 0) { $_SESSION['user_id']=$row['user_id']; header('location:home.php'); } else { echo 'Incorrect Username OR Password'; } } ?>
6 -Creating Myaccount Page
In this step creating a page named “home.php” if a user login then this page will appear.
<?php include('dbcon.php'); include('session.php'); $result=mysqli_query($con, "select * from users where user_id='$session_id'")or die('Error In Session'); $row=mysqli_fetch_array($result); ?> <html> <head> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="form-wrapper"> <center><h3>Welcome: <?php echo $row['name']; ?> </h3></center> <div class="reminder"> <img src="img/php.jpg"> <div class="button-panel"> <a href="logout.php"><input type="submit" class="button" title="Log out" value="Logout"></input></a> </div> </div> </div> </body> </html>
7 – Creating Session For user using PHP
in this step we are creating a session for Check whether the session variable SESS_MEMBER_ID is present or not.
<?php //Start session session_start(); //Check whether the session variable SESS_MEMBER_ID is present or not if (!isset($_SESSION['user_id']) || (trim($_SESSION['user_id']) == '')) { header("location: index.php"); exit(); } $session_id=$_SESSION['user_id']; ?>
8 – Creating Logout Function using PHP
after login we need to logout and in this step we are creating a logout function using PHP to Destroy session , Expire the cookies and redirect to login Form.
<?php session_start(); session_destroy(); header('location:index.php'); ?>
Login Details –
user – admin
pass – admin
If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.
Thanks
What’s up friends, its enormous article regarding teachingand entirely explained,
keep it up all the time.