The previous post was about PHP Session There explained about session its purpose and syntax in detail. Let’s see one good example of the session.
User login system
1. connection.php
This page is used to establish a database connection from the application.
2. index.php
This page retrieves the user detail from the database
query($sql);
$num = $res->num_rows;
if($num!=0)
{
$row = $res->fetch_array();
$_SESSION[userid] = $row[id];
$_SESSION[chk] = true;
header("location:profile.php"); //redirect one PHP file to another PHP file
}
else
{
echo "Login Fail";
}
}
?>;
New User
3.
query($sql);
header("location:index.php");
}
?>
Already Register
4. checking.php
Check whether the user is in session or not
5. profile.php
This page displays the user information on the page
To select this select * from user query is used and we passed session userid that is extracted from $_SESSION[]
query($sql); $row = $res->fetch_array(); echo "
| $row[name] | $row[address] | $row[phone] | $row[email] |
"; echo " Change Password"; ?>
6. header.php
This page starts the session and shows a link login or logout based on user session was created or not
Login'; } else{ echo 'Logout'; } ?> Profile
7.logout.php
This page is used to destroy the session
8. change.php
This page is used to change the user’s password
query($sql);
$num = $res->num_rows;
if($num!=0)
{
if($_POST[np]==$_POST[cp])
{
$sql1 = "update user set password = '$_POST[np]' where id='$_SESSION[userid]'";
$con->query($sql1);
header("location:logout.php");
}
else
{
echo "Password Does not Matched";
}
}
else
{
echo "Current Password Wrong";
}
}
?>
Profile