0

I have already managed to store the visitors IP address and assign them an ID (1, 2, 3, etc.) and I want to display different messages to them. The code I have so far is this:

function DisplayWelcomeMessage() {
    $checkUserIDExists = mysql_query("SELECT * from Information where id = '$myid'");
    if(mysql_num_rows($checkUserIDExists) < 0) {
        return '<div class="Message">New visitor message</div>';
        } else {
        return '<div class="Message">Returning visitor message</div>';
        }
}

When I use this code, it always displays the returning visitor message.

Shivam Amin
  • 75
  • 1
  • 1
  • 7
  • ip != user, never has, never will. 1 IP can many people, one person can be many IP addresses –  Jul 14 '13 at 21:54
  • An alternate approach to this is to set a cookie. It simplifies things because no database is required. A user may clear cookies, but then again, they may not have the same IP address every time either. – Surreal Dreams Jul 14 '13 at 21:55
  • @Dagon I know, but this website is simple and I really don't want to setup registrations for it. – Shivam Amin Jul 14 '13 at 21:56
  • @SurrealDreams I have no idea how to use cookies – Shivam Amin Jul 14 '13 at 21:57
  • then don't bother doing this, its meaningless if your not going to do it properly –  Jul 14 '13 at 22:00
  • Check out [setcookie](http://php.net/manual/en/function.setcookie.php). If you can do database queries, you can set and check a cookie. – Surreal Dreams Jul 14 '13 at 22:00

1 Answers1

1

Probably the easiest thing to do would be to set a cookie t track if they have visited the site before.

setcookie("FirstVisit", '1');

Then your welcome method would then become something like this:

function DisplayWelcomeMessage()
{
    if (isset($_COOKIE['FirstVisit']) && $_COOKIE['FirstVisit'] == 1)
    {
         // Display a welcome message

         // Update the cookie so that they don't get this message again
         setCookie("FirstVisit", "0");
    }
    else
    {
        // Do something different for people who have visited before
    }
}

You can lookup the documentation for setCookie here

  • Thanks, I'd use this but I need to give them an ID due to another part of my website, and I thought I would just use the same database to know if they visited before or not. – Shivam Amin Jul 14 '13 at 22:05
  • Will dropping another cookie called __id__ with the value from your database solve that problem? – Charles R. Portwood II Jul 14 '13 at 22:06