Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 426

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/templates/ja_purity/ja_templatetools.php on line 49
Create login page in PHP - The Scripts Library Community

Free Programming Tutorials & Source Code

 
  • Increase font size
  • Default font size
  • Decrease font size
Home PHP Create login page in PHP

Create login page in PHP

E-mail
(2 votes, average: 4.50 out of 5)

In this tutorial, I will explain step by step how to create a login page in PHP.

Create a table Users in your mySQL database.
 
CREATE TABLE `Users` (
`id` int(4) NOT NULL auto_increment,
`user_name` varchar(100) NOT NULL default '',
`password` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

For testing purpose, run the following sql query to inset a row into the database:
 INSERT INTO `Users` VALUES (1, 'feras', 'pass'); 


Create login.php page with the following HTML controls:
A form. Name it as frm, set the action property as validatelogin.php
Text Box for uesrname. Name it as user.
Text Box for password. Name it as password.
Submit Button. Name it as login

Your form should look like this:

login

Create validatelogin.php:
 
$hostname="hostname"; // hostname, use localhost for local machine
$username="dbusername"; // database username
$password="dbpassword"; // databanse password
$db_name="myDatabase"; // Database name //

//create connection to the server

mysql_connect("$hostname", "$username", "$password")or die("connecting failed");

// select the database

mysql_select_db("$db_name")or die("Error selecting database");

// username and password variable from the form ( see code above)

$username=$_POST['user'];
$password=$_POST['password'];
$sql="SELECT * FROM Users WHERE user_name='$username' and password='$password'";
$sqlresult=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($sqlresult);
// if matched, count should be 1
if($count==1){ // create username and password sessions, and redirect to login succeed
session_register("username");
session_register("password");
header("location:loginsuccess.php");
else
{ echo "Login failed"; }
ob_end_flush(); // for php5 users


Create the loginsucceed.php file:
 
session_start();
if(!session_is_registered(username)){
header("location:login.php");
  echo 'Welcome to the members area!';
}


Create logout.php page to kill the session:
 
session_start();
session_destroy();

This is a simple login page. However, it requires some additional coding to prevent sql injection, malicious behaviors and to encrypt the password in the database. You may check Prevent SQL Injection with PHP and MySql and Encryption Password using md5() function.