Jump to content

[SOLVED] Last Visited. Weigh in Please!


Trium918

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

Link to comment
Share on other sites

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'");
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

That would be in the user table. You would have a field that would track their login times, I just used the name login_time to make it easier to follow.

 

This would be the field in database correct?

login_time TIMESTAMP NULL DEFAULT NULL

 

Link to comment
Share on other sites

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'");
?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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);  
?>

Link to comment
Share on other sites

Can anyone explain to me what is it that

I am doing wrong. I an trying to get the

last visit to display as 5/8/2007. Currently,

I'm getting the output 20070508155335.

 

If there are any question that will help you

in guiding me, please ask!

Link to comment
Share on other sites

Wow, yes $d should be whatever date you want to be converted to a specific format.

 

strtotime is a wonderful function, glad the php coders included it. Makes date conversion a ton easier!

Link to comment
Share on other sites

<?php
$d = '20070508155335';

echo date ('n/d/Y', strtotime($d));             //--> 5/08/2007
?>

 

I just tried this as a test and I got a blank screen!

 

The problem was the time added at the end 155335. Is there

a way to do away with this?

Link to comment
Share on other sites

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.