PHP Session Help (Single Page)

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
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

PHP Session Help (Single Page)

Post by TheBrandon »

Hello all,

The client wants to display a flash intro page (preferrably once a week). I wrote a solution using cookies, but I realized if they weren't allowing cookies they would never get past the front page. So I figured I could set a $_SESSION token to get them in anyways, they would just see the intro page everytime (not once a week).

However, I can't seem to get it to work. Can anyone take a look?

Code: Select all

<?php
session_start(); //start PHP session 
print_r($_SESSION);

	//if the intro hasn't been viewed
	if(!isset($_COOKIE['viewed_intro']) || !isset($_SESSION['viewed_intro'])){
	
		//set the viewed_intro cookie so they don't see the flash intro again
		
		//5 second debug
		setcookie ('viewed_intro', 'Yep, I viewed it', time() + 5);
		
		//set the session
		$_SESSION['viewed_intro'] = 'Yep, I viewed it';
		print_r($_SESSION);
?>

FLASH INTRO GOES HERE

<?php
	}else{
?>
HTML WEBSITE GOES HERE
<?php
	}
?>
It works in a cookie-enabled browser however if I disable cookies, it just hangs on the intro page.

I have the 2 print_r's in check and its giving me this:
Array ( ) Array ( [viewed_intro] => Yep, I viewed it )
I know the issue is it's failing the if check. But why?

Does session_start clear the session, so it's erasing the fact that I just set it? How can I work around this?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: PHP Session Help (Single Page)

Post by flying_circus »

Persistent sessions are based on cookies. This is why you are seeing the problem when cookies are disabled.
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

Re: PHP Session Help (Single Page)

Post by TheBrandon »

Do you have any recommendations on achieving the desired functionality?
rnoack
Forum Commoner
Posts: 34
Joined: Mon May 03, 2010 12:38 am

Re: PHP Session Help (Single Page)

Post by rnoack »

this is maybe excessive, but if you have a mysql database, you could generate some combination of user ip + http user agent, and store that in the database with viewed=true.

then just check it when the page is accessed.

they would see it again if they used a different browser or their ip address changed, though.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: PHP Session Help (Single Page)

Post by flying_circus »

There is no reliable way when working with cookies. The server blindly sends them to the client, but the client does not respond wether they were received, accepted, or rejected. About the only way to do it is to enfore a cookie check and warn your users that your site requires cookies.

1. When the client visits your page, check if the cookie from your domain exists.
2. If the cookie exists, the client accepts/propagates cookies, set your cookie flag as flash = viewed, and be done.
3. If no cookies exist, manually set one, then redirect the visitor to a cookie check page (or set a flag in the url querystring), which checks for cookie existence.
4. If no cookie exists, your client does not support cookies, and you can warn them that your site requires cookies.
Post Reply