点击javascript广告PHP

Already a few days to deal with droughts and problem and can not solve it Completely. I need to count clicks on ads. When someone click on ads, save ip and date on database...

Ad code looks like:

<!-- Kontextová reklama Sklik -->
<div id="sklikReklama_12112"></div>
<script>
    var sklikData = { elm: "sklikReklama_12112", zoneId: "12112", w: 468, h: 60 };
</script>
<script src="//c.imedia.cz/js/script.js"></script>

When somebody click on ads, i need save ip and date on table ips for column ip, date...

IP know this:

$ip_uzivatel = $_SERVER['HTTP_X_FORWARDED_FOR']
//uložení ip do db
$ipp = "'$ip_uzivatel'";

time in this format:

$t=time();
$time=(date("Y-m-d",$t));
$times = "'%$time%'";

save ip:

INSERT INTO `d130729_20`.`ips` ( `id` , `ip` , `time` ) VALUES ( NULL , '.$ipp.', '.$times.' );

So how do I know if someone clicked on the ad and Then I save the data in database, please?

This can be achieved using javascript. Create an event in javscript that triggeres when you click on the div with id="sklikReklama_12112", and sends a post request to another php script (insert.php) that does the insertion in the database.

So your code would be like this:

<div id="sklikReklama_12112" style="width:468;height:60;"></div>

<script src="//c.imedia.cz/js/script.js"></script>

<script>

    var sklikData = { elm: "sklikReklama_12112", zoneId: "12112", w: 468, h: 60 };

    document.getElementById("sklikReklama_12112").onclick = function() {

        var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
        xhr.open( 'post', 'insert.php', true );
        xhr.send();

    };

</script>

Then in the new insert.php add the code that handles the sql query like this:

<?php

$db = new mysqli("YOUR_HOST", "YOUR_USERNAME", "YOUR_PASSWORD", "YOUR_DATABASE");
mysqli_set_charset ( $db , "utf8" );

$ip_uzivatel = $_SERVER['HTTP_X_FORWARDED_FOR'];
//uložení ip do db
$ipp = "'$ip_uzivatel'";

$t=time();
$time=(date("Y-m-d",$t));
$times = "'%$time%'";

$db->query("INSERT INTO `d130729_20`.`ips` ( `id` , `ip` , `time` ) VALUES ( NULL , '.$ipp.', '.$times.' );");

?>

The insert.php will not appear in the user, it will just insert the data in your database. I hope this works and solves your problem. Please let me know !

EDIT

Since the onclick function doesn't wrok (apparently) because inside the div for the ad there is an iframe, I found a solution in this stackoverflow answer that I tested and worked. So, after some modifications in order to adjust it on your problem, here is the code you need:

<div id="sklikReklama_67864"></div>
<script> 
    var sklikData = { elm: "sklikReklama_67864", zoneId: "67864", w: 468, h: 60 };
</script>
<script src="//c.imedia.cz/js/script.js"></script>

<script>

var monitor = setInterval(function(){
    var elem = document.activeElement;
    if(elem && elem.tagName == 'IFRAME'){
        elem.blur();
        var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
        xhr.open( 'post', 'insert.php', true );
        xhr.send();
    }
}, 100);

</script>

What this code does, is checks every 100 millisecond (the '100' in the last line, you can change it if you want) where the focus is. If the focus is on an iframe, it means the user clicked on the ad, and then sends the request to the insert.php script, in order to execute the database query. The insert.php remains as it was, since it works fine. Please let me know if it worked.