0

okay so I have this link

<a id="link" href="https://yahoo.com" target="blank">Link</a>

then i have this script:

    var security = function() {
        var link = $('#link').attr('href');
        $.getJSON('http://myweb.com/func.php',function( result ) {
        if ( result % 5 === 0 ) {
            $('#link').attr("href", link);
            alert('his link');  
            } else {
            $('#link').attr('href', 'https://google.com');
            alert('your link');
        }
        });
        $("#link").click(function() {
        $.getJSON('http://myweb.com/func2.php',function( results ) {
            if ( results === results ) {
            location.reload();
            }
        });
        }); 
    };

func.php:

$results = mysqli_query($con,"SELECT * FROM `c_clicks`");
while ( $row = mysqli_fetch_array( $results ) ) {
   $clicks = $row['id'];
   echo $clicks;
}

func2.php:

$results = mysqli_query($con,"INSERT INTO `c_clicks`(`link`,`date`) VALUES('claim',now())");

What I'm trying to accomplish is that for every 5 clicks the #link will send the user to another domain. The thing is it works fine but if someone rips my website and switches the #link href func.php and func2.php are no longer accesible so they dont work. I tried fixing it with JSON but im guessing its wrong. How could I still perform func and func2 through a different server?

anderZubi
  • 6,414
  • 5
  • 37
  • 67
Rodrigo Lessa
  • 69
  • 3
  • 11

3 Answers3

0

There are some links which will be helpful to you ..

For current day browsing have a look at this link http://www.w3.org/TR/access-control/

Then also you can find some useful snippets here jQuery and Cross Domain POST Requests

And finally have a look at this blog http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/

Hope this all helps you ..

Community
  • 1
  • 1
Vaibs_Cool
  • 6,126
  • 5
  • 28
  • 61
0

To make cross-domain requests in a browser you need to take a look at CORS. CORS, or Cross-origin Resource Sharing allows you to request files using XMLHttpRequest, which is what $.getJSON uses, from domains other than the one that is serving the current page. MDN has a great writeup on CORS: https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS

Specifically, in your case, you'd want to take a look at CORS with php headers where 'slashingweaponæ have set up a PHP script to allow requests from other domains.

Community
  • 1
  • 1
bjornl
  • 1,757
  • 3
  • 17
  • 29
0

Add this to the top of each php file:

header('Access-Control-Allow-Origin: *');

This will let you know very quickly if it is a CORS issue.

Steve Papa
  • 422
  • 2
  • 9