Jump to content

Access logs php/mysql


Recommended Posts

Hi Guyz

After getting assistance on the previous posts, here I am with another one. I want to record the time when user login and logout of the web. I have searched through the web and found web stats and access counters etc but I dont need something as detailed as that I have a very small website with users given login passwords to go and add reports (done in php/mysql). Can someone direct me to a suitable tutorial or a sample I can look at -- thanks.

Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/
Share on other sites

[!--quoteo(post=380577:date=Jun 6 2006, 08:48 AM:name=sab)--][div class=\'quotetop\']QUOTE(sab @ Jun 6 2006, 08:48 AM) [snapback]380577[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi Guyz

After getting assistance on the previous posts, here I am with another one. I want to record the time when user login and logout of the web. I have searched through the web and found web stats and access counters etc but I dont need something as detailed as that I have a very small website with users given login passwords to go and add reports (done in php/mysql). Can someone direct me to a suitable tutorial or a sample I can look at -- thanks.
[/quote]

just add something like this after the login verification is succesful

$logged = date("H:i:s");

and UPDATE it in your user's table

same thing for loggout, just make sure you make 2 new rows in your database, one for the time they logged in and the other for the time they logged out
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-42450
Share on other sites

[!--quoteo(post=380640:date=Jun 6 2006, 11:00 AM:name=digitalgod)--][div class=\'quotetop\']QUOTE(digitalgod @ Jun 6 2006, 11:00 AM) [snapback]380640[/snapback][/div][div class=\'quotemain\'][!--quotec--]
just add something like this after the login verification is succesful

$logged = date("H:i:s");

and UPDATE it in your user's table

same thing for loggout, just make sure you make 2 new rows in your database, one for the time they logged in and the other for the time they logged out
[/quote]

thanks for the reply but if I use update I will overwrite the login/logout time. I want keep the old login as well as new one.

I might need a new table for this but not sure. Also how can i have 2 timestamp columns in a table one for login and one for logout

you help is much appreciated

thanks
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-42728
Share on other sites

[!--quoteo(post=380926:date=Jun 7 2006, 05:02 AM:name=sab)--][div class=\'quotetop\']QUOTE(sab @ Jun 7 2006, 05:02 AM) [snapback]380926[/snapback][/div][div class=\'quotemain\'][!--quotec--]
thanks for the reply but if I use update I will overwrite the login/logout time. I want keep the old login as well as new one.

I might need a new table for this but not sure. Also how can i have 2 timestamp columns in a table one for login and one for logout

you help is much appreciated

thanks
[/quote]


you can have as many timestamp columns in a table as you want. If you want to keep the old login you'll probably have to make a new table with just: id users login_time logout_time , where users is the name of your user that logged in/out.
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-42742
Share on other sites

[!--quoteo(post=380940:date=Jun 7 2006, 05:57 AM:name=digitalgod)--][div class=\'quotetop\']QUOTE(digitalgod @ Jun 7 2006, 05:57 AM) [snapback]380940[/snapback][/div][div class=\'quotemain\'][!--quotec--]
you can have as many timestamp columns in a table as you want. If you want to keep the old login you'll probably have to make a new table with just: id users login_time logout_time , where users is the name of your user that logged in/out.
[/quote]

But I get error if I put two timestamps. Can you explain how to go about and do this.
thanks
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-42748
Share on other sites

[!--quoteo(post=380946:date=Jun 7 2006, 06:12 AM:name=sab)--][div class=\'quotetop\']QUOTE(sab @ Jun 7 2006, 06:12 AM) [snapback]380946[/snapback][/div][div class=\'quotemain\'][!--quotec--]
But I get error if I put two timestamps. Can you explain how to go about and do this.
thanks
[/quote]

I have solved half of the problem, I have managed to insert login time but how do i insert logout time can any1 help plzzz.
thanks in advance

here is my logout code:

session_start();

$_SESSION['logged_in'] = 0;

setcookie('login_cookie', false, time() - 3600, '/secure/', 'index.php');

mysql_query("update access_log set logout_datetime = date('Y-m-d h:i:sa', time()) ");

session_destroy();

header("Location: index.php");
?>
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-42800
Share on other sites

[!--quoteo(post=380999:date=Jun 7 2006, 10:19 AM:name=sab)--][div class=\'quotetop\']QUOTE(sab @ Jun 7 2006, 10:19 AM) [snapback]380999[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I have solved half of the problem, I have managed to insert login time but how do i insert logout time can any1 help plzzz.
thanks in advance

here is my logout code:

session_start();

$_SESSION['logged_in'] = 0;

setcookie('login_cookie', false, time() - 3600, '/secure/', 'index.php');

mysql_query("update access_log set logout_datetime = date('Y-m-d h:i:sa', time()) ");

session_destroy();

header("Location: index.php");
?>
[/quote]

mysql_query("UPDATE access_log SET logout_datetime = date('Y-m-d h:i:sa', time()) WHERE id = 1 ");

should do the trick
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-42890
Share on other sites

[!--quoteo(post=381091:date=Jun 7 2006, 01:15 PM:name=digitalgod)--][div class=\'quotetop\']QUOTE(digitalgod @ Jun 7 2006, 01:15 PM) [snapback]381091[/snapback][/div][div class=\'quotemain\'][!--quotec--]
mysql_query("UPDATE access_log SET logout_datetime = date('Y-m-d h:i:sa', time()) WHERE id = 1 ");

should do the trick
[/quote]

The query still not updating logout_datetime column :(
+--------+----------+---------------------+---------------------+
| log_id | username | login_datetime | logout_datetime |
+--------+----------+---------------------+---------------------+
| 1 | abc | 2006-06-07 17:10:35 | 0000-00-00 00:00:00 |
| 2 | abc | 2006-06-07 17:11:49 | 0000-00-00 00:00:00 |
+--------+----------+---------------------+---------------------+
2 rows in set (0.05 sec)

any suggestions?
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-43098
Share on other sites

[!--quoteo(post=381305:date=Jun 8 2006, 05:49 AM:name=sab)--][div class=\'quotetop\']QUOTE(sab @ Jun 8 2006, 05:49 AM) [snapback]381305[/snapback][/div][div class=\'quotemain\'][!--quotec--]
The query still not updating logout_datetime column :(
+--------+----------+---------------------+---------------------+
| log_id | username | login_datetime | logout_datetime |
+--------+----------+---------------------+---------------------+
| 1 | abc | 2006-06-07 17:10:35 | 0000-00-00 00:00:00 |
| 2 | abc | 2006-06-07 17:11:49 | 0000-00-00 00:00:00 |
+--------+----------+---------------------+---------------------+
2 rows in set (0.05 sec)

any suggestions?
[/quote]


just replace the part that says WHERE id = 1 with WHERE username = and that variable or session you used to store the username

something like

mysql_query("UPDATE access_log SET logout_datetime = date('Y-m-d h:i:sa', time()) WHERE username = '$username' ");
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-43294
Share on other sites

[!--quoteo(post=381503:date=Jun 8 2006, 12:49 PM:name=digitalgod)--][div class=\'quotetop\']QUOTE(digitalgod @ Jun 8 2006, 12:49 PM) [snapback]381503[/snapback][/div][div class=\'quotemain\'][!--quotec--]
just replace the part that says WHERE id = 1 with WHERE username = and that variable or session you used to store the username

something like

mysql_query("UPDATE access_log SET logout_datetime = date('Y-m-d h:i:sa', time()) WHERE username = '$username' ");
[/quote]

Thanks digitalgod it worked. :)
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-43567
Share on other sites

[!--quoteo(post=381779:date=Jun 9 2006, 04:00 AM:name=sab)--][div class=\'quotetop\']QUOTE(sab @ Jun 9 2006, 04:00 AM) [snapback]381779[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Thanks digitalgod it worked. :)
[/quote]

Hi

Here I am again, I thought i have managed to make it work but i didnt. the problem is when I update logout time for specific user, the whole column get updated for example:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]+--------+----------+---------------------+---------------------+
| log_id | username | login_datetime | logout_datetime |
+--------+----------+---------------------+---------------------+
| 1 | abc | 2006-06-07 17:10:35 | 2006-06-16 14:11:29 |
| 2 | def | 2006-06-07 17:11:49 | 2006-06-16 14:11:29 |
| 4 | ghi | 2006-06-16 12:05:02 | 2006-06-16 14:11:29 |
| 5 | jkl | 2006-06-16 12:08:27 | 2006-06-16 14:11:29 |[/quote]

the code is:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]<?
require "conn.php";

Session_start();

$_SESSION['logged_in'] = 0;
$_SESSION['username'] = $_POST['username'];
$_SESSION['log_id'] = log_id;

setcookie('login_cookie', false, time() - 3600, '/secure/', '.index1.php');

mysql_query("UPDATE access_log SET logout_datetime = now() WHERE log_id=log_id and username=username"); or die (mysql_error());

session_destroy();

header("Location: index1.php");

?>[/quote]

here is my login script:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]<?
ob_start();
session_start();
$_SESSION['time_start']=$start;
$_SESSION['log_id']= log_id;

if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
header ("Location: page.php");
$sql = mysql_query("INSERT INTO users (first_name, last_name, email_address,
username, password, info, signup_date)
VALUES('$first_name', '$last_name', '$email_address',
'$username', '$db_password', '$info2', now())") or die (mysql_error());
} //end if logged in

if(isset($_POST['submit'])) {

if(!$_POST['username']) die("Error: You must enter your username before logging in.");
if(!$_POST['password']) die("Error: You must enter your password before logging in.");


mysql_query("INSERT INTO access_log (username,login_datetime)
VALUES('$username', now())") or die (mysql_error());

$get_user = mysql_query("SELECT * FROM `users` WHERE username = '".$_POST['username']."' AND

user_password = '".md5($_POST['password'])."'");
$q = mysql_fetch_object($get_user);
if(!$q) die("Login Failure: An error occured, please verify your username and password are correct.");

//set session variables
$_SESSION['logged_in'] = 1;
$_SESSION['log_id']= $_POST['log_id'];
$_SESSION['username'] = $_POST['username'];

session_write_close();

header("Location: page.php");

} else {

//show login form
?>
<html>
<head><title>Rep sign-up page</title>
</head>
<body>
<h4>Existing user please login</h4>
<form name="login" method="post" action="<? $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
<td>Username:<input type="text" id="username" name="username"></td>
</tr>
<tr>
<td>Password:<input type="password" id="password" name="password"></td>
</tr>
<tr>
<td>Submit: <input type="submit" value="Login" name="submit" id="submit"></td>
</tr>
</table>
</form>


</body>
</html>
<?
}//end else

?>[/quote]
Can some1 help plzzz.

thanks in advance
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-46258
Share on other sites

[code]

logout.php
<?

require "conn.php";

Session_start();

$_SESSION['logged_in'] = 0;
$_SESSION['username'] = $_POST['username'];
$_SESSION['log_id'] = log_id;

setcookie('login_cookie', false, time() - 3600, '/secure/', '.index1.php');




if($_GET["cmd"]=="update") {

$logout_datetime=date('Y-m-d h:i:sa', time());
$logout_datetime=$_POST['logout_datetime'];



mysql_query("UPDATE access_log SET logout_datetime = '$logout_datetime' WHERE log_id=log_id and username=username"); or die (mysql_error());
}

session_destroy();

header("Location: index1.php");

?>
[/code]


The link to update and log out.
[code]

<a href="logout.php?&cmd=update&log_id=$log_id">Logout</a>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-46267
Share on other sites

[!--quoteo(post=384564:date=Jun 16 2006, 09:20 AM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Jun 16 2006, 09:20 AM) [snapback]384564[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]

logout.php
<?

require "conn.php";

Session_start();

$_SESSION['logged_in'] = 0;
$_SESSION['username'] = $_POST['username'];
$_SESSION['log_id'] = log_id;

setcookie('login_cookie', false, time() - 3600, '/secure/', '.index1.php');
if($_GET["cmd"]=="update") {

$logout_datetime=date('Y-m-d h:i:sa', time());
$logout_datetime=$_POST['logout_datetime'];
mysql_query("UPDATE access_log SET logout_datetime = '$logout_datetime' WHERE log_id=log_id and username=username"); or die (mysql_error());
}

session_destroy();

header("Location: index1.php");

?>
[/code]
The link to update and log out.
[code]

<a href="logout.php?&cmd=update&log_id=$log_id">Logout</a>

[/code]
[/quote]

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]I have tried that but now I get the following:
[code]+--------+----------+---------------------+---------------------+
| log_id | username | login_datetime | logout_datetime |
+--------+----------+---------------------+---------------------+
|     50 |          | 2006-06-19 17:06:49 | 0000-00-00 00:00:00 |
|     51 |          | 2006-06-19 17:07:25 | 0000-00-00 00:00:00 |
|     52 |          | 2006-06-19 17:08:16 | 0000-00-00 00:00:00 |
|     53 |          | 2006-06-19 17:09:33 | 0000-00-00 00:00:00 |
|     54 |          | 2006-06-19 17:10:08 | 0000-00-00 00:00:00 |
+--------+----------+---------------------+---------------------+[/code]
Username is blank and logout column remain '0000-00-00 00:00:00'

here is the table structure if helps:
[code]+-----------------+-------------+------+-----+---------------------+------------
----+
| Field           | Type        | Null | Key | Default             | Extra
    |
+-----------------+-------------+------+-----+---------------------+------------
----+
| log_id          | int(25)     | NO   | PRI | NULL                | auto_increm
ent |
| username        | varchar(25) | NO   |     |                     |
    |
| login_datetime  | datetime    | NO   |     | 0000-00-00 00:00:00 |
    |
| logout_datetime | datetime    | NO   |     | 0000-00-00 00:00:00 |
    |
+-----------------+-------------+------+-----+---------------------+------------[/code]
Any suggestions plzzz

thanks[/quote]
Link to comment
https://forums.phpfreaks.com/topic/11321-access-logs-phpmysql/#findComment-47342
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.