How to Create Login Page in PHP/MySQL
Creating Our Database
First we are going to create our database which stores our data. To create a database: 1. Open phpmyadmin 2. Click create table and name it. 3. Then name the database as "simple_login". 4. After creating a database name, click the SQL and paste the following code.- CREATE TABLE IF NOT EXISTS `member` (
- `mem_id` int(11) NOT NULL AUTO_INCREMENT,
- `username` varchar(30) NOT NULL,
- `password` varchar(30) NOT NULL,
- `fname` varchar(30) NOT NULL,
- `lname` varchar(30) NOT NULL,
- `address` varchar(100) NOT NULL,
- `contact` varchar(30) NOT NULL,
- `picture` varchar(100) NOT NULL,
- `gender` varchar(10) NOT NULL,
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
Creating Our Form
Next step is to create a form and save it as index.php. To create a form, open your HTML code editor and paste the code below in the upper part of the document or above the html tag. The code below is use to disallow the user to go back as if it still logged in.- <?php
- //Start session
- //Unset the variables stored in session
- ?>
- <form name="loginform" action="login_exec.php" method="post">
- <table width="309" border="0" align="center" cellpadding="2" cellspacing="5">
- <tr>
- <td colspan="2">
- <!--the code bellow is used to display the message of the input validation-->
- <?php
- echo '<ul class="err">';
- foreach($_SESSION['ERRMSG_ARR'] as $msg) {
- echo '<li>',$msg,'</li>';
- }
- echo '</ul>';
- }
- ?>
- </td>
- </tr>
- <tr>
- <td width="116"><div align="right">Username</div></td>
- <td width="177"><input name="username" type="text" /></td>
- </tr>
- <tr>
- <td><div align="right">Password</div></td>
- <td><input name="password" type="text" /></td>
- </tr>
- <tr>
- <td><div align="right"></div></td>
- <td><input name="" type="submit" value="login" /></td>
- </tr>
- </table>
- </form>
Creating our Connection
Next step is to create a database connection and save it as "connection.php". This file is used to connect our form to database.- <?php
- $mysql_hostname = "localhost";
- $mysql_user = "root";
- $mysql_password = "";
- $mysql_database = "simple_login";
- $prefix = "";
- $bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
- ?>
Writing Our Login Script
Next step is to create our login script that validates our input data and save it as login_exec.php.- <?php
- //Start session
- //Include database connection details
- require_once('connection.php');
- //Array to store validation errors
- //Validation error flag
- $errflag = false;
- //Function to sanitize values received from the form. Prevents SQL injection
- function clean($str) {
- echo "str: ".$str;
- }
- }
- //Sanitize the POST values
- $username = $_POST['username'];
- $password = $_POST['password'];
- //Input Validations
- if($username == '') {
- $errmsg_arr[] = 'Username missing';
- $errflag = true;
- }
- if($password == '') {
- $errmsg_arr[] = 'Password missing';
- $errflag = true;
- }
- //If there are input validations, redirect back to the login form
- if($errflag) {
- $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
- }
- //Create query
- $qry="SELECT * FROM member WHERE username='$username' AND password='$password'";
- //Check whether the query was successful or not
- if($result) {
- //Login Successful
- $_SESSION['SESS_MEMBER_ID'] = $member['mem_id'];
- $_SESSION['SESS_FIRST_NAME'] = $member['username'];
- $_SESSION['SESS_LAST_NAME'] = $member['password'];
- }else {
- //Login failed
- $errmsg_arr[] = 'user name and password not found';
- $errflag = true;
- if($errflag) {
- $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
- }
- }
- }else {
- }
- ?>
Creating Our Authentication File
Next step is to create our authentication file and save it as auth.php. this code is use to disallow the user to go back as if it still logged in.- <?php
- //Start session
- //Check whether the session variable SESS_MEMBER_ID is present or not
- }
- ?>
Creating Our Home page
Next step is to create a homepage and save it as home.php.- <?php
- require_once('auth.php');
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>Untitled Document</title>
- <style type="text/css">
- <!--
- .style1 {
- font-size: 36px;
- font-weight: bold;
- }
- -->
- </style>
- </head>
- <body>
- <p align="center" class="style1">Login successfully </p>
- <p align="center">This page is the home, you can put some stuff here......</p>
- <p align="center"><a href="index.php">logout</a></p>
- </body>
- </html>
Comments
user profile
I like you code
Firefox has detected that the server is redirecting the request
Help me out of this Error
Be sure to remove the white
QUERY FAILED
Change the error_reporting in
error_reporting = E_ALL & ~E_NOTICE
Session login page
Page1:login.php
<?php
session_start();
mysql_connect("localhost","root","") or die("error connect");
mysql_select_db("task2") or die(" error database");
$email=$_POST['email'];
$pass=$_POST['password'];
$login=mysql_query("SELECT * FROM login WHERE email='$email' AND password='$pass'");
$sql=mysql_num_rows($login);
if($sql!=0){
$_SESSION['email']=$email;
header('Location: login_verify.php');
}
else {
echo "Wrong Username or Password";
}
?>
page2:login_check.
<?php
session_start();
if(isset($_SESSION['email'])){
echo "<center><h1>".$_SESSION['email']."Welcome to Home page</h1></center>";
}
else{
header('Location: login_im.php');
}
?>
<a href="newlogout.php">Logout</a>
page3:logout.php
<?php
session_start();
session_destroy();
header("location:/login_im.php");
?>
Your code is very good but
table creation
Use username or member_id variable in home.php
Logout
- ';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '
- ',$msg,' '; } echo '
i dont no where to put the welcome and username
Hey great work there: but one
fetching data from database
very useful source code
about displaying some information related to user
Login System
Hello Sir,
I'm also try to develop login system.I'm new in PHP.Please send me complete login system for my reference if possible.my email-id is:[email protected] .
help me out
love the code
Add new comment
- Add new comment
- 6272 views