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">
<form name="userlogin" method="post" action="verify-login.php">
<table width="100%" border="0" cellpadding="10" cellspacing="5" bgcolor="#099AE3">
<td colspan="10"><strong>Registered User Login</strong></td>
<td width="100">Username</td>
<td width="300"><input name="username" type="text" id="username"></td>
<td><input name="password" type="text" id="password"></td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>

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

// some code


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

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 '',

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
$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")
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

$sql="SELECT * FROM $table_name WHERE username='$username' and password='$password'";

// Mysql_num_row is counting table row
// If result matched $username and $password, table row must be 1 row

// Register $username, $password and redirect to file "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

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

7. Create the file by using the below code and name it as logout.php

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

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 2011   Privacy Policy  Terms of Service  Feedback