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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题