Log User Login Time with PHP MySQL

Log User Login Time with PHP MySQL

 

This post will show you How to create a Table to Log User Login Time , into mySQL Database using PHP.

 

Prior to that you need to complete  the items listed Below

  1. Mamp or xampp with phpMyAdmin install into your Computer , check out here
  2.  A Sample Database created Check out Here
  3.  New User Created . Check out Here

Step 1 

(1)  Go to MySQL Database ,

(2) Select Your Database

(3) Create A New Table name “accesslog ” **  you  can name it differently

(4) To Create a New Table  Copy and Paste the Below SQL Code to the Query Text Box & Click Go.

 

CREATE TABLE accessLog (
						ID INT (11) NOT NULL  AUTO_INCREMENT PRIMARY KEY ,
						userID INT( 11 ) NOT NULL, 
						timeLogin DATETIME, 
						timeLogout DATETIME, 
					    IPaddress VARCHAR (15) 
                      )





 

 

Step 2 

Paste the Code below between the Code Chunk , after you Verified your User’s Passwords

 


//-------------------------------------------Record User Time When Login------------------------------------------------------------------------
							
	// Insert User Time Login
	   $success = false;
	// Get the User IP
	  $userIP = $_SERVER['REMOTE_ADDR'];										
	  $nowTimeStamp = date("Y-m-d H:i:s");
        // Prepare the SQL Statements  to Insert User Login Time											
											
        $insertLogin_SQL = 'INSERT INTO accesslog (userID,timeLogin,IPaddress )VALUES('.$id.',"'.$nowTimeStamp.' ","'.$userIP.'"'.')';
															
	if ( $dbConnectionStatus->query($insertLogin_SQL))  {	
							    $success = true;
							    } else {
									echo'Error';	
										}						 
												
													
//------------------------------------------------------------------------------------------------------------------------------------------------------




 

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × one =