This tutorial is for PHP beginner who want to code a simple registration page for their website or any PHP web application. To create a working registration form you need a database in which you will store data that will be entered in registration form. We have taken necessary precaution in our codes in order to prevent it from intruders and hackers. Its a secure registration page that you can use in your projects doing necessary modification as per your need.
In this example my database name is ‘CLASS’ and it has a table with name ‘STUDENT’. The structure of student table is as follows:
CLASS table has three columns with name ‘ID’,’NAME’ and ‘PASSWORD’. SQL command to create CLASS table is:
CREATE TABLE IF NOT EXISTS <code>class</code> ( <code>id</code> int(2) NOT NULL AUTO_INCREMENT, <code>name</code> varchar(20) NOT NULL, <code>password</code> varchar(50) NOT NULL, PRIMARY KEY (<code>id</code>) );
Now we have to code a registration form and registration.php script.Registration form can be created using html or you can code the form within registration.php. We’ll use the first method to write our code.
Code for Registration.html:
<form action="registration.php" method="post"> <label>Name :</label> <input type="text" name="name"/><br /> <label>Password :</label> <input type="password" name="password"/><br/> <input type="submit" value=" Submit "/><br /> </form>
Code for registration.php:
<?php $connection = mysql_connect("localhost","root","12345") or die("Opps some thing went wrong"); $db_select=mysql_select_db("school",$connection) or die("Opps some thing went wrong"); $name = strip_tags(substr($_POST['name'],0, 100)); $safename = mysql_escape_string($name); $password = strip_tags(substr($_POST['password'],0, 100)); $safepassword = mysql_escape_string($password); /* Securing registration page from SQL injection and XSS */ $encrypted = md5($safepassword); // Encrypting Password Using md5 algo $query=mysql_query("INSERT INTO class(<code>name</code>,<code>password</code>) VALUES('$safename','$encrypted')"); ?>
If you have any doubt regarding registration script in PHP then please feel free to ask. Share the tutorial to help beginners to learn. Also comment how you can make this code more secure and reliable.