Wednesday, May 14, 2008

Designing a User-Registration Script in PHP

Designing a User-Registration Script in PHP

Having enough knowledge of Storing and Retrieving Data from MySQL Database we now can easily create a User Registration System to register new users for sites requiring user authorization. We’ve previously created many scripts and applications that require user authorization but none of them can register new users by themselves. We had to hard code the user data into the scripts. For any real application though, that won’t work and we require a “New User Registration’ system. That’s what we are going tot create in this post.

Registering new user is just a matter of storing username, password and optionally other information about the user in a database. As we have discussed Storing and Retrieving Data from MySQL Database, so we straightaway look at the code listing:

<title>My Web Site | User Registration</title>

<h1>My Web Site</h1>
<h2>User Registration</h2>
//chek if all the informations are entered or not
if($name!='' && $uname!='' && $pass1!='' && $pass2!='' && $email!='')
//if properly filled
        //check if both passwords match
//connect to MySQL 
            //provide your 'USERNAME' and 'PASSWORD' 
            //change 'localhost' to the MySQL server 
            //host, if not using on 'local server' 
$db=new mysqli('localhost','USERNAME','PASSWORD');

//if this is the first time
            //and database is not craeted
//create the database
$db->query('create database one');
//select the databasw to work with
//if table is not created, create it
if(!$db->query('select * from user'))
$db->query('create table user (userid int auto_increment primary key, name varchar(50), uname varchar(15), email varchar(50), pass varchar(15))');
//ready to insert data
            //check if username entered is available or not
$result=$db->query("select uname from user where uname='$uname'");
"<p style=\"color: #0000ff\"><i>$uname</i> already exists. Please choose a different UserName.</p>";
$db->query("insert into user (name, uname, email, pass) values ('$name', '$uname', '$email', '$pass1')");
"<p style=\"color: #0000ff\">Registered Sucessfully!</p>";
//don't show form on successful completion
//if passwords don't match
else echo "<p style=\"color: #ff0000\">Both passwords do not match! Please re-enter and register.</p>";
//if not poperly filled
else echo "<p style=\"color: #ff0000\">You haven't filled all the information. Please Re-Enter.</p>";

//close MySQL connection

//show the form
<code><span style="color: #000000"><span style="color: #007700"> </span></span></code> 
<form name="form1" id="form1" method="post" action="">
  <table width="500" border="0" cellspacing="0" cellpadding="10">
      <td width="133">Name</td>
      <td width="327"><input name="name" type="text" id="name" value=""></td>
      <td><input name="uname" type="text" id="uname" value=""></td>
      <td>E-Mail Address</td>
      <td><input name="email" type="text" id="email" /></td>
      <td><input name="pass1" type="password" id="pass1" /></td>
      <td>Re-Type Password</td>
      <td><input name="pass2" type="password" id="pass2" /></td>
      <td><input name="register" type="submit" id="register" value="Register" /></td>
      <td><div align="right"> 
          <input type="reset" name="Submit2" value="Reset" />

We’d easily be able to integrate this to our existing ‘user authorization‘ sites that we had created. We would just have to query the database to match username and password rather than with variables or constants.

These are the lines in the ‘login’ page that match username and password:

//user data

//match username & password
$username==$user && $password==$pass)

which can be changed as following:

$result=$db->query("select pass from user where uname='$username'");

Previous Articles:


  1. Anonymous4:31 PM

    Handy Script, and nice Blog! Very Useful

  2. Hi Nathan,

    Thanks very much!


You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.