Store Last Login Details In DB

Posted on 2004-09-20
Last Modified: 2008-02-01

I have a Database with a field called Last_login, how do I store the date, time and IP of the clients PC within this cell, it will need to go into the correct Last_login cell that belongs to dms_id (so I store it in the correct persons field)

I wish the details to be stored as below for example...

Thursday, 18 February 2004 - 18:26 - IP:

How do I put that data into the field for the correct client, and also read it back when needed ?

Question by:MirageSF
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
LVL 36

Expert Comment

ID: 12109308
Hi MirageSF,

You of course would need to have a cross-referencing field. For example, your table would have to have a userid field. That way, you can tell which user is which row.

Then, you'd use standard mysql_query to insert stuff into a mysql database (other ways for other databases).

How advanced are you in PHP, mySQL, etc. or programming in general?


Author Comment

ID: 12109362
Hi Zyloch,

I have the two tables, one called clients contains DMS_ID, Name, Email and DMS Password.

The other called domains contains DMS_ID, Domain, Username, Password, Plan, Server, Created, Status, Invoice_reference, invoice_amount, invoice_freq and last_login.

Basically I need some code that would generate a string of "Thursday, 18 February 2004 - 18:26 - IP:" for example, and I could then insert this into the correct field.  Reading it back would be easy as its already stored as one string.

I used to program in Pascal, Basic, C++ etc years and years ago, but my grey matter in these areas has gone somewhat fuzzy, from little snippets of code and ideas plus part memory I have managed to do what I need to do so far, and with the help of you guys on EE my knowledge is improving.

LVL 36

Accepted Solution

Zyloch earned 250 total points
ID: 12109381
Ah wait, I remember you...

You're not half bad :)

Anyways, you can get the date like this:

date("l dS of F Y -  H:i:s");

The IP can be gotten in several ways, but second post
has a great script.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 12109478
Thankyou, works great dont understand how I miss these simple things :(

This works fine for server time of last login, but any easy way to get the client machines time ?

BTW - Just found out the hard way that MD5 Hash encryption is one way lol
LVL 36

Expert Comment

ID: 12109491
Heh, it's what makes it more secure, md5 I mean.

Client machine time... hmm... I haven't heard of a way to get this with PHP. Only with client side Javascript...
LVL 49

Expert Comment

ID: 12110042
You could possibly store the clients timezone as a property of their accounts. Then you'd just save the servertime and adjust for the timedifference.

LVL 49

Expert Comment

ID: 12110050
Owh, one thing.. You could use the DATETIME column format instead of the VARCHAR/STRING column. This would be more spaceefficient and should automatically always make sure your dates are correct because the db does the checking.

you'd input data in a date('Y-m-d H:i:s') format--> ('2004-09-21 10:15:12') in my timezone at the moment.



Expert Comment

ID: 12114668
I don't think you want to store the client's machine time. that could be set to anything. store all your times in the same timezone (based on the server time) in the db to make life easy, and then do what Roonan suggested (store the client timezone somewhere) if you need to see the time in the client's local time.

Expert Comment

ID: 12119269
You should try to never trust users or users PC since you never know if they have correctly configured their PCs. You should use the server time and if the website is going to be seen from other countries then you should ask the user for their timezone and calculate the current time for them.

I would do it something like this:

Mysql database:
# Table structure for table 'user'

  `user_id` varchar(32) NOT NULL default '',
  `username` varchar(32) binary NOT NULL default '',
  `password` varchar(32) binary NOT NULL default '',
  PRIMARY KEY  (`user_id`),
  UNIQUE KEY `k_username` (`username`)

# Dumping data for table 'user'

INSERT INTO `user` VALUES("cd5afb82baeb22502d9be171039e5c20", "user1", "pwd1");
INSERT INTO `user` VALUES("992bc880c621d1dd4655f24094070548", "user2", "pwd2");

# Table structure for table 'log'

  `user_id` varchar(32) NOT NULL default '',
  `date` date default NULL,
  `time` time default NULL,
  `ip` varchar(15) NOT NULL default ''

# Dumping data for table 'log'

INSERT INTO `log` VALUES("cd5afb82baeb22502d9be171039e5c20", "2004-09-01", "15:05:25","");
INSERT INTO `log` VALUES("cd5afb82baeb22502d9be171039e5c20", "2004-09-01", "16:06:26","");
INSERT INTO `log` VALUES("992bc880c621d1dd4655f24094070548", "2004-09-22", "11:05:06","");

PHP code:

//you can use this variables or CURRENT_TIMESTAMP()
$now_value = date("Ymd H:i:s");
$user_ip = getenv("REMOTE_ADDR");

//insert connection to db and select database
mysql_query("INSERT INTO log values ('$uid',CURRENT_TIMESTAMP(),'$user_ip')") or die(mysql_error());

//depends on what you want to do you could use
//MAX(DATE_FORMAT(date,'%W, %d %M %Y %H:%i')) AS date
$sql = "SELECT DATE_FORMAT(date,'%W, %d %M %Y %H:%i') AS date,ip FROM log WHERE user_id='$uid';
$query_log = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows ($query_log) > 0) {
  $date_txt = mysql_result ($query_log,0,"date");
  $ip = mysql_result ($query_log,0,"ip");
} else {
  $date_txt = "Never logged on";
  $ip = "";

if ($ip!="") echo($date_txt." - ".$ip) else echo $date_txt;

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question