User Login And Login Tracking Using PHP

User Login And Login Tracking Using PHP

config.php

								
									<?php
									define('DB_SERVER','localhost');
									define('DB_USER','root');
									define('DB_PASS' ,'');
									define('DB_NAME', 'logintracking');
									$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();
									}
									?>
								
							

index.php

								
									<?php
									session_start();
									error_reporting(0);
									include 'config.php';
									if(isset($_POST['login']))
									{
										
									    $username=$_POST['username']; // Get username
									    $password=md5($_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
									        // query for inser user log in to data base
									        mysqli_query($con,"insert into userlog(userId,username,userIp) values('".$_SESSION['id']."','".$_SESSION['login']."','$uip')");
									        // 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
									    {
									    	echo "<script>alert('Invalid username or password');</script>";

									    }
									}
									?>
									<!DOCTYPE html>
									<html >
									<head>
										<meta charset="UTF-8">
										<title>User login and tracking in PHP using PHP OOPs Concept</title>
										<meta name="viewport" content="width=device-width, initial-scale=1">
										<link rel="stylesheet" href="css/style.css">
									</head>
									<body>

										<form name="login" method="post">
											<header>Username(admin), Password(1234)</header>
											<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>
								
							

welcome.php

								
									<?php
									session_start();
									include('config.php');
									error_reporting(0);
									if($_SESSION['login'])
									{
										?>

										<!DOCTYPE html>
										<html lang="en">
										<head>
											<meta charset="utf-8">
											<meta name="robots" content="noindex, nofollow">
											<title>Login Form</title>
											<meta name="viewport" content="width=device-width, initial-scale=1">
											<link href="css/bootstrap.css" rel='stylesheet' type='text/css' />

										</head>
										<body>
											<div class="container">
												<div> </div>
												<div class="  d-flex flex-row align-items-center justify-content-between">
													<div class="">
														<a href="logout.php"><button type="button" class="btn btn-sm btn-primary"><i class="fas fa-plus" ></i>Logout
														</button></a>
													</div>
												</div>
												<div> </div>
												<div class="row ">
													<table id="" class="table table-bordered">
														<thead>
															<tr>
																<th class="text-center">No</th>
																<th>User Id</th>
																<th>User Name</th>
																<th>User Ip</th>
																<th>Login Time</th>
															</tr>
														</thead>
														<tbody>
															<?php 
															$id=$_SESSION['id'];
															$query=mysqli_query($con,"SELECT * from userlog where  userId='$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['loginTime'];?></td>
																</tr>
																<?php $cnt=$cnt+1;
															} ?>
														</tbody>
													</table>
												</div>
											</div>
										</body>
										<?php
									} else{
										header('location:logout.php');
									}
									?>
								
							
Download

Comments

Gloria Tracy

This code worked for me thank you very much

Judith Mackline

I thank God who equip you with good heart of teaching world

Leave a Comment:

You Might Also Like

hostel booking

Hostel booking management system
Learn More
.

car rental

Car rental management system in Php
Learn More
.

student details

Student details management system
Learn More
.

Tourism

Tourism management system in Php and Mysql
Learn More

Latest Tutorial

sweet alerts

How to delete table row using sweet alert2
Learn More

piechart

How to create piechart with Mysql data
Learn More

register and login

How to register and login in php
Learn More

edit data

How to edit Mysql data in modal using php
Learn More