Jump to content

Recommended Posts

1- You need to keep a field in your users table

2- when user logs into you website update this field with the timestamp of server at that moment

3- display this field where you want

 

Step 1a, before you update, save the existing value as $lastVisit, otherwise you be displaying the time of this visit

This is what I have so far, and what is wrong with it?

 

Question: When would the lastVisit column

update. During registeration or during login.

If its during login the field is lefted blank at

registeration.

 

CREATE TABLE users (username varchar(30),password varchar(32), 
lastVisit TIMESTAMP NULL DEFAULT NULL);

<?php
$currentVisit = date();
mysql_query( "update users set lastVisit= '$currentVisit'
where user_name = '$user_name'");
?>

What I do is to only track logins. You have to concate the the time of login into a mysql field using a common delimiter to separate time entries.

 

When the time of login is checked you can simply manipulate the $row['login_time'] by exploding it into an array.

 

$log_times = explode(",", $row['login_time']);//If you use a comma as the delimiter

 

You could then count the array using:

$log_check = count($log_times -1); Php starts arrays at position 0, this will get you current login

 

Now you can use the exploded array along with the array count to display the current login, if you want to you can use other variables to get various logins by manipulating the math formula on $log_check:

 

$last_log =  "$log_times[$log_check]";

 

echo "$last_log";

 

I hope that gets you going in the right direction.

What I do is to only track logins. You have to concate the the time of login into a mysql field using a common delimiter to separate time entries.

 

When the time of login is checked you can simply manipulate the $row['login_time'] by exploding it into an array.

 

$log_times = explode(",", $row['login_time']);//If you use a comma as the delimiter

 

You could then count the array using:

$log_check = count($log_times -1); Php starts arrays at position 0, this will get you current login

 

Now you can use the exploded array along with the array count to display the current login, if you want to you can use other variables to get various logins by manipulating the math formula on $log_check:

 

$last_log =  "$log_times[$log_check]";

 

echo "$last_log";

 

I hope that gets you going in the right direction.

 

Where does $row['login_time']; get its value from?

Correct me if I am wrong, but wouldn't the syntax

be similar to the code below for saving the existing

value as $lastVisit.

 

Note: The database field is set as TIMESTAMP NULL DEFAULT NULL

Also, I noticed that when I enter data during registeration the TIMESTAMP

field auto updated time. Not sure if this is what I want.

<?php
$currentVisit = date();
mysql_query( "update users set lastVisit= '$currentVisit'
where user_name = '$user_name'");
?>

create a field for user-> Last Visit  (datetime) NOT NULL  default:0000-00-00 00:00:00

 

then:

<?php

$sql1="SELECT Last_visit FROM user WHERE user_id='$userid'";
$result1=mysql_query($sql1);
$row=mysql_fetch_assoc($result1);
$last=$row['Last_visit'];
$_SESSION['last']=$last;   //use the session variable to hold the previous last visit date.


$sql="UPDATE user SET Last_visit=NOW() WHERE user_id='$userid'";   //now only update it
$result=mysql_query($sql);

?>

 

it should work well for you.

 

Regards,

chai

 

What am I doing wrong?

I am looking for this format MMDDYYYY

 

last_visit datetime not NULL default'0000-00-00' 
returns YYYYMMDDHHMMSS

last_visit TIMESTAMP NULL DEFAULT NULL
returns YYYYMMDDHHMMSS

 

<?php

$sql1="SELECT last_visit FROM users WHERE user_name='$user_name'";
$result1=mysql_query($sql1);
$row=mysql_fetch_assoc($result1);
$last=$row['last_visit'];
$_SESSION['last']=$last;   //use the session variable to hold the previous last visit date.


$sql="UPDATE users SET last_visit=NOW() WHERE user_name='$user_name'";   
//now only update it
$result2=mysql_query($sql);  
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.