Simple PHP Project:
In this article I explained a simple PHP Project, in this project
I done registration, login,
update profile, delete profile, and logout operations.
I posted code here clearly and I drawn a flow chart for clear
understanding.
Project
flowchart:
Database file:
|
Database
connection PHP file:
<?php
$con= mysql_connect("localhost","user","12345");
/*mysql_connect(servername,username,password);*/
if(!$con){
die ("could not
connect". mysql_connect());
}
$db= mysql_select_db('narendar');
if(!$db){
die("Database is
unavailable".mysql_error());
}
?>
Registration
HTML file:
In this
registration form I added client side validation and I taken 9 fields (name,
email id, password, reenter password, gender, phone number, security question,
and security answer).
<form action="register.php"
method="post">
<table>
<tr><td>Name :</td><td><input
type="text"
name="na"
required/></td></tr>
<tr><td>Email id
:</td><td><input
type="email"
name="email"pattern="*@*.+"
required/></td></tr>
<tr><td>Password
:</td><td><input
type="password"
name="psw"pattern="[A-Za-z0-9!@#$%^&*()_]{8,30}"onchange="this.setCustomValidity(this.validity.patternMismatch
? 'Password contain at least 8 charecters':'');if(this.checkValidity())form.cpsw.pattern=this.value"required/></td></tr>
<tr><td>Reenter
Password :</td><td><input type="password"
name="cpsw"onchange="this.setCustomValidity(this.validity.patternMismatch
? 'Please enter same password as
above':'');"required/></td></tr>
<tr><td>Gender :</td><td><input
type="radio"
name="gender"
value="male"
required/>Male
<input type="radio"
name="gender"
value="female"
required/>Female</td></tr>
<tr><td>Birth
Date :</td><td><input type="date"
name="date"required/></td></tr>
<tr><td>Phone
Number :</td><td><input type="tel"
name="phno"pattern="[0-9]{10}"required /></td></tr>
<tr><td>Security
Question :</td><td>
<select
name="sq"required>
<option
value="">Select
question</option>
<option
value="who
is your best friend">Who is your best friend</option>
<option value="what
is your favorite color">What is your favorite color</option>
<option
value="what
you like most in this world">What you
like most in this world</option>
</select>
</td></tr>
<tr><td>Security
Answer :</td><td><input type="text"
name="sa"required/></td></tr>
<tr><td></td><td><input
type="checkbox"
name="tc"
value="ON"required />Accepted
Terms and Conditions</td></tr>
<tr><td></td><td><input
type="submit"
value="Signup"
name="Signup"
/></td></tr>
</table>
</form>
Registration
PHP file:
<?php
include("database_connection.php");
session_start();
$r="insert
into user
(name,uid,password,gender,dob,ph_number,security_q,security_a)values('$_POST[na]','$_POST[email]',
'$_POST[psw]','$_POST[gender]','$_POST[date]','$_POST[phno]','$_POST[sq]','$_POST[sa]')";
if(!mysql_query($r,$con))
{
die('error:'.mysql_error());
}
else{
echo "successfully
registred";
header("location:login.php");
}
?>
If
registration is successful it will for login page
Login
HTML file:
<form action="login_ser.php"
method="post">
<table>
<tr><td>Email id
:</td><td><input
type="email"
name="email"pattern="*@*.+"
required/></td></tr>
<tr><td>Password
:</td><td><input
type="password"
name="psw"pattern="[A-Za-z0-9!@#$%^&*()_]{8,30}"required/></td></tr>
<tr><td></td><td><input
type="submit"
value="Singin"
name="Signin"
/></td></tr>
</table>
</form>
Login
PHP file:
<?php
// put your
code here
include "database_connection.php";
session_start();
$email=$_POST["email"];
$psw=$_POST["psw"];
$r=mysql_query("select * from user where
uid="$email" and password="$psw"");
while($row = mysql_fetch_array($r)){
$dbuid=$row["uid"];
$dbpsw=$row["password"];
$user_name=$row["name"];
if($dbuid==$email&&$dbpsw==$psw){
session_register("dbuid");
$_SESSION["login_user"]=$dbuid;
header("location:home.php");
}
else{
echo "incorrect user id and password";
header("location:login.php");
}
}
?>
In this
login PHP file create the session and get that session id into Session PHP
file. in this session php file check that login user is available or not,
and get values of login user from the
database.
Session
PHP file:
<?php
// put your
code here
session_start();
$se_uid=$_SESSION["login_user"];
include
"database_connection.php";
$sql= mysql_query("select * from user where
uid="$se_uid"");
$row = mysql_fetch_array($sql);
$login_name=$row["name"];
$l_uid=$row["uid"];
$l_dob=$row["dob"];
$l_phno=$row["ph_number"];
?>
If login
is successful it will go for Home page
Welcome
Home PHP:
<?php
// put your
code here
include "session.php";
?>
<h3>Welcome <?echo $login_name;?></h3>
<nav>
<a href="home.php">Home</a> |
<a href="update.php">Update
Profile</a> |
<a href="delete.php">Delete
Account</a> |
<a href="logout.php">Logout </a>
</nav>
Update
HTML file:
<?php
// put your
code here
include "session.php";
?>
<form action="update_ser.php"method="post">
<table>
<tr><td>Email id
:</td><td><?echo $l_uid;?></td></tr>
<tr><td>Name :</td><td><input
type="text"
name="na"
value="<?echo $login_name;?>"required/></td></tr>
<tr><td>Birth
Date :</td><td><input type="date"
name="date"value="<?echo $l_dob;?>"required/></td></tr>
<tr><td>Phone
Number :</td><td><input type="tel"
name="phno"pattern="[0-9]{10}"value="<?echo $l_phno;?>"required /></td></tr>
<tr><td></td><td><input
type="submit"
value="Update"
name="Update"
/></td></tr>
</table>
</form>
Update
PHP file:
<?php
// put your
code here
include "session.php";
$u_name=$_POST["na"];
$u_phno=$_POST["phno"];
$u_date=$_POST["date"];
$sql="update user set name="$u_name",dob="$u_date",ph_number="$u_phno"where
uid="$l_uid"";
if(!mysql_query($sql)){
die("error".mysql_error());
}
else{
header("location:home.php");
}
?>
Delete
PHP file:
<?php
// put your
code here
include "session.php";
$sql= mysql_query("delete from user where
uid="$se_uid"");
echo "your account deleted";
?>
<a href="index.php">Click
here to register again</a>
Logout
PHP file:
<?php
session_start();
if(session_destroy())
{
header("Location:
login.php");
}
?>
Screen shorts: