PHP IP Logging Question

wantabe2
wantabe2 used Ask the Experts™
on
Is there any way for me to edit this code so I can somehow log all attempts to log into this system? This code currently displays the IP address at the bottom of the login page....but is there any way I can log this somewhere or log unsuccessful & successful logons?
<html>  
<body bgcolor="#03EBA6"> 
<head> 

<?php

include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{

// username and password sent from Form 

$myusername=addslashes($_POST['username']); 
$mypassword=addslashes($_POST['password']); 

$sql="SELECT id FROM admin WHERE username='$myusername' and passcode='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1)
{
session_register("myusername");
$_SESSION['login_user']=$myusername;

header("location: welcome.php");
}
else 
{
$error="Your Login Name or Password is invalid";
}
}

?>

<form action="" method="post">
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<center> <h2>Eastern District</h2></center>
<center> <h2> Human Resource Employment Tracking System </h2> </center>
<br>
<br>
<center><label><b>USER NAME :</b></label>

<input type="text" name="username"/><br />

<label><b>PASSWORD :</b></label>

<input type="password" name="password"/><br/>
<br>

<input type="submit" value=" Login "/><br />
</center>
<br>
<br>
<br>
<center> <b> *** The IP address of your computer has already been logged *** </center>
<center> *** All unsuccessful & successful attempts to log onto this system is recorded *** </b> </center>

</form>

<center><b>Your IP Address is: </b><?php echo $_SERVER["REMOTE_ADDR"]; ?></center>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
Sure.  Here are the steps:

1. Create a data base table to keep the log entries.  Maybe three columns with the id, the DATETIME of the event, and the IP address
2. In the code between lines 25 and 36, make the INSERT query to put this information into the data base table.

Sidebar note:  Please see line 27 where it says this, session_register("myusername");  You might want to get rid of that before it's too late:
http://us3.php.net/manual/en/function.session-register.php
Create a table to log the attempts. I think u need to insert 4 things. Username, IP, time, (succes/failure).
On password check insert a row. On line 27-28 succes row and line 34 a failure.

That should do it.
make a table in mysql called "login_log" using the folloing sql. change the database_name to your database name.

CREATE TABLE  `database_name`.`login_log` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 200 ) NOT NULL ,
`ip` VARCHAR( 30 ) NOT NULL ,
`status` INT( 1 ) NOT NULL
) ENGINE = MYISAM ;


modify the code as below:


<html>  
<body bgcolor="#03EBA6">
<head>

<?php

include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{

// username and password sent from Form

$myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['password']);

$sql="SELECT id FROM admin WHERE username='$myusername' and passcode='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];
$count=mysql_num_rows($result);

$query = "INSERT INTO login_log (username, ip,status) VALUES ('" . $myusername  . "','" . $_SERVER["REMOTE_ADDR"] . "','" . $count . "');";
mysql_query($query);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1)
{
session_register("myusername");
$_SESSION['login_user']=$myusername;

header("location: welcome.php");
}
else
{
$error="Your Login Name or Password is invalid";
}
}

?>

<form action="" method="post">
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<center> <h2>Eastern District</h2></center>
<center> <h2> Human Resource Employment Tracking System </h2> </center>
<br>
<br>
<center><label><b>USER NAME :</b></label>

<input type="text" name="username"/><br />

<label><b>PASSWORD :</b></label>

<input type="password" name="password"/><br/>
<br>

<input type="submit" value=" Login "/><br />
</center>
<br>
<br>
<br>
<center> <b> *** The IP address of your computer has already been logged *** </center>
<center> *** All unsuccessful & successful attempts to log onto this system is recorded *** </b> </center>

</form>

<center><b>Your IP Address is: </b><?php echo $_SERVER["REMOTE_ADDR"]; ?></center>

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial