Query execution after a few seconds

2

I have a script that logs all page views for my site that I've uploaded, and it's working fine. But now there are some issues with GOOGLE visitation, and there is "a lot", more "much" visitation of Google Robots and it is not legal to have a WebSite visitation base.

$db->query( "INSERT INTO visitas (ip_address,page_location,date_visitation,visitor_browser,visitor_plataform,visitor_city,visitor_region,visitor_country) VALUES ('$ip','$page_location','$date_visitation','$visitor_browser','$visitor_plataform','$visitor_city','$visitor_region','$visitor_country')" )->fetchAll();

As far as I know, the google robot does not spend more than 5 seconds on a page on the site.

I'd like to have this INSERT run after 10 or 15 seconds. It would be possible? or would you have another solution to this situation?

Thanks for the support.

EDIT 1

The need for only the function setCounterVisitor () to run after 15 seconds, because my script is required on another page. It is important that to function to come it is on the same page and not on visitor.php.

<?php 
require_once 'app/visitor.php'; 
setCounterVisitor();
?>

Is it possible to run the setCounterVisitor (); after 15 seconds on the loaded page?

    
asked by anonymous 21.05.2018 / 23:08

1 answer

0

Call a function within setTimeout , passing the delay time it should have (in milliseconds), this function will make an asynchronous call to the php file that will execute the insert

setTimeout(function() {
    httpRequest = new XMLHttpRequest();
    httpRequest.open('GET', 'nomedoarquivo.php');
    httpRequest.send();
}, 15000);

If you're already using JQuery in your code, you can use its ajax function. Another option is the api fetch

I'm not sure if this is what you want but you can pass a variable through the url and in the visitor.php check if it exists, if yes run the function

In javascript change the url of the xhr:

httpRequest.open('GET', 'visitor.php?exe=true');

In php it checks to see if it exists and calls the function:

if($_GET["exe"]) {
    setCounterVisitor();
}
    
22.05.2018 / 02:19