0

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?

BeFa
  • 1
  • 2
  • 5

1 Answers1

0

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.

Community
  • 1
  • 1
Thanasis1101
  • 1,614
  • 4
  • 17
  • 28
  • Verry verry thanks, but here is small problem... Ad code can not be changed. Where it is modified, advertising does not work. What now? – BeFa Oct 30 '16 at 13:36
  • I have an idea. Try to add another div outside the div for the ad, and use that div in the javascript. Eg `
    ` and in the javascript replace the onclick function with this `document.getElementById("clickDiv").onclick = function() {...};`. Let me know if it worked or not.
    – Thanasis1101 Oct 30 '16 at 13:52
  • So it should look like this: `
    ` True?
    – BeFa Oct 30 '16 at 14:10
  • You forgot to close the first div. It is `
    `. The rest of it is right.
    – Thanasis1101 Oct 30 '16 at 14:17
  • Ads show, bud click on database not save... when i go on to insert.php so ip save in database. But when i click on ads ip not save – BeFa Oct 30 '16 at 14:26
  • Inside the onclick function, in the beginning add this line: `alert('something');`. Then click on the ad. If after clicking on the ad, there is not a message in your browser, it means that the onclick function doesn't trigger (this is a problem). If there is a message, it means that the request for insert.php dosn't work (not a big problem). Tell me the result (if the message appeared or not). – Thanasis1101 Oct 30 '16 at 14:47
  • So this...? `
    `
    – BeFa Oct 30 '16 at 14:57
  • Yes. Test it and tell me – Thanasis1101 Oct 30 '16 at 14:58
  • I found something that worked when I tested it. I'm posting it as an edit in a few minutes. See it and tell me if it worked. – Thanasis1101 Oct 30 '16 at 16:17
  • So what please..? – BeFa Oct 30 '16 at 17:52
  • I posted it. It is under the EDIT in the answer. – Thanasis1101 Oct 30 '16 at 17:54
  • @BeFa What happened? Did it work? Please let me know. I am very interested to know if this was helpful for your problem. – Thanasis1101 Oct 30 '16 at 19:20
  • Sorry but not work :( Give me please your mail? I give you FTP – BeFa Oct 30 '16 at 20:03
  • thanasis5432@gmail.com – Thanasis1101 Oct 30 '16 at 21:59