User login and login tracking using PHP
In this tutorial I will explain user login system and how to track user accesslog. In user log we can see how to track user ip and user login and logout time.
Files Included for this system
config.php
index.php
welcome.php
userlog.php
logout.php
Create a Database with name demos. Demos database contain two tables :
login
userlog
Structure of the login table
1 2 3 4 5 |
CREATE TABLE `login` ( `id` int(11) NOT NULL, `userName` varchar(255) NOT NULL, `password` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Structure of userlog table
1 2 3 4 5 6 7 |
CREATE TABLE `userlog` ( `id` int(11) NOT NULL, `userId` int(11) NOT NULL, `username` varchar(255) NOT NULL, `userIp` varbinary(16) NOT NULL, `loginTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Database Connection (config.php)
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php define('DB_SERVER','localhost'); define('DB_USER','root'); define('DB_PASS' ,''); define('DB_NAME', 'demos'); $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?> |
Now create an HTML form with two fields username and password
1 2 3 4 5 6 7 8 9 |
<form name="login" method="post" > <header>Login</header> <p style="color:red;"><?php echo $_SESSION['msg'];?><?php echo $_SESSION['msg']="";?></p> <label>Username <span>*</span></label> <input name="username" type="text" value="" required /> <label>Password <span>*</span></label> <input name="password" type="password" value="" required /> <button type="submit" name="login">Login</button> </form> |
Create PHP script for user login and login tacking put this script above the index.php page.
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 |
<?php session_start(); include 'config.php'; if(isset($_POST['login'])) { $username=$_POST['username']; // Get username $password=$_POST['password']; // get password //query for match the user inputs $ret=mysqli_query($con,"SELECT * FROM login WHERE userName='$username' and password='$password'"); $num=mysqli_fetch_array($ret); // if user inputs match if condition will runn if($num>0) { $_SESSION['login']=$username; // hold the user name in session $_SESSION['id']=$num['id']; // hold the user id in session $uip=$_SERVER['REMOTE_ADDR']; // get the user ip $action="Login";// query for inser user log in to data base mysqli_query($con,"insert into userlog(userId,username,userIp,action) values('".$_SESSION['id']."','".$_SESSION['login']."','$uip','$action')") // code redirect the page after login $extra="welcome.php"; $host=$_SERVER['HTTP_HOST']; $uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\'); header("location:http://$host$uri/$extra"); exit(); } // If the userinput no matched with database else condition will run else { $_SESSION['msg']="Invalid username or password"; $extra="index.php"; $host = $_SERVER['HTTP_HOST']; $uri = rtrim(dirname($_SERVER['PHP_SELF']),'/\\'); header("location:http://$host$uri/$extra"); exit(); } } ?> |
Here the full code of index.php
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 50 51 52 53 54 55 56 |
<?php session_start(); include 'config.php'; if(isset($_POST['login'])) { $username=$_POST['username']; // Get username $password=$_POST['password']; // get password //query for match the user inputs $ret=mysqli_query($con,"SELECT * FROM login WHERE userName='$username' and password='$password'"); $num=mysqli_fetch_array($ret); // if user inputs match if condition will runn if($num>0) { $_SESSION['login']=$username; // hold the user name in session $_SESSION['id']=$num['id']; // hold the user id in session $uip=$_SERVER['REMOTE_ADDR']; // get the user ip $action="Login";// query for inser user log in to data base mysqli_query($con,"insert into userlog(userId,username,userIp,action) values('".$_SESSION['id']."','".$_SESSION['login']."','$uip','$action')") // code redirect the page after login $extra="welcome.php"; $host=$_SERVER['HTTP_HOST']; $uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\'); header("location:http://$host$uri/$extra"); exit(); } // If the userinput no matched with database else condition will run else { $_SESSION['msg']="Invalid username or password"; $extra="index.php"; $host = $_SERVER['HTTP_HOST']; $uri = rtrim(dirname($_SERVER['PHP_SELF']),'/\\'); header("location:http://$host$uri/$extra"); exit(); } } ?> <!DOCTYPE html> <html > <head> <meta charset="UTF-8"> <title>User login and tracking in PHP using PHP OOPs Concept</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <form name="login" method="post" > <header>Login</header> <p style="color:red;"><?php echo $_SESSION['msg'];?><?php echo $_SESSION['msg']="";?></p> <label>Username <span>*</span></label> <input name="username" type="text" value="" required /> <label>Password <span>*</span></label> <input name="password" type="password" value="" required /> <button type="submit" name="login">Login</button> </form> </body> </html> |
After login, you will redirect to welcome.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php session_start(); if($_SESSION['login']) { ?><!DOCTYPE html> <html > <head> <meta charset="UTF-8"> <title>welcome</title> </head> <body bgcolor="#d6c2c2"> <p>Welcome : <?php echo $_SESSION['login'];?> | <a href="logout.php">Logout</a> </p> <p><a href="userlog.php">User Access log</a></p> </body> </html> <?php } else{ header('location:logout.php'); } ?> |
After login, you can see your login details by click on User Access log(userlog.php)
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 |
<?php session_start(); include('config.php'); if($_SESSION['login']) { ?><!DOCTYPE html> <html > <head> <meta charset="UTF-8"> <title>welcome</title> </head> <body bgcolor="#d6c2c2"> <p><a href="welcome.php">Welcome : <?php echo $_SESSION['login'];?> </a>| <a href="logout.php">Logout</a> </p> <table align="center" border="1"> <tr> <th>Sno.</th> <th>User Id</th> <th>User Name</th> <th>User Ip</th> <th>Action Performed</th> <th>Login Time</th> </tr> <?php $query=mysqli_query($con,"select * from userLog where userId='".$_SESSION['id']."'"); $cnt=1; while($row=mysqli_fetch_array($query)) { ?> <tr> <td><?php echo $cnt;?></td> <td><?php echo $row['userId'];?></td> <td><?php echo $row['username'];?></td> <td><?php echo $row['userIp'];?></td> <td><?php echo $row['action'];?></td> <td><?php echo $row['actionTime'];?></td> </tr> <?php $cnt=$cnt+1; } ?> </table> </body> </html> <?php } else{ header('location:logout.php'); } ?> |
Logout script (logout.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php session_start(); include('config.php'); // Getting logout time in db $username=$_SESSION['login']; // hold the user name in session $uid=$_SESSION['id']; // hold the user id in session $uip=$_SERVER['REMOTE_ADDR']; // get the user ip $action="Logout"; // query for inser user log in to data base $query=mysqli_query($con,"insert into userlog(userId,username,userIp,action) values('$uid','$username','$uip','$action')"); if($query){ session_unset(); //session_destroy(); } $_SESSION['msg']="You have logged out successfully..!"; ?> <script language="javascript"> document.location="index.php"; </script> |
CSS (style.css)
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
html, body { border: 0; padding: 0; margin: 0; height: 100%; } body { background: tomato; display: flex; justify-content: center; align-items: center; font-size: 16px; } form { background: white; width: 40%; box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.7); font-family: lato; position: relative; color: #333; border-radius: 10px; } form header { background: #FF3838; padding: 30px 20px; color: white; font-size: 1.2em; font-weight: 600; border-radius: 10px 10px 0 0; } form label { margin-left: 20px; display: inline-block; margin-top: 30px; margin-bottom: 5px; position: relative; } form label span { color: #FF3838; font-size: 2em; position: absolute; left: 2.3em; top: -10px; } form input { display: block; width: 78%; margin-left: 20px; padding: 5px 20px; font-size: 1em; border-radius: 3px; outline: none; border: 1px solid #ccc; } form .help { margin-left: 20px; font-size: 0.8em; color: #777; } form button { position: relative; margin-top: 30px; margin-bottom: 30px; left: 50%; transform: translate(-50%, 0); font-family: inherit; color: white; background: #FF3838; outline: none; border: none; padding: 5px 15px; font-size: 1.3em; font-weight: 400; border-radius: 3px; box-shadow: 0px 0px 10px rgba(51, 51, 51, 0.4); cursor: pointer; transition: all 0.15s ease-in-out; } form button:hover { background: #ff5252; } |