/    Sign up×
Community /Pin to ProfileBookmark

Session help, please.

Hello,

Not sure if this is easily achieved, but I’m using a PHP login script (based on sessions) that I created.

I want to add two session variables which contains the time a user logged in and the date.

For the date syntax we wanted to use something like: 21/04/2005
For the time syntax: 14:34:33

and then be able to echo each out – so for example two variables ‘LOGINDATE’ and ‘LOGINTIME’

I’ve tried all various forms of using date() but after about a minute I just get figures like 2893292… how else can I do this please?

We are not using any form of database for the login script, the actual logins are in our ‘login.php’ encrypted as MD5 hashes for the passwords.

[code=php]
$_SESSION[‘SESSIONDATE’] = ..;
$_SESSION[‘SESSIONTIME’] = ..;
[/code]

Any help on this would be very much appreciated.
Thank you.

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 21.2005 — [code=php]
$_SESSION['SESSIONDATE'] = date("d/m/Y");
$_SESSION['SESSIONTIME'] = date("H:i:s");
[/code]
Copy linkTweet thisAlerts:
@DanUKauthorApr 21.2005 — Hi NogDog, thanks for that.

Yes, that's what we tried too.

It works to begin with, but after about a minute we just get various digits, for example those given above.

That's what I cannot work out. ?
Copy linkTweet thisAlerts:
@DanUKauthorApr 22.2005 — Any ideas on this, please? ?

Thanks.
Copy linkTweet thisAlerts:
@abudabitApr 22.2005 — We can't tell what it is without seeing your script.

I always just:

session_register("searchdata");

session_register("userprofile");

in my header. Always works great for me. Try that.
Copy linkTweet thisAlerts:
@gaston9x19Apr 23.2005 — Here's another idea, I set cookies for the last time a user logged in, and that seems to work pretty well. You can then echo the cookie data easily.

setcookie("logged_in_date", date("d/m/Y"));

setcookie("logged_in_time", date("H:i:s"));

echo $_COOKIE['logged_in_date'];

echo $_
COOKIE['logged_in_time'];

These session cookies expire when the user closes the browser window, or you could manually expire the cookies at user logout.
Copy linkTweet thisAlerts:
@DanUKauthorApr 23.2005 — Hi gaston.

That's great, thank you.

The main script is using sessions for everything else, however for the login date/time the cookies would be fine!

We're using a logout.php which contains:

[code=php]
include_once('/home/us/protect.php');

unset($_SESSION['logged']);
header("Location: http://www.domain.com/secure/login.php");
exit();
[/code]


How could we also get rid of the cookies too without the user closing their browser in case they wanted to login again without closing their browser and to get the 'new' login date and time?

MANY thanks indeed!
Copy linkTweet thisAlerts:
@scragarApr 23.2005 — [code=php]<?
$_SESSION['now'] = mktime();
echo date("H:i:s d/m/Y", $_SESSION['now']);
?>[/code]
Copy linkTweet thisAlerts:
@DanUKauthorApr 23.2005 — Hi scragar.

Many thanks for your reply.

Would that add to the session the time of the login (and not allow it to change until the logout?).

How could I also split that into two seperate variables of logintime and logindate?

Many thanks.
Copy linkTweet thisAlerts:
@scragarApr 23.2005 — I just stored the timestamp, that's much easier to change and allows you to change date formats and such. I don't think it's possible to split it, but you can read in both date and time seperatly if you wanted:[code=php]<?
$_SESSION['now'] = mktime(); //this goes wherever it is you want to set the date.


echo "the current time is ".date('H:i:s', $_SESSION['now']);
echo " on ".date('l', $_SESSION['now'])." the ".date('dS', $_SESSION['now']);
echo " of ".date("F Y", $_SESSION['now']);
/* today gives:
the current time is 12:39:08 on Saturday the 23rd of April 2005
*/
?>[/code]
Copy linkTweet thisAlerts:
@gaston9x19Apr 23.2005 — Kill the cookies at logout by setting them with an expiration date in the past:

setcookie("logged_in_date", "", time()-60);

setcookie("logged_in_time", "", time()-60);

Remember when setting or deleting cookies, the setcookie() function must be called before any output to the browser is sent. Hope this works for ya! ?
Copy linkTweet thisAlerts:
@DanUKauthorApr 23.2005 — Kill the cookies at logout by setting them with an expiration date in the past:

setcookie("logged_in_date", "", time()-60);

setcookie("logged_in_time", "", time()-60);

Remember when setting or deleting cookies, the setcookie() function must be called before any output to the browser is sent. Hope this works for ya! ?[/QUOTE]


Thanks for the replies everyone. ?

I think this cookie one is the best one to go with.

Would you mind confirming, based on the above quote, that logout.php should therefore look like:

[code=php]
include_once('/home/us/protect.php');

setcookie("logindate", "", time()-60);
setcookie("logintime", "", time()-60);

unset($_SESSION['logged']);
header("Location: http://www.domain.com/secure/login.php");
exit();
[/code]


Just to also confirm, as the header goes back to the login page, if they login again there'll be a new cookie set which will show the new login date and time right? The user won't have to close their browser in order for it to work (something we don't want...)

MANY thanks for this help! much appreciated.
Copy linkTweet thisAlerts:
@gaston9x19Apr 23.2005 — Right. Assuming your cookies' names are "logindate" and "logintime", the logout.php script will kill those cookies, and you can set new values at the next login.
×

Success!

Help @DanUK spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...