PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

(User registration register script) Build PHP Database Driven Websites – Tutorial 6

October31

User registration script with database structure and CMS to save and delete registered users.

Previously we had build the database structure, CMS and website interface for an online egames website and now we will add user registration script.

Download file 100.rar “start” folder consist of the work done previously and “final” folder consist of the complete work done in this tutorial. Rename the folder “start” to “egames” copy paste at c:/wwwroot/inetpub/. Open internet explorer and type http://localhost/egames to access the application

The database structure

Open PHPmyadmin and type the following code to add table user

CREATE TABLE IF NOT EXISTS `user` (`id` int(11) NOT NULL auto_increment,`username` varchar(50) default NULL,`password` varchar(50) default NULL,`name` varchar(255) default NULL,`email` varchar(255) default NULL,`website` varchar(255) default NULL,PRIMARY KEY (`id`)
)

register.php

Now after the database structure is set lets make user registration form. Open register.php find <!– listing –> and type the following:

saveregister.php

Open saveregister.php and type the following:

sidebar.php

Open sidebar.php and beneath <ul> type the following

login.php

I have also explained the login script while developing the CMS . Click here to view it.

Note: add session_start(); at the beginning of every webpage

Code Explanation
saveregister.php
$username=$_POST["username"];
$password=md5($_POST["password"]); ...
Extracting values from register.php
md5() used to encrypt the password entered
$res=mysql_query("select * from user where username like '$username'") or die(mysql_error());
$tot=mysql_num_rows($res);
if($tot==0){
check if the entered username exist. If it does not then continue else stop registration
mysql_query("insert into user values(NULL, '$username','$password','$name','$email','$website')"); Inserting values in table ‘user’
sidebar.php
if(isset($_SESSION["webuser"])) { $webuser=$_SESSION["webuser"]; echo "Welcome ".$webuser;} else { check if session “webuser” exits. If yes give a welcome message else show login form
<form name="myform" action="login.php" method="post">
Username: <input type="text" name="username" size="15" /><br /> ...
login form
login.php
session_start(); login script uses sessions. To use sessions we have to declare session_start(); on every webpage.
include("config.php"); connect to the database
$username=$_POST["username"];
$password=md5($_POST["password"]);
extract values from previous form
$ress=mysql_query("select * from user where username='$username'") or die(mysql_error());
$rows=mysql_fetch_array($ress);

if(($rows["username"]==$username)&&($rows["password"]==$password))
{
$_SESSION['webuser']=$username;
header (”Location: index.php”);
}

else { header (”Location: index1.php”); }

Open table to check if username exists

Matches username and password stored in the table and entered by the user

if match found create session “webuser” and go to page index.php else go to page index1.php

CMS

View the users of the website and delete offenders. The code involved has already been explained before so I will not repeat myself again here. Open egames/admin/user.php to view the script.

posted under Development Guide

Email will not be published

Website example

Your Comment:

 

3,048 spam comments
blocked by
Akismet