Sending a user to a ’splash’ page the first time they visit your site seems like a pretty simple task, but its not. At first glance I thought it would easily work a bit like this:
This is not robust enough though, if the users’ browser has cookies turned off then they will enter an infinite loop and end up endlessly visiting the splash page - not a very good thing for search engine spiders to do :).
So, you need to test if the cookies are turned off on the users browser before you do any redirecting or anything. As it turns out the best test is to set a dummy cookie and then verify its existence with javascript. You can’t easily perform this test using php, because, by the time you set the cookie everything is at the browser and php has finished performing its tasks - so without having to reload every page relying on Javascript is the easiest solution.
The right way to do it:
So, if all goes well, set a dummy cookie on the page requested using php or javascript.
Verify its existence with javascript once the page has loaded, then you know that the browser accepts cookies you can move onto the next step of checking whether the cookie exists that indicates the user has viewed the splash page.
If that cookie does not exist then send the user to your splash page to view.
This will in effect send users to your ’splash’ page the first time they visit your site. However, if the user’s browser has cookies or javascript turned off, or both, then they will never see your splash page. Which means search engine spiders will never see the splash page either, although some of them might know that the javascript redirect is there ;).
Download the Splash Page Script:
As you can see in the scripts I have used a Javascript Cookie Script from techpatterns.com which was incredibly simple to implement, as you can also see from the scripts that I like simple
Update:
The download was not working, the one below should now though:
Related posts:
No tags for this post.
Hello, I’m trying to download splash.zip and I’m getting a 404 not found. How do I download it? Thanks!
Hi Chris,
Cheers for pointing this out. I’ve fixed it up now, try the link at the bottom of the post. If you have any further problems with it let me know!
Enjoy!
Thank you! It works perfectly!
I am just wondering how does test.php come into play here?
Does that file contain code we should add to all the other pages of our site to constantly check for existence of the cookie? Or how are we supposed to set this up?
Hi Zen, Thanks for the question.
You have to put the code at the top of the test.php page onto every page you want to check whether the user has visited your splash page or not.
So, basically, pick the pages on your site which you think need the user to have seen the splash page first and put the code at the top of these.
Feel free to let me know how you get on or if you have any more questions.
Frank, thanks for the scripting lesson. I’m having the most difficult time making it work in IE. It’s as though the cookie isn’t even there. I put the script for the test.php page onto my index.php page. I’d like it to allow returning visitors 30 days before seeing the splash page again, but it goes directly to the splash page everytime in IE. What am I doing wrong?
http://www.trm.org is the site I’m using it on.
Hi Josh, Just make sure both cookies and javascript is enabled on your browser and it should all work fine. What version of IE are you using though?
I was trying to download the splash page script and it came up ERROR does it still work? franktank.com/blog/splash.zip/
Hi Rose,
Sorry, the first download link is not working, but the second one should still be all go. It is right at the bottom of the post under the ‘Update’ title.
Cheers,
Frank.
Is there a way to work this using html instead of php ?
In splash.php you write in the code comments that you set the cookie lifetime to one year, but in fact you are setting it to only half a year since you’re multiplying by 12 instead of 24 in your formula.
A more accurate (but not accurate still) formula for number of seconds pr year: 60*60*24*365
The number of days in a year is closer to 365.24 according to Google Calculator, but okay
By the way, here is the link for Google Calculator which had this piece of info:
Seconds in a year: http://www.google.no/search?q=seconds+in+year
Days in a year: http://www.google.no/search?hl=no&q=days+in+year
Thanks for a great script!
can it work if user turn on the firewall and the protection of antivirus software ?
So does this mean that the user will not see the website splash page if they do not have javascript or cookies enabled?
hi, i’m very new to all these. Do I change anything on the three files or just place them on my server and it’ll work… i keep getting error msg
“Notice: Use of undefined constant splash - assumed ’splash’ in /home/fhlinux170/1/1pounddvds.co.uk/user/htdocs/splash/test.php on
line 10″
Pls help.
Sorry for the delayed reply here but the blog has taken a bit of a back seat recently.
@hoangthienphuc
I don’t think firewalls and antivirus software should be an issue, unless javascript is disabled.
@Adaptiv Media
as long as javascript and cookies are turned off the splash page will not be displayed no - which is desirable so that the splash page is not displayed on every visit.
From memory
@Muhitul
No you shouldn’t have to change anything there mate, I’ll find some time to look through the code again and get back to you
This is a great piece of script when it works.
But I’m still having issues with the splash page.
when a user clicks on a link to another page from the splash page, it just sends them right back to the splash page, so they actually never leave the splash page.
Can you help?
thanks frank
thanks frank
@Treigh:
Apologies for the delay. I’ve had another brief look at the script, not sure how its going wrong for you.
Once a user visits your splash page the splash cookie should be set, if the user then navigates away from the splash page then the new page should only inject the javascript to send that user back to the splash page IF the splash page cookie is not set.
My advice would be to add the javascript to send a user to the splash page ONLY on the index page or main entry points to the site.
I know the reply is a bit late, but let me know how you got on. Does the page that the user is navigating to exist on the same domain? If not it may not recognise the cookie set…