Skip to content

Ebhor.com

Knowledge Never Sets

  • Web Technologies
    • HTML
    • CSS
    • JavaScript
    • JQuery
    • Bootstrap
  • PHP
  • Java Technologies
    • Java Basics
    • Java Applets
    • Java Threadnig
    • Java IO
    • Jsp Servlet
    • Struts1
    • Struts2
    • Spring
    • Hibernate
  • Database
    • MySql
  • Basic Programming
    • C Programming
    • C++ Programming
  • Unix
  • Mathematics
  • Tools
    • Hash Generator
    • Password Generator

PHP Session Example

Previous post was about PHP Session There explained about session its purpose and syntax in detail. Lets see one good example of session.

User login system
1. connection.php
This page is used to establish database connection from application.

1
2
3
<?php
    $con = new mysqli("localhost","root","","session");
?>

2. index.php
This page retrives the user detail from database

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
    include "header.php";
    include "connection.php";
if($_POST[em])
{
$sql = "select * from `user` where `email`='$_POST[em]' and `password`='$_POST[pw]'";
$res = $con->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";
}
 
}
?>
<br>
<center>
<form method="post" action="">
<table border="1">
<tr>
<td>Email-</td>
<td><input type="text" name="em"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pw"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
<a href="registration.php">New User</a>
</center>

3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
    include "connection.php";
if($_POST[em])
{
$sql = "INSERT INTO `user`(`id`, `name`, `address`, `phone`, `email`, `password`) VALUES ('','$_POST[nm]','$_POST[add]','$_POST[ph]','$_POST[em]','$_POST[pw]')";
$con->query($sql);
header("location:index.php");
}
?>
<center>
<form method="post" action="">
<table border="1">
<tr>
<td>Name-</td>
<td><input type="text" name="nm"></td>
</tr>
<tr>
<td>Address-</td>
<td><textarea name="add" rows="" cols=""></textarea></td>
</tr>
<tr>
<td>Phone-</td>
<td><input type="number" name="ph"></td>
</tr>
<tr>
<td>Email-</td>
<td><input type="text" name="em"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pw"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="signin"></td>
</tr>
</table>
</form>
<a href="index.php">Already Register</a>
</center>

4. checking.php
Check whether the user is in session or not

1
2
3
4
5
6
7
<?php
    session_start();
if($_SESSION[chk]!=true)
{
header("location:index.php");
}
?>

5. profile.php
This page displays the user information to page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
    include "header.php";
    include "connection.php";
    include "checking.php";
$sql = "select * from `user` where `id`='$_SESSION[userid]'";
$res = $con->query($sql);
$row = $res->fetch_array();
echo "<table>
      <tr>
  <td>$row[name]</td>
  <td>$row[address]</td>
  <td>$row[phone]</td>
  <td>$row[email]</td>
  </tr>
  </table>";
echo "<br>
      <a href='change.php'>Change Password</a>";
?>

6. header.php
This page starts the session and show link login or logout bases on user session created or not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
session_start();
?>
<center>
<?php
if($_SESSION[chk] != true){
echo '<a href="index.php">Login</a>';
}
else{
echo '<a href="logout.php">Logout</a>';
}
?>
&nbsp;
<a href="profile.php">Profile</a>
</center>

7.logout.php
This page is used to destroy the session

1
2
3
4
5
<?php
    session_start();
session_destroy();
header("location:index.php");
?>

8. change.php
This page is used to change the user password

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
    session_start();
    include "connection.php";
if($_POST[np])
{
$sql = "select * from `user` where `password`='$_POST[op]' and `id`='$_SESSION[userid]'";
$res = $con->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";
}
}
?>
<center>
<form method="post" action="">
<table border="1">
<tr>
<td>Current Password-</td>
<td><input type="password" name="op"></td>
</tr>
<tr>
<td>New Password-</td>
<td><input type="password" name="np"></td>
</tr>
<tr>
<td>Confirm Password-</td>
<td><input type="password" name="cp"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
<a href="profile.php">Profile</a>
</center>

Categories php Tags login example, php, session
Post navigation
PHP Session
How to Send Email using PHP mail() Function
  • PHP Introduction
  • PHP Comments
  • PHP Variables
  • PHP Echo and print
  • PHP Data Types
  • PHP Operators
  • PHP if else
  • PHP Switch Case
  • PHP for loop
  • PHP While loop
  • PHP Do While loop
  • PHP Form Handling
  • PHP String
  • PHP String Functions
  • PHP Array
  • PHP Array Types
  • Functions in PHP
  • PHP Date and Time
  • PHP Email Sending
  • PHP File Upload Example
  • PHP Session
  • PHP Session Example
  • PHP Cookies
  • PHP File Handing
  • PHP Directories
  • PHP Constants
  • PHP Class and Objects
  • PHP Constructor and Destructor
  • PHP Inheritance
  • PHP Final Class
  • PHP MySQL
  • PHP PDO
  • PHP Polymorphism
  • PHP Regular Expressions
  • How to use CKEditor in PHP Installation and Uses
  • PHP Creating Image
  • About Us
  • Contact Us
  • Thanks to
  • Careers
  • Disclaimer
  • Careers
  • Disclaimer
  • Log In
  • Register
© 2021 Ebhor.com • Built with GeneratePress