How to create a simple login script in php

Jul 12, 2012, by admin

simple-login-script-in-phpInterested in creating your own forum page, blog, or even a shopping cart? A login area is the first step. Here is how to create a script for it in PHP.

Steps to create a simple login script in php

Create the login database and table. For the purposes of the code examples below, we will assume the database name to be “test” and the table name to be “members”. The database and table can be created through the control panel of your webhost. The “members” table will store the username and the passwords of all the people would be allowed access via this login script. The table should have a unique id field as the primary key, a username field, and a password field.

Example:

<form name=”form1″ method=”post” action=”checklogin.php”>

<strong>Member Login </strong>

Username: <input name=”myusername” type=”text” id=”myusername” />

Password: <input name=”mypassword” type=”password” id=”mypassword” />

<input type=”submit” name=”Submit” value=”Login” /></form>

Create the login interface. This is an html page containing a form, two text boxes for the user name and password, and a submit button. As long as you have these elements, you may design the layout however you wish.

Create the php script that will check the login input against the database. This script will connect to the database, send a query and recover the result, check to see if the username and password is correct and send you to the next page depending on the result of the comparison.

Example (connecting to the database):

<?php

$host=”localhost”; // Host name

$username=””; // username

$password=””; // password

$db_name=”test”; // Database name

$tbl_name=”members”; // Table name

 

// Replace database connect functions depending on database you are using.

mysql_connect(“$host”, “$username”, “$password”);

mysql_select_db(“$db_name”);

Example (submitting query and retrieving results):

// Replace counting function based on database you are using.

$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

Example (checking results):

if($count==1){

  // Register $myusername, $mypassword and redirect to file “login_success.php”

  session_register(“myusername”);

  session_register(“mypassword”);

  header(“location:login_success.php”);

} else {

  echo “Wrong Username or Password”;

}

Example (direct user based on result):

Create the page that will display after login success. This script will start your session and display an html message of your choosing.

Example:

// Check if session is not registered , redirect back to main page.

<?

session_start();

 

if(!session_is_registered(myusername)){

  header(“location:main_login.php”);

}

?>

 

<html>

<body>

Login Successful

</body>

</html>

Create logout script. This step is optional as sessions can time out. If you wish to create one, you will need a button or link to initiate the script and a message to confirm the success of logging out.

Example:

<?

session_start();

session_destroy();

?>