Query execution after a few seconds

Asked

Viewed 113 times

2

I have a script that records all the views of the pages of my site that I downloaded, and it’s working very well. Only now some problems have arisen in what it comes to GOOGLE visitation, and has "a lot", more "a lot" visitation of the Google Robots and is not cool 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 doesn’t spend more than 5 seconds on a page of the site.

I would like to make this INSERT run after 10 or 15 seconds. Would it be possible? or would there be another solution to this situation?

Grateful for the support.

EDIT 1

The need arose to only the Function setCounterVisitor() run after 15 seconds, due to my script being required on another page. It is important that to work Function it comes is on the same page and not in Visitor.php.

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

It is possible to execute the setCounterVisitor(); after 15 seconds on loaded page?

  • put the code in a php file and call it with ajax, in this ajax do a setTimeou

  • Could you give me an example? the code is already in a separate PHP file. It is calling in a requere_once.

1 answer

0

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

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

If you are already using Jquery in your code you can use its function ajax. Another option is the fetch


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

In javascript change the xhr url:

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

In php checks if it exists and calls the function:

if($_GET["exe"]) {
    setCounterVisitor();
}
  • Very good! It worked. Only now there’s a new surprise in my script. Is it possible to do this to run a Function in php with this same method? Since Function which is "setCounterVisitor()" should be executed on the same page and not picked up via a url. It is possible?

  • Can you edit the question with an example? I don’t quite understand

  • I edited, please see !

  • I edited the answer see if it solves the problem

  • It gave the following error when running $_GET["exe"] from setCounterVisitor(); ----- Fatal error: Uncaught Error: Call to Undefined Function setCounterVisitor() in C: Appserv www site header.php:18 Stack trace: #0 C: Appserv www site main pages.php(16): include() #1 C: Appserv www site index.php(16): include('C: Appserv www ...') #2 {main} thrown in

  • The function setCounterVisitor does not exist in the php file named in XHR or is inside a class, import the function with the require or instate the class and call the function by its instance

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.