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

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
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

Set a cookie with an infinite or very long lifetime and then check for this cookie.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post by WithHisStripes »

I don't see how that solves my problem. Can you elaborate a little and provided an example? Thanks!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

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

Post 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.
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post 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!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

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

Post 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
}
?>
Post Reply