Enable PHP page with jquery

0

We get a system from which all pages are in HTML. Only the client wants each person who accesses your site, information such as IP, date and time to be stored in a database. On storage all right, just how could you do that when accessing the site, jquery activated the page in PHP that does this registration? If we have to change index.html through index.php, it would give more work, because its site has several pages and we would have to change the link in all of them.

    
asked by anonymous 14.09.2017 / 22:29

2 answers

1

According to this site this function in jquery executes some command when loading a page

// A $( document ).ready() block.
$( document ).ready(function() {
   console.log( "ready!" );
});

And according to this other site this function sends a POST:

$.ajax({
   type: "POST",
   url: url,
   data: data,
   success: success,
   dataType: dataType
});

Putting together the two ideas above, in a very simplified way:

$( document ).ready(function() {
   // manda a requisição para o PHP aqui.   
   $.post( "paginaQueSalvaDadosNoBanco.php", { ip: "192.168.1.1", hora: "02:00" } );
});

Done, putting this in your index.html solves the problem: D

    
14.09.2017 / 22:54
0

Create a file called log.js or something like this and put it in the "root" of the public folder ( public_html or www , depends on your system)

Then create a file called log.php also in root

Now in log.js add this:

  

note: (new Date().getTime()) is only to avoid caching

(function (d) {

    function trigger() {
        var oReq = new XMLHttpRequest();

        //Defina como true
        oReq.open("GET", "/log.php?_=" + (new Date().getTime()), true);

        //Envia a requisição, mas a resposta fica sendo aguardada em Background
        oReq.send(null);
    }

    //Verifica se a página já foi carregada
    if (/^(interactive|complete)$/i.test(d.readyState)) {
        trigger();
    } else {
        d.addEventListener('DOMContentLoaded', trigger);
    }

})(document);

No log.php add to your mysql statements:

<?php

//Conecta
$mysqli = new mysqli("localhost", "usuario", "senha", "banco");

if (mysqli_connect_errno()) {
    printf("Erro de conexão: %s\n", mysqli_connect_error());
    exit;
}

$ip = $_SERVER['REMOTE_ADDR'];

$result = $mysqli->query('INSERT INTO 'logs' ('data', 'ip') VALUES (now(), \'' . $ip . '\')');

if ($result) {
    $result->close();
}

$mysqli->close();

Now on all HTML pages add

<script src="/log.js"></script>
    
14.09.2017 / 22:53