Page 1 of 1
Simple Session
Posted: Fri Jun 30, 2006 8:54 am
by jammyjames
Hi,
I have a site with a main feature page. People are linking directly to this page and I am not getting visitors on my home page.
I wanted to create a session that lasted 24 hours and if expired and the link click it would redirect to the homepage where the session could be started for a further 24 hours.
I don't want any messages to appear and I don't want to use a login.
Is this possible?
I am pretty new to PHP and kind of struggling with this one.
Can anyone help?
Thanks

Posted: Fri Jun 30, 2006 9:02 am
by Luke
Let me make sure I understand: You want to make it so if user hasn't been to your home page in 24 hours, the second page will redirect them to it? Well if that's the case, you can do it without a login, but cookies would have to be enabled, so there is no way to strictly ENFORCE it without a login
Posted: Fri Jun 30, 2006 9:09 am
by jammyjames
You've got it exactly.
I want to maximise the number of visits to the homepage and this is the one way I can think of doing it.
Could there be a hidden login where php will automatically fill in details?
Posted: Fri Jun 30, 2006 9:15 am
by Luke
jammyjames wrote:You've got it exactly.
I want to maximise the number of visits to the homepage and this is the one way I can think of doing it.
Could there be a hidden login where php will automatically fill in details?
No... that would require cookies too. I wouldn't go that route anyway. Redirecting your users to your home page when they are used to being able to simply go to a page they are already familiar with is probably not the best idea. If a site I went to often did that I would be irritated. But, if you really want to do it.... just use cookies. If you aren't familiar with the use of
Sessions and
Cookies, read the information at the links provided. Once you are done with that, if you still have questions, I'd be happy to answer them!

Posted: Fri Jun 30, 2006 9:17 am
by jammyjames
Well the good thing is that the site is pretty new. Ill take a gander at those links.
Thanks
Posted: Fri Jun 30, 2006 9:54 am
by Jenk
A messy way is to use (PHP's own) session ID's and set the session exipry time to 24hours.
A 'better' way, would be to use your own form of session ID's in a DB table, along with a timestamp - then a simple check and redirect if necessary, like so:
Code: Select all
<?php
//mysql_connect etc. is assumed up here..
if (!empty($_GET['mySessID'])) {
$result = mysql_query("SELECT `timeStamp` FROM `UserSessions` WHERE `sessionID` = '" . mysql_real_escape_string($_GET['mySessID']) . "'");
if (($row = mysql_fetch_assoc($result)) !== false) {
if ((intval($row['timeStamp']) + 86400) < time()) {
header('Location: homepage.php');
}
} else {
header('Location: homepage.php');
}
} else {
header('Location: homepage.php');
}
?>
When emailing links (or whatever) remember to generate a session ID (uniqueid() or auto-increment field or such.)
Could be tidier.. but should be enough to give you an idea..