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.

6
  • ip != user, never has, never will. 1 IP can many people, one person can be many IP addresses
    – user557846
    Commented Jul 14, 2013 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. Commented Jul 14, 2013 at 21:55
  • @Dagon I know, but this website is simple and I really don't want to setup registrations for it. Commented Jul 14, 2013 at 21:56
  • @SurrealDreams I have no idea how to use cookies Commented Jul 14, 2013 at 21:57
  • then don't bother doing this, its meaningless if your not going to do it properly
    – user557846
    Commented Jul 14, 2013 at 22:00

1 Answer 1

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

2
  • 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. Commented Jul 14, 2013 at 22:05
  • Will dropping another cookie called id with the value from your database solve that problem? Commented Jul 14, 2013 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.