简体   繁体   中英

PHP Cookie page counter for each User

I'm implementing a cookie counter for my website. I set the cookie to count the number of visit every time the user log in, and display it somewhere on the website. here is the code for setting the cookie:

$login = login($username, $password);
    if ($login === false) {
                    header('location:login.php');
        $errors[] = 'The username/password combination is incorrect';
    } else {                    
                    if (!isset($_COOKIE['counter']))
                            {  
                                $cookie = 1;
                                setcookie("counter", $cookie);
                            }
                     else
                            {
                                 $cookie = ++$_COOKIE['counter'];
                                 setcookie("counter", $cookie);
                             }
                    setcookie("user",$login,time()+24*3600);
                    $_SESSION['user_id'] = $login;  
                    header('Location:Index.php');                        
    exit();

As you can see I have 2 cookies. It works fine, but when I try to log in with another user account, the counter keeps counting the number of visit. What I want is actually to display the number of visit for each user account. Does anyone have an idea?

Give the cookie a unique name that represents the logged in user ID.

eg if $_SESSION['user_id'] contains the user id, you could do...

setcookie("counter_".$_SESSION['user_id'], $cookie);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM