Page 1 of 1

Showing message on first visit only, need help...

Posted: Mon Jun 14, 2010 12:28 pm
by WithHisStripes
Heya,
So I need to display a message to first time visitors (even if they close their browser and come back a day later) but I'm not sure how to do this and I have scoured Google for the solution to no avail. Can someone help? Thanks!

Re: Showing message on first visit only, need help...

Posted: Mon Jun 14, 2010 12:48 pm
by AbraCadaver
Set a cookie with an infinite or very long lifetime and then check for this cookie.

Re: Showing message on first visit only, need help...

Posted: Mon Jun 14, 2010 6:26 pm
by WithHisStripes
I don't see how that solves my problem. Can you elaborate a little and provided an example? Thanks!

Re: Showing message on first visit only, need help...

Posted: Mon Jun 14, 2010 7:35 pm
by JakeJ
It's highly advisable that you look up a cookie tutorial. That will answer all of your questions and it's full of examples.

In a nutshell though, a cookie resides on the user's computer. When the user visits your page, your site checks for the existence of the cookie. If it's there, the message does not display. If it's not there, the message you want displayed the first time appears. Problem solved.

Re: Showing message on first visit only, need help...

Posted: Mon Jun 14, 2010 7:37 pm
by WithHisStripes
Hey Jake, thanks for the reply. I probably should have mentioned that I've followed several cookie tutorials (JS and PHP) thus far and haven't been able to figure it out. So if someone can give me an example now that you've explained it I would really appreciate it because I'm stuck. Thanks!

Re: Showing message on first visit only, need help...

Posted: Mon Jun 14, 2010 11:10 pm
by JakeJ
Here's an example, it should work.

Code: Select all

<?php
start_session();
set_cookie('myCookie','visitCheck', time()+60*60*24*30*12, '/','.yourdomain.com',false, true); //cookie set to one year

//here's the test
If (!isset($_COOKIE['myCookie'])) { //if the cookie is NOT set, then display the block of code. if it is set, you don't need to do anything.
  //display something
}
?>