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:


<html>

<head>

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


</head>



<body>

<h1>My Web Site</h1>

<h2>User Registration</h2>

<?php 

if(isset($_POST['register']))


{

    
$name=$_POST['name'];


    
$uname=$_POST['uname'];


    
$pass1=$_POST['pass1'];


    
$pass2=$_POST['pass2'];


    
$email=$_POST['email'];


    

    
//chek if all the informations are entered or not


    
if($name!='' && $uname!='' && $pass1!='' && $pass2!='' && $email!='')


    {
//if properly filled


        //check if both passwords match


        
if($pass1==$pass2)


        {

            
//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


            
if(!$db->select_db('one'))


            
//create the database


            
$db->query('create database one');


        

            
//select the databasw to work with


            
$db->select_db('one');


            

            
//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'");


            if(
$result->num_rows>0)


                echo 
"<p style=\"color: #0000ff\"><i>$uname</i> already exists. Please choose a different UserName.</p>";


            else


            {

                
$db->query("insert into user (name, uname, email, pass) values ('$name', '$uname', '$email', '$pass1')");


                echo 
"<p style=\"color: #0000ff\">Registered Sucessfully!</p>";


                
//don't show form on successful completion


                
exit;


            }

        }

        
//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


$db->close();



//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">


    <tr> 

      <td width="133">Name</td>


      <td width="327"><input name="name" type="text" id="name" value=""></td>


    </tr>

    <tr> 

      <td>UserName</td>

      <td><input name="uname" type="text" id="uname" value=""></td>


    </tr>

    <tr> 

      <td>E-Mail Address</td>


      <td><input name="email" type="text" id="email" /></td>


    </tr>

    <tr> 

      <td>Password</td>

      <td><input name="pass1" type="password" id="pass1" /></td>


    </tr>

    <tr> 

      <td>Re-Type Password</td>


      <td><input name="pass2" type="password" id="pass2" /></td>


    </tr>

    <tr> 

      <td><input name="register" type="submit" id="register" value="Register" /></td>


      <td><div align="right"> 


          <input type="reset" name="Submit2" value="Reset" />


        </div></td>

    </tr>

  </table>

</form>

</body>

</html>


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


$user='one';


$pass='123';


...

...

...

//match username & password

if(
$username==$user && $password==$pass)


{


which can be changed as following:


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


if(
$result->num_rows>0


{

    
$pass=$result->fetch_row();


    
$pass=$pass[0];


    if(
$password=$pass)


    {


Previous Articles:


Check out this stream