Вопрос

Is there any way of knowing, when a user comes to my website, if he has already come, without having users to login ? I do not need it to be extremely accurate, but to be simple to implement and to work in most cases. I thought of using a ip address+useragent combo. What do you think about it ?

Это было полезно?

Решение 2

Set a cookie on every user's visit on your site, If the client has that cookie it means he has already visited your site else he has'nt or it could be also checked by IP

A quick script using Cookies:

if(isset($_COOKIE["user_visited"]))
{
    //He has already visited you.
}else{
    //Set a new cookie.
    setcookie("user_visited","user");
}

IP Method

//Need to create a user_ip table for it and store everybodies ip(s) in it.
$user_ip    =   $_SERVER["REMOTE_ADDR"];
if(isset($user_ip)){
    $query  =   mysql_query("SELECT * FROM `user_ip` WHERE `ip`='$user_ip'");
    $count  =   mysql_num_rows($query);
    if($count > 0){
        //User already visited you
    }else{
        //He hasn't visited you.
        //Save his IP to know if he visit again.
        $query("INSERT INTO `user_ip` VALUES('$user_ip')");
    }
}

Другие советы

Sure, this is what cookie are there for. Cookies are independant of an authentication. Just have a cookie stored when your pages are first accessed. Whenever a request comes with an already existing cookie you can be sure that is not a first time visit.

Note however that cookies can be deleted or rejected by clients. Also many clients treat cookies automatically as session cookies, so the cookies are automatically removed the moment the browser is closed.

The simplest way is to use cookie for it. Just add cookie like this:

if ( !isset( $_COOKIE['visited'] ) {
    // user is newbie
    setcookie("visited", '1', time() + 365 * 3600); // set cookie for 1 year
} else {
    // user has already visited your site
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top