PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

PHP Login Script

August10

Simple php login logout script using encrypted passwords to guarantee security.

This is a simple PHP script to enable access to your website or CMS. Click here to view a similar tutorial where you can download and practice extra features like a CMS and php website code with a proper html structure.

google_protectAndRun(”ads_core.google_render_ad”, google_handleError, google_render_ad);

First you have to create a mysql DB to store the username and password

CREATE DATABASE `login` ;

CREATE TABLE IF NOT EXISTS `adminuser` (

`sno` varchar(50) NOT NULL,

`username` varchar(50) NOT NULL,

`password` varchar(50) NOT NULL,

PRIMARY KEY (`username`)

);

INSERT INTO `adminuser` (`sno`, `username`, `password`) VALUES

(’1′, ‘admin’, ‘5f4dcc3b5aa765d61d8327deb882cf99′);

We have to create three files

  • index.php
  • checklogin.php
  • index.php


google_protectAndRun(”ads_core.google_render_ad”, google_handleError, google_render_ad);

index.php

Open a new file in dreamweaver or notepad.

<form method=”post” action=”checklogin.php”>

Please enter your Username and Password:<br>

Username: <input class=ainput type=”text” name=”susername” size=”30″><br>

Password: <input class=ainput type=”password” name=”password” size=”30″><br>

<input type=”submit” value=”Submit” name=”Submit”>

</form>

save as login.php

checklogin.php

<?php

session_start();

mysql_connect(”localhost”,”root”,”"); mysql_select_db(”login”);

$susername=$_POST["susername"];

$password=md5($_POST["password"]);

$ress=mysql_query(”select * from adminuser where username=’$susername’”) or die(mysql_error());

$rows=mysql_fetch_array($ress);

if(($rows["username"]==$susername)&&($rows["password"]==$password))

{

$_SESSION['susername']=$susername;

header (”Location: homepage.php”);

}

else echo “wrong username or password”;

?>

homepage.php (The homepage of your website or the access page)

<?php

session_start();

if(!(isset($_SESSION['susername'])))

{ ?> <script language=”JavaScript”>window.open(”index.php”, “_self”)</script><? }?>

<html>

<head><title>My Homepage</title></head>

<body> WELCOME TO MY SECURE WEBSITE<br>

<a href=”logout.php”>Logout</a></body>

</html>

logout.php

<?

session_start();

session_destroy();

header (”Location: index.php”);

?>

Explanation

Code Explanation
checklogin.php
session_start() is used to start a session and has to be placed on every page sessions are used.
$susername=$_POST["susername"];

$password=$_POST["password"];

is used to extract values from index.php page
$password=md5($password); md5 is used to encrypt the password
$ress=mysql_query(”select * from adminuser where username=’$susername’”) used to connect to the mysql table adminuser
or die(mysql_error()) gives mysql error if the table does not get connected for some reason
$rows=mysql_fetch_array($ress); fetches the data from the table row by row
if(($rows["username"]==$susername)&&($rows["password"]==$password)) checks if the entered username and password matches with the table entries
$_SESSION['susername']=$susername;

header (”Location: homepage.php”);

if it does then create session variable susername and take the user to page controlpanel.php
homepage.php
if(!(isset($_SESSION['susername']))) check if the username isset
<script language=”JavaScript”>window.open(”index.php”, “_self”)</script> if not go back to the index.php page

that is done!!!!! hopefully………. I will recommend again to Click here to view a similar tutorial where you can download and practice extra features like a CMS and php website code with a proper html structure.

posted under Development Guide

Email will not be published

Website example

Your Comment:

 

3,055 spam comments
blocked by
Akismet