私は、このコードがほとんど常に機能していることを発見しました。カスタムポストデータを使用して転送したい場合や、クライアントがJavaScriptをサポートしていない場合を除きます。
307 Temporary Redirect
は、POST
のデータを転送しているように見せかけるか、または自己提出のjavascriptフォームを作成することによって行われます。
これはハックですが、POSTデータを転送しなければならない場合にのみ使用してください。
<?php
function redirectNowWithPost($url, array $post_array = NULL)
{
if(is_null($post_array)) { //we want to forward our $_POST fields
header("Location: $url", TRUE, 307);
} elseif(! $post_array) { //we don't have any fields to forward
header("Location: $url", TRUE);
} else { //we have some to forward let's fake a custom post w/ javascript
?>
<form action="<?php echo htmlspecialchars($url); ?>" method="post">
<script type="text/javascript">
//this is a hack so that the submit function doesn't get overridden by a field called "submit"
document.forms[0].___submit___ = document.forms[0].submit;
</script>
<?php print createHiddenFields($post_array); ?>
</form>
<script type="text/javascript">
document.forms[0].___submit___();
</script>
<?php
}
exit();
}
function createHiddenFields($value, $name = NULL)
{
$output = "";
if(is_array($value)) {
foreach($value as $key => $value) {
$output .= createHiddenFields($value, is_null($name) ? $key : $name."[$key]");
}
} else {
$output .= sprintf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />",
htmlspecialchars(stripslashes($name)),
htmlspecialchars(stripslashes($value))
);
}
return $output;
}
セッション変数に 'POST'データを入れます。 –
私は 'echo method = 'POST' action = '...' ...> ..."と書くだけですが、もっと良い方法があれば私は不思議です。 – JiminP