how to enable cross domain POST-ing in PHP?

泄露秘密 提交于 2020-01-21 06:27:27

问题


I'm tying to send POST data from one site to another (both sites have been developed by us). The problem is that the POST variables are not available if the page is requested from another domain. Even if I test it locally, but specify the complete url, the POST data is gone.

So, this will work:

<form method="POST" action="test.php">

But this will not:

<form method="POST" action="http://example.com/test.php">

Here is the HTML for the page:

<html>
<head>
    <title></title>
</head>
<body>
    <form method="post" action="http://example.com/test.php">
        <input type="text" name="request" value="" id="" />
        <input type="submit" value="" id="" />
    </form>
</body>
</html>

After the comments I got that this should work, I tested it on another server and there everything worked fine indeed. This might have something to do with the fact on the first server https is enabled. But if this is the case, I find it strange that I do get information back but that just the POST data has gone missing.


回答1:


What you have should work fine - forms came before the same-origin policy - you're allowed to submit to different domains.

If I were to hazard a guess, I'd say there's a 301 or 302 redirect getting in there somehow? Follow the HTTP headers with Firebug for example to be sure.




回答2:


As others have said, there should be no problem doing this. I would suggest replacing your test.php script with something simple, like this

<?php
echo "<pre>";
var_dump($_POST);
echo "</pre>";

You should find it works, which means the blame lies somewhere in the "real" script...




回答3:


Maybe also a timesaver:

If you POST to domain.com make sure it doesn't get redirected to www.domain.com. The redirect doesn't take the POST variables into account , only GETvariables.

If it does get redirected to the www.domain.com add the www. in your POST-action




回答4:


Thank you. I too found out that redirection to www and https was blocking the performance of my $_POST request. By changing my action to include https://www. I fixed my problem. Thank you



来源:https://stackoverflow.com/questions/1324829/how-to-enable-cross-domain-post-ing-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!