/    Sign up×
Community /Pin to ProfileBookmark

cross-domain POST

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[/url] :

[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>”;[/CODE]

[url]http://www.secondserver.com/hits_downloads.php[/url] :

[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'”);
}[/CODE]

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).

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @anarchoi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...