Simple Session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jammyjames
Forum Newbie
Posts: 12
Joined: Fri Jun 30, 2006 8:45 am

Simple Session

Post 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

:lol:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
jammyjames
Forum Newbie
Posts: 12
Joined: Fri Jun 30, 2006 8:45 am

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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! :D
jammyjames
Forum Newbie
Posts: 12
Joined: Fri Jun 30, 2006 8:45 am

Post by jammyjames »

Well the good thing is that the site is pretty new. Ill take a gander at those links.

Thanks
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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..
Post Reply