dongyang5716 2013-12-22 01:25
浏览 72
已采纳

如果使用mod_rewrite,使用POST发送的变量是否会发送到PHP?

How do sites such as Stack Overflow submit forms using action="/questions/ask/submit"?

I would've thought mod_rewrite would lose the $_POST vars?

.htaccess

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L]

index.php

$q = explode("/", $_SERVER['REQUEST_URI']);
if($q[1]!=""){
  switch($q[1]){
    case "test":
      include("test.php");
      break;
      ...

test.php

<?php
  if(isset($_POST["submitButton"])){
    echo "Submitted";
  }
  else{
    echo "Not submitted";
  }
?>
<form method="post" action="/test/submit">
  <input type="submit" name="submitButton">
</form>

If I remove action="/test/submit"

If my URL is /test then it returns Not submitted when I click the button.
If my URL is /test.php then it returns Submitted when I click the button.


Update

For the time being, I'm using the following.

index.php

if($_SERVER['REQUEST_METHOD'] == 'POST'){
  $q = explode("/", $_POST["url"]);
}
else{
  $q = explode("/", $_SERVER['REQUEST_URI']);
}
...

test.php

<form method="post" action="/">
  <input type="hidden" name="url" value="/test">
  ...

This allows my test.php to receive the post vars.

If there are no errors by the user, I use header("Location: test/success");

If there are errors, the URL will have to be / which isn't ideal.

Update/resolution

The problem may be with Apache 2.4. The fix was to add this line to index.php:

parse_str(file_get_contents("php://input"),$_POST);

With this method, there's no need for any action="..." (this will successfully POST to itself, even if the URL is a slug).

  • 写回答

2条回答 默认 最新

  • dongma2388 2013-12-22 01:34
    关注

    mod_rewrite does not affect post variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题