PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

(Login logout script) PHP Database Driven Websites – Tutorial 2a

August29

Download

Previously we had build a database structure for our website and now we will develop a CMS for the same.

Lets give our website a name, we will call it egames. egames offers various types of free games.

Website: egames

  • products: video games
  • categories:  adventure, fighting, fun, puzzles and racing

Content Management System (CMS)

  1. Username and password or login id will be required to access the CMS.
  2. CMS will add delete modify and view the “products” and “categories”.

I do not want you to waste time by building an html structure, so I have already created one and you can download it from this webpage. Extract the file and rename the folder “start” to “egames” and copy it to c:/wwwroot/inetpub.

Note:

  • The start folder comes with javascript validation built in with the php pages
  • If you face any problems please check the config.php file

Login Script

Open the file egames/admin/index.php. Here the adminuser enters the username and password to gain access. The username is “admin” and password is “password” which is stored in the table “adminuser” in your egames database which you had created in the previous tutorial. Now our script will do the following:

  • if the user enters correct username and password he is taken to the controlpanel.php page
  • else checklogin.php

checklogin.php

Go through index.php it consist of a html form with javascript validation. The form goes to checklogin.php so open that file and type the following at the very top of the page.

and type <? } ?> at the very bottom of the page

Explanation

Code Explanation
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: controlpanel.php");
if it does then create session variable susername and take the user to page controlpanel.php
else {
?>
<? } ?>
else ask for the username password again

that is done!!!!! hopefully……….

This is how a login script works, simple if you break it in steps…. (If I am boring you I am sorry can’t help it I lack humor :( ) try to make that up with knowledge as if……)

Now we have ensured that a user can only access our CMS with a username and password. You think so… don’t be too sure. Lets test it… if you can access any of the pages without entering the password.  Open a internet explorer type http://localhost/egames/admin/controlpanel.php you will see that the page opens even though you have not entered the password. So you see all our hard work and login script gone to waste….. Not so fast buddy!!!!

adminchecksession.php

We will fight the battle open adminchecksession.php. Type the following:

<? if(!(isset($_SESSION['susername'])))    {  ?>
<script language="JavaScript"> window.open("index.php", "_self")</script>
<? } ?>

This is so simple!…. It just checks if the $_SESSION['susername'] exists; if it does not the page goes to index.php else stays where it is………… Kool Na???

logout.php

Open logout.php and type the following

<?
session_start();
session_destroy();
header ("Location: index.php");
?>

Ok that login script is done, now we have to write scripts to add, delete, modify and view our products and categories. Guys we will do it in the next tutorial this page is getting big time full and so is my head. So if you have not still fainted click here to view the part 2 of tutorial2 (I love when my words rhyme)  ;)

posted under Development Guide

Email will not be published

Website example

Your Comment:

 

3,048 spam comments
blocked by
Akismet