PHP Session Example
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 system1. connection.phpThis page is used to establish a database connection from the application.
1 2 3 | <!--?php $con = new mysqli("localhost","root","","session"); ?--> |
2. index.phpThis page retrieves the user detail from the database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!--?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"; } } ?>; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <center> <p> </p> <form action="" method="post"> <table border="1"> <tbody> <tr> <td>Email-</td> <td><input name="em" type="text"></td> </tr> <tr> <td>Password</td> <td><input name="pw" type="password"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Login"></td> </tr> </tbody> </table> </form> <p>New User</p> <p> </p> </center> |
3.
1 2 3 4 5 6 7 8 9 | <!--?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"); } ?> |