Simple PHP Registration and login form

Am going to write a simple php  Registration and login code that will execute in the same page containing the form.I always dont the way developers write and save their pages some as .php and others as.html.

The importance of saving your website files as .php is that you can write html code and still run php code in the same page

Here is example of a Simple PHP Registration and login  Form code

Task A:Registration Users

Step 1.Create a table and name it users.Its should have the following fields:firstname ,secondname,email and password. Email should be the  primary key so that no two users can be registered using the same email

Step2.Desing a simple HTML form.


<form action="" name="registerform" method="post" onSubmit="return validateForm()">

<table>
<tr>
<td>
<div class="control-group">
<label class="control-label" for="inputEmail0">First Name</label>
</td><td>
<input class="span3" required type="text" id="inputEmail0" placeholder="Firstname" name="firstname">

</div>

</td>
</tr>
<tr style="height:10px">
<td>
<div class="control-group">
    
</div>
</td>
</tr>
<tr>
<tr>
<td>
<div class="control-group">
<label class="control-label" for="inputEmail0">Second Name</label>

</td><td><input class="span3" required type="text" id="inputEmail0" placeholder="Second name" name="secondname">

</div>
</td>
</tr>
<tr style="height:10px">
<td>
<div class="control-group">
    
</div>
</td>
</tr>
<tr>
<td>
<div class="control-group">
<label class="control-label" for="inputEmail0">E-mail address</label>
</td><td>
<input class="span3" required type="email" id="inputEmail0" placeholder="Email" name="email">

</div>

</td>
</tr>
<tr style="height:10px">
<td>
<div class="control-group">
    
</div>
</td>
</tr>
<tr>
<tr>
<td>
<div class="control-group">
<label class="control-label" for="inputEmail0">Password</label>
</td><td>
<input class="span3" required type="password" id="inputEmail0" placeholder="Password" name="pass">

</div>
</td>
</tr>

<tr align="center">
<td></td>
<td align="center">
<input type="submit" required class="btn btn-large btn-success" value="Register" name="register" >
</td>
</tr>

</table>
</form>

 

PHP Code to Register Users


if(!empty($_POST['register']))
{
$email=$_POST['email'];
$password =$_POST['pass'];

$fname=$_POST['firstname'];
$sname=$_POST['secondname'];

$sql="INSERT INTO users(email,password,firstname,secondname)values('$email','$password','$fname','$sname')";

$result=mysql_query($sql) or die(mysql_error());

Task 2:Validate data submitted by users and grant or deny access (Login Code)

Simple login form


<form name="loginform" action="" method="post">
<table>
<tr>
<td>
<div class="control-group">
<label class="control-label" for="inputEmail1">E-mail address</label>
</td><td>

<input class="span3" required type="email" id="inputEmail1" placeholder="Email" name="email">

</div>
</td>
</tr>
<tr style="height:10px">
<td>
<div class="control-group">
    
</div>
</td>
</tr>
<tr>
<td>
<div class="control-group">
<label class="control-label" for="inputPassword1">Password</label></td><td>

<input type="password" required class="span3" id="inputPassword1" placeholder="Password" name="password">

</div>
</td>
</tr>
<tr>
<td>
</td>
<td>
<div class="control-group">
<div class="controls">

<input type="submit" required name="login" class="btn btn-large btn-success" value="Sign in">
</div>
</div>
</form>

PHp Code to Check Data submitted by the user


if(!empty($_POST['login']))
{

$email=$_POST['email'];
$password=$_POST['password'];

$sql="SELECT*FROM users WHERE email='$email' AND password ='$password'";
$result = mysql_query($sql) or die(mysql_error());
$user=mysql_fetch_array($result);

$count=mysql_num_rows($result);

if($count==1)
{

//Grant access

 

}
else
{
//Deny access
}

}

 

About the Author: Eric K

My name is Eric.Am a software developer with skills in Php,Mysql, Java , Android,Node.js Above all, I have certification in Cisco Networking Associate (CCNA) and holds degree in Information Technology. Hire me: Email : kawi3462@gmail.com