i’m trying to do a cross-domain post but it isn’t working. What did i do wrong ?
[url]http://www.firstserver.com/somefile.php
[CODE] <script type=’text/javascript’ src=’//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js’></script>
<script type=”text/javascript”>
function dc(id){$.post(“http://www.secondserver.com/hits_downloads.php”,{file_id:id})}
</script>
$downloadlink = “somefile.zip”;
$base64link = base64_encode($downloadlink);
echo “<a href=”http://www.firstserver.com/$downloadlink” id=”$base64link” onClick=”dc(this.id)”>Download file</a>”;
[url]http://www.secondserver.com/hits_downloads.php
[CODE]switch ($_SERVER[‘HTTP_ORIGIN’]) {
case ‘http://firstserver.com’: case ‘http://www.firstserver.com’:
header(‘Access-Control-Allow-Origin: ‘.$_SERVER[‘HTTP_ORIGIN’]);
header(‘Access-Control-Allow-Methods: POST, GET, OPTIONS’);
header(‘Access-Control-Max-Age: 1000’);
header(‘Access-Control-Allow-Headers: Content-Type’);
break;
}
include(“db.php”);
$file_id = $_POST[‘file_id’];
$query = “SELECT * FROM 1downloads_hits WHERE base64 = ‘$file_id'”;
$res = mysql_query($query) or die(mysql_error());
$exist = mysql_num_rows($res);
if ($exist == 0) {
$base64decode = base64_decode($file_id);
mysql_query(“INSERT INTO 1downloads_hits (base64,hits,fichier)
VALUES (‘$file_id’,’1′,’$base64decode’);”);
} else {
mysql_query(“UPDATE hits SET hits = hits + 1 WHERE base64 = ‘$file_id'”);
}
The problem is that $_POST[‘file_id’] isn’t sent to the php script on the other domain. So when the php file try to update the hit counter, $file_id is empty. An empty entry is added to the database but “base64” row is empty (where $file_id is supposed to be added).