How to Create a Robust Splash Page

by frank on November 3, 2006

in Featured, PHP

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:

  • Set a cookie on the ‘splash’ page using say: setcookie() that doesn’t expire for a very long time
  • Check on every other page if the cookie exists on the users browser, if not, send them to visit your splash page

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, however, it should now.

Was this article useful?

rss feed icon

Email this article to yourself or...

rss feed icon

Subscribe to the RSS feed for more useful articles and tips.

Share this article with others

  • del.icio.us
  • Twitter
  • Reddit
  • StumbleUpon
  • Facebook
  • Digg
  • Chris

    Hello, I’m trying to download splash.zip and I’m getting a 404 not found. How do I download it? Thanks!

  • http://deadlytechnology.com franktank

    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!

  • Chris

    Thank you! It works perfectly!

  • Zen

    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?

  • http://deadlytechnology.com franktank

    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.

  • http://www.ainsworthstudio.com Josh

    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.

  • http://deadlytechnology.com franktank

    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?

  • http://n/a Rose Marie

    I was trying to download the splash page script and it came up ERROR does it still work? deadlytechnology.com/splash.zip/

  • http://deadlytechnology.com franktank

    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.

  • dave

    Is there a way to work this using html instead of php ?

  • http://torkiljohnsen.com Torkil Johnsen

    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 :)

  • http://torkiljohnsen.com Torkil Johnsen

    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!

  • hoangthienphuc

    can it work if user turn on the firewall and the protection of antivirus software ?

  • http://www.adaptivmedia.com/ Adaptiv Media

    So does this mean that the user will not see the website splash page if they do not have javascript or cookies enabled?

  • Muhitul Haque

    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.

  • http://deadlytechnology.com franktank

    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
    From memory :-) 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.

    @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

  • http://www.assonsol.org Treigh

    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?

  • Muhitul Haque

    thanks frank :-)

  • Muhitul Haque

    thanks frank

  • http://deadlytechnology.com franktank

    @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…

  • http://www.theskipper.ca Ayton

    Hi Frank, thanks so much for this code! It is exactly what I am looking for. Not sure if you still check these comments or not (been a while since the last post it seems), but was wondering if you can help me with the problem I have been having getting this to work properly. Basically it seems as though the cookie isn’t being set on the splash page (http://www.theskipper.ca/landing.php), and so when a visitor goes to the main page (http://www.theskipper.ca/index.php) they keep getting redirected to the splash each time. Everything else seems to work fine (i.e. if cookies are turned off, user stays on main page; and the redirect to the splash page is working just fine). Any idea what might be causing this issue?

    Thanks!

    Ayton

  • http://deadlytechnology.com franktank

    Hi Ayton,

    I would make sure that the cookie is not being set first of all, you can check cookies with the developer toolbar in ffx if you didn’t know so already. Then, if they are not being set I guess there could be many reasons – cookies must be set for the domain that the script is being served on so maybe that is an issue?

    HTH

  • http://www.brandcake.us David

    Frank,

    Same issue as Ayton above. one is unable to get out of the splash page and to the index becuase the link keeps sending them back to the splash.

    Thanks
    David

  • http://deadlytechnology.com franktank

    Hmm, not sure what to say here guys. The script seems to work for me on my local server, I’ve just dug it up and given it a dust off. If you are getting redirected to the splash page all the time it seems that cookies are enabled on the browser, and javascript is enabled – but the splash cookie has not been set.

    What I would do is strip it back to the example here and run that on your server. After visiting the splash page, before clicking on the link have a look at the cookies stored via the web developer toolbar or something similar. If it has been set then the index page should not include the javascript to test for cookies and then redirect the browser if cookies are enabled…

    Get back to me with versions of php you are using if you like, that might help. Also, make sure you are not caching anything such as the index page…

    HTH

  • Dave Berutich

    Hello, excuse me if this question is out of place. I’m being asked by a client to create a “Pearl Splash Page” in his words. I am assuming this means a Pearl scripted splash page. Does this example qualify in your opinion. I’m a designer with moderate html, css, java experience…. thanks kindly for any direction you can point me.

  • http://deadlytechnology.com franktank

    Hi Dave, I don’t know how you would code this in Perl but yes, I think if you followed the same technique that should see you right. HTH.

  • http://evenayr.com Mike

    I had the same problem with the splash page till I went through and set all the domain settings for each of the setcookie options to my localserver… and edited the same settings for when I posted it online.

  • http://quietcitydesign.com Len Marshall

    Hey Frank
    I hate to join in the ugly mob, but I can’t get this script out of the “Test–Splash” loop either! I haven’t touched the code from the download. In FFX 3, I see nothing when I check the cookie information in the dev-bar. I’d paste all the code here, but it’s the same as the download (collected on May 9 ’09). Browsers affected: FFX3, IE7, Opera 9. Please help us, Obi-Wan…

  • frank

    This problem still puzzles me a bit. To be sure I downloaded the zip myself and gave it a try – one problem I had was with the permissions of the files. Make sure the files have the right permissions in order to set the cookies and run properly. Have a look at the other files on your server to get an idea of permissions.

    HTH!

  • Len Marshall

    Hi Frank (and anyone else following)
    I went back to the start, and uploaded the files to a test server on the web… VIOLA! It works. I can only assume that testing on my localhost was the problem. If anyone is experiencing the same loop problem, try testing it on a remote host server.
    By the way, for interest’s sake, what should the permissions be for these files?
    Furthermore… a big request… How can I use this script to fire something like Thickbox or Shadowbox – overlay full screen and server the webpage that way?
    Thanks for your support!

  • Daniel

    Hello Frank,

    Is this script not available anymore? I’m getting a 404 error when I download it.

  • frank

    Hey Daniel, yes one of the download links was not working. I have tidied the post up a bit now so only the one working download link is available – should all work for you now enjoy :-)

  • http://www.designdelux.com Mark Mitchell

    Frank,

    This works great! Thanks It was exactly what I was looking for.

  • vitali

    Hi thank you for script. It is perfect. Just one question. Is it possible make such thing.
    when user visit website first time, he see Splash page (with choice go to website or get out from website). If he choose go to website than he will be redirected to main page (this part is clear for me), but if user choose get out from website than all cookies will be deleted and in next visit user again will see splash page.

  • vitali

    one more thing in Splash page I tried to use such structure of Get Out link
    Get Out

  • vitali

    I mean I tried to run function onClick Delete_Cookie

  • Spitfire

    For anyone stuck in the loop problem, you need to add your domain into the setcookie values on the spash.php page.

    eg:
    setcookie(‘splash’, true, time()+(3600*12*7*52), ‘/’, ‘yourdomain.com’);