haheute
haheute

Reputation: 2199

use php session to count image views?

I want to count image views on my website. Until now, I set a cookie that contains the last 20 viewed images like 1254.12.963.4328.32 and so on.

This is to prevent multiple counting if somebody presses F5 / reloads the page. Also I don't want to count crawlers and spiders. (I somewhere read that bots won't set this cookie, but I don't know)

Would I count only real users, when I use the session id and save an array with the viewed image-ids in the session? I use laravel with database session driver. And how much data can be stored per session?

Upvotes: 1

Views: 333

Answers (3)

bestprogrammerintheworld
bestprogrammerintheworld

Reputation: 5520

Would I count only real users, when I use the session id and save an array with the viewed image-ids in the session? No. I guess the answer is no to question if you would count real users because you would have to require some action to verify that it is a real user. That's why - for example captchas are used when submtting forms.

However, you could narrow it down though so you could check "who's" browsing your page. You've got a great answer from Lea Tano which I beiieve would give you a hint where to start.

And how much data can be stored per session? Sessions are (by default) stored to the server, so the actual limits is determined by PHP's memory_limit() and diskspace (on the server).

Upvotes: 1

Leandro Papasidero
Leandro Papasidero

Reputation: 3738

This is a home-made way to do that. Of course, there are some libraries that do a better job than this. However, this is a good start. Specially, if you want to do it yourself.

<?php
if (!$_SESSION['counter'] || !isCrawler()) {
    $_SESSION['counter'] = 'whatever-counter';
}


function isCrawler() {
    $crawlers = array('googlebot'); //add more crawlers agents
    foreach ($crawlers as $crawler) {
        if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), strtolower($crawler))) {
            return true;
        }
    }
    return false;
}

Upvotes: 1

Wojtek B
Wojtek B

Reputation: 170

Probably you want to determ by browser.

See in to this project which provide browser list and documentation how to catch it.

BrowsCap

Upvotes: 1

Related Questions