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

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题