Is there a API or possiblity to identify in joomla if the user is a new visitor, who visits the page the first time or a defined period passed?

To be specific, the user is not registered joomla user, it is a simple anonymous visitor, who is visiting us the first time (never been before on the page) or who is visiting us again after lets say 30 days of inactivity.

So my question here, is there a joomla api to identify if the user is new or if the user has visited us already some time before?


4 Answers 4


Assuming you have a database table setup for this, you can use the following code to perform you check:

// This function is taken from my own extension
public function getIpAddress()
    $server   = $this->app->input->server;
    $clientIp = $server->get('HTTP_CLIENT_IP');
    $httpFwd  = $server->get('HTTP_X_FORWARDED_FOR');

    if (!empty($clientIp))
        $ip = $clientIp;
    elseif (!empty($httpForwarded))
        $ip = $httpForwarded;
        $ip = $server->get('REMOTE_ADDR');

    // If we don't have a valid IP address just return null
    if (filter_var($ip, FILTER_VALIDATE_IP) === false)
        return null;

    return filter_var($ip, FILTER_VALIDATE_IP);

// Get the users' IP
$userIP = getIpAddress();

// Check to see if the IP already exists in the database
$db = JFactory::getDbo();
$query = $db->getQuery(true);
    ->where($db->qn('ip') . ' = ' . $db->q($userIP));

$result = $db->loadResult();

// Check if the result is not empty
if (!empty($result))
    // We have a match, the users' IP already exists in the database
    // You can perform some action here
    // The IP doesn't exists in the database, let's add it
    $object = new stdClass();
    $object->ip = $userIP;

    $result = JFactory::getDbo()->insertObject('#__mytable', $object);

I've added a few comments in the code to show you what's happening, however note that I haven't tested it.

You will of course need to made some minor modifications to the code, such as changing the database table name.

Important: You will need to notify all users that you're storing their IP address for GDPR purposes

Hope this helps

  • Just to clarify for other readers: This answer does not represent a Joomla API for identifying a new or returning visitor. It's just a simple procedure that saves visitors IPs into the database and then checks for new users if their IP already exists. Do note that this is not a 100% reliable approach either for what the OP asked. What if 2 users with different devices come from the same IP? What when the IPs change?
    – FFrewin
    Commented Sep 12, 2018 at 5:46
  • It doesnt represent it but it does utilise Joomla's methods in a basic form. Of course it's not going to be 100% concrete. If 2 users with the same IP visit the site then you'll probably have to start looking further into geolocation :) Either way this is purely a base to start on
    – Lodder
    Commented Sep 12, 2018 at 10:27
  • Hey Lodder, the comment wasn't meant for you, but for readers that may come here searching for the same. The question is "Joomla API for identifying a new visitor" and your answer as stand-alone does not clarify, instead assuming things mentioned on the other answers - it looks more like a partial answer if someone decides to depend on IP recognition to define if a user is new or not. The questions in my comment about the possible IP issues meant for those readers as well and to question overall this approach. BTW, Geolocation wouldn't be able to help with this either. ;)
    – FFrewin
    Commented Sep 12, 2018 at 10:49

I went down this rabbit hole several years ago as a self-educating endeavor. You can go to the extent of leveraging a blend of server-side and client-side to sniff/frisk/detect a device.

The truth is that all data that you might wish to process:

  • can be spoofed/circumvented
  • can naturally fluctuate/vary enough to trip up your algorithm
  • can be missing depending on the device/browser

I mean, look, you can check:

  • IP address
  • UserAgent
  • Screen/Window Dimension
  • Platform
  • User Language
  • Plugins
  • ...and more

and you can get a "pretty good" fingerprint on your user, but you could never be 100% confident about the detection.

If you are trying to offer a courtesy (not issue an annoying popup if this is a repeat visitor), then you might go down this path.

In the end, I'd recommend that you take the more sensible approach and ask your users to sign up so that you can deliver a better User eXperience for them.

  • okay thank you for your insights on the areas. But is there any joomla provided API to get all these details? I need to have the same guess level like with FBAnalytics or GAnalytics
    – cilap
    Commented Sep 8, 2018 at 16:51
  • I am not aware of any pre-existing Joomla tools that can deliver this functionality, but I am rather new to Joomla and I haven't searched for this type of Joomla solution either. For my past project, I tried to accomplish my detection without the use of a COOKIE because they can be deleted/blocked by the user. If you go for the COOKIE, that can serve you well for great majority of cases, but depending on your use case, you could fallback to other techniques. Commented Sep 8, 2018 at 21:18

Just to clear a few things: I don't think there is anything that can tell this, not only in Joomla but in anything in the web. Under certain conditions you may be able to tell if a certain browser was at your site recently, e.g. like what G. Analytics does when counting new vs old visitors, which is a cookie based solution.

But there is no real accuracy on such metrics, so if you are trying to implement specific critical things based on that, simply you can't.

So with this in mind you need to clarify and narrow down or change your requirements on what you are trying to do, based on what is actually feasible.

Update for your comment:

I don't know exactly how Google handles it, but briefly from what I have read on the subject, the GAnalytics JS sets a cookie on the user's browser and assigns a unique id for that user which is also stored on Google's server. So in case this user returns, GA JS reads the cookie and knows that this user is a returning one.

I don't know for sure, but I am almost certain that there is nothing like that is provided by Joomla - and frankly I don't think Joomla needs to provide something like this.

But depending on what you are actually want to do, this can be as simple as setting a cookie in the user's browser.

  • basically I need the same "quality" on guessing the user like done on FBAnalytics or GAnalytics.
    – cilap
    Commented Sep 8, 2018 at 16:50
  • @cilap Updated my answer - not sure if it will satisfy you though.
    – FFrewin
    Commented Sep 8, 2018 at 17:22

Personally, I wouldn't put that pressure (checking if the visitor is new or not) on the server powering the Joomla website as I've seen the havoc that a custom made analytics can wreak on a Joomla website. I would throw it on Google Analytics as described here.

  • 1
    good idea, but what if the user tells google (like safari as default) do not track and google is correctly not providing this details to you?
    – cilap
    Commented Sep 12, 2018 at 13:38
  • @cilap That is possible - but I don't think it's very common. On the positive side of things, the user will not feel tracked on your website as well, which is possibly what they want.
    – itoctopus
    Commented Sep 12, 2018 at 15:32

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.