Last Login [PHP & MYSQL]

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
Wouldn't even work
What do you mean by last login script, are you basically just trying to determine the last time a user logged in? If so that's extremely simple, you could just insert timestamp data into a field called lastlogin on the user's record upon the login script, and then when you wish to output the last login time you simply just format it into the date you want it to be from the timestamp with date("", $timestamp).
 

TrueJewix

Member
Aug 9, 2012
332
21
What do you mean by last login script, are you basically just trying to determine the last time a user logged in? If so that's extremely simple, you could just insert timestamp data into a field called lastlogin on the user's record upon the login script, and then when you wish to output the last login time you simply just format it into the date you want it to be from the timestamp with date("", $timestamp).
YEah exactly..aw, can I get a script pls lol
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
YEah exactly..aw, can I get a script pls lol
Well I basically told you how to do it but here is a very basic method of the script, don't forget you would have to escape the query and such and also link the POST's etc.

PHP:
$last = time(); // this is the current time as a timestamp

INSERT INTO users (id, username, password, last)
VALUES ('','$username','$password','$last');
then output it like so:
PHP:
$time = SELECT last FROM users WHERE username = '$username';
date("H:i:s", $time) // this converts the timestamp to a readable date/time
read for more formatting options of the date/time.
 

TrueJewix

Member
Aug 9, 2012
332
21
Well I basically told you how to do it but here is a very basic method of the script, don't forget you would have to escape the query and such and also link the POST's etc.

PHP:
$last = time(); // this is the current time as a timestamp

INSERT INTO users (id, username, password, last)
VALUES ('','$username','$password','$last');
then output it like so:
PHP:
$time = SELECT last FROM users WHERE username = '$username';
date("H:i:s", $time) // this converts the timestamp to a readable date/time
read for more formatting options of the date/time.
Shit is really confusing for me, see I'm new to PHP & MYSQL so I'm learning... is there a way if you could give me like the whole code tell me where to put it etc?
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
You don't even need to create a new table for it. Just add a column called LastLogin or something in your users table

Then when they log in, just do an UPDATE on LastLogin to the value of time()

Then when you want to display it, just do a SELECT from users and use date() to format it
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
Shit is really confusing for me, see I'm new to PHP & MYSQL so I'm learning... is there a way if you could give me like the whole code tell me where to put it etc?
You can't just put all of that in one place lol, if I gave you the whole code I'd have to make the login script as well etc.

You would just run a query which will update the last login time within your actual PHP login script so it will update the last login time every time a user successfully logs in.

And then where ever you want to display the time that the user last logged in the second part, use a query to select the data and then format it with date().
 

TrueJewix

Member
Aug 9, 2012
332
21
You don't even need to create a new table for it. Just add a column called LastLogin or something in your users table

Then when they log in, just do an UPDATE on LastLogin to the value of time()

Then when you want to display it, just do a SELECT from users and use date() to format it
Aha, I see but I don't understand 'to the value of time()'
what do you mean by that ?
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Step 1) Create a new column to hold your Date information
05lVHIi.png

LastLogin is the name of the column, Date is the value, and I always put 0 0 until I know how much information I want it to store, and Not Null because a date column should always have a default value. Upon registery you will satisfy the LastLogin with todays Date.

Step 2) Satisfy the database upon registery
$last = time(); <- As @BIOS explained, time() is a function in PHP that gets the time from your computer and will store that into the variable $last
INSERT INTO users (id, username, password, LastLogin)VALUES ('','$username','$password','$last'); Then you will set LastLogin = the variable $last that you previously set to the time of the computer.

Step 3) Retrieve the information
$LastLoginTime = mysql_query("SELECT LastLogin FROM users WHERE id="'.USER_ID.'" LIMIT 1");
$DateMe = strtotime( $LastLoginTime );
$EchoMe = date('Y-m-d H:i:s', $DateMe);
echo $EchoMe;
 

TrueJewix

Member
Aug 9, 2012
332
21
Step 1) Create a new column to hold your Date information
05lVHIi.png

LastLogin is the name of the column, Date is the value, and I always put 0 0 until I know how much information I want it to store, and Not Null because a date column should always have a default value. Upon registery you will satisfy the LastLogin with todays Date.

Step 2) Satisfy the database upon registery
$last = time(); <- As @BIOS explained, time() is a function in PHP that gets the time from your computer and will store that into the variable $last
INSERT INTO users (id, username, password, LastLogin)VALUES ('','$username','$password','$last'); Then you will set LastLogin = the variable $last that you previously set to the time of the computer.

Step 3) Retrieve the information
$LastLoginTime = mysql_query("SELECT LastLogin FROM users WHERE id="'.USER_ID.'" LIMIT 1");
$DateMe = strtotime( $LastLoginTime );
$EchoMe = date('Y-m-d H:i:s', $DateMe);
echo $EchoMe;
Aw, thanks a lot. I'm stuck at the 3rd part lol ... like is there a certain way to do it ?

, and it displays this : . My bad if I i'm being nooby
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Aw, thanks a lot. I'm stuck at the 3rd part lol ... like is there a certain way to do it ?

, and it displays this : . My bad if I i'm being nooby
It's likely you don't actually have a constant defined as USER_ID. I'm assuming Jay wanted you to change that to whatever variable your user ID is called

Also, the whole 'strtotime' line is useless because if you're storing time() in the LastLogin column, it'll already come out as a UNIX time stamp so essentially you'll be converting a UNIX time stamp to a UNIX time stamp, which I guess may give you a different value? Not sure. So you can remove that line
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
Please avoid all the above code. mysql_* functions are deprecated. Check out MySQLi or PDO instead.
Duh. He didn't ask that, the reason the above comments are in MySQL is because it's the easiest possible way to show him how to achieve what he's trying to.

Plus he's fine using MySQL for now with escaping since he's stated he doesn't know much and trying to learn, not to mention he could always upgrade it further on.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Duh. He didn't ask that, the reason the above comments are in MySQL is because it's the easiest possible way to show him how to achieve what he's trying to.

Plus he's fine using MySQL for now with escaping since he's stated he doesn't know much and trying to learn, not to mention he could always upgrade it further on.
The MySQL functions are deprecated and, from my understanding, soon be removed from newer versions of PHP so it's best for him to traverse now. But let's not get bogged down in what method to use.
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
The MySQL functions are deprecated and, from my understanding, soon be removed from newer versions of PHP so it's best for him to traverse now. But let's not get bogged down in what method to use.
Yeah, I think he has started to try MySQLi slightly from what I can see, he is using MySQLi for his connection however for his queries, he's using MySQL. -
 

Users who are viewing this thread

Top