How to Create registered User Login for Website in PHP & MySQL

How to Create Registered User Login Page with Session

The below example is the simple walk through to let you understand how to create registered user login page with session. In order to complete the task follow the below instructions
Place all the below files in to the same folder
1. Create a connection to MySQL database
2. Create a database
3. Create a database table
4. create a login page
5. create verify login page
6. Create a login welcome page with session start
7. Create a logout page

1. Create file by using this below code and name it login.php. This file is used by the registered users to log into your website

<table width="400" border="3" align="center" cellspacing="10" bgcolor="#18C93C">
<tr>
<form name="userlogin" method="post" action="verify-login.php">
<td>
<table width="100%" border="0" cellpadding="10" cellspacing="5" bgcolor="#099AE3">
<tr>
<td colspan="10"><strong>Registered User Login</strong></td>
</tr>
<tr>
<td width="100">Username</td>
<td width="300"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

2. Create a DB and name it logindemo by using the below instruction
Connect to MySql database by using this below code
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

// some code

mysql_close($con);
?>

3. Execute the below Mysql query to create a new database
CREATE DATABASE logindemo

4. Create Mysql Table by using the below code
CREATE TABLE `usernames` (
`id` int(5) NOT NULL auto_increment,
`username` varchar(50) NOT NULL default '',
`password` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;

INSERT INTO `usernames` VALUES (1, 'williams', 'password');

5. Use this below code to create a file to check the database to confirm whether the username and password combination present in the database and name it as verify-login.php
<?php
$host="localhost"; // Your host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="logindemo"; // Database name
$table_name="usernames"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")
or
die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// The below username and password details about to received from the login form
$username=$_POST['username'];
$password=$_POST['password'];

$sql="SELECT * FROM $table_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row

if($count==1){
// Register $username, $password and redirect to file "login-welcome.php"
session_register("username");
session_register("password");
header("location:login-welcome.php");
}
else {
echo "Username or Password is wrong. Please try Again";
}
?>

6. Create the file by using the below details and name it as login-welcome.php
<?
session_start();
if(!session_is_registered(username)){
header("location:main_login.php");
}
?>

<html>
<body>
<h1>You have successfully Logged in!</h1>
</body>
</html>

7. Create the file by using the below code and name it as logout.php
<?
session_start();
session_destroy();
?>

<html>
<body>
<h1>You have successfully Logged Out</h1>
<p>Thank You</p>
</body>
</html>


Related Topic PHP Mysql New User Registration Code Example
Remote Mysql Server SSH Login
Pagination in JSP Page Example Code
How to Add Facebook Layout or Wallpapers to your Profile
SSH Secured Shell Configurations and Security Best Practices

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback