使用表单变量重定向无法正常工作

时间:2014-10-28 10:56:01

标签: php .htaccess

我有文件(search.php),它使用收到的变量,如下所示:

session_start();
if (isset($_POST['Search'])){
    $_SESSION['Search'] = $_POST['Search'];
}

表格:

<form id="searchbox" action="/search.php" method="post">
      <input id="search" name="Search" type="text" placeholder="Search Products">

  <input id="submit" type="submit" value="Search" ></form>

所以它提交给自己

我有以下重定向:

rewritecond %{THE_REQUEST} \s/+search\.php? [NC]
rewriterule ^ /Search/? [R=301,L]
rewritecond %{REQUEST_FILENAME} !-d
rewritecond %{REQUEST_FILENAME} !-f
rewriterule ^Search/?$ /search.php? [L,QSA]

rewritecond %{THE_REQUEST} \s/+search\.php\?pageNum_RS_Search=([^\s&]+)&totalRows_RS_Search=([^\s&]+) [NC]
rewriterule ^ /Search/%2/%1? [R=301,L]
rewritecond %{REQUEST_FILENAME} !-d
rewritecond %{REQUEST_FILENAME} !-f
rewriterule ^Search/([^/]+)/([^/]+)/?$ /search.php?pageNum_RS_Search=$2&totalRows_RS_Search=$1 [L,QSA]

将search.php重写为Search /并将分页重写为Search / 12/1这就是我想要它做的事。

问题是当执行搜索时,没有使用$ _POST ['Search']变量。没有重定向就可以了。 欢迎任何帮助

这是分页和搜索请求的脚本:

$maxRows_RS_Search = 7;
$pageNum_RS_Search = 0;
if (isset($_GET['pageNum_RS_Search'])) {
  $pageNum_RS_Search = $_GET['pageNum_RS_Search'];
}
$startRow_RS_Search = $pageNum_RS_Search * $maxRows_RS_Search;


$colname_RS_Search = "-1";
if (isset($_SESSION['Search'])) {
  $colname_RS_Search = $_SESSION['Search'];
}
mysql_select_db($database_dconn, $dconn);
$query_RS_Search = sprintf("SELECT * FROM products WHERE (Category LIKE %s OR products.Manufacturer LIKE  %s OR products.Model LIKE  %s OR products.Color LIKE  %s) AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no'", GetSQLValueString("%" . $colname_RS_Search . "%", "text"),GetSQLValueString("%" . $colname_RS_Search . "%", "text"),GetSQLValueString("%" . $colname_RS_Search . "%", "text"),GetSQLValueString("%" . $colname_RS_Search . "%", "text"));


$query_limit_RS_Search = sprintf("%s LIMIT %d, %d", $query_RS_Search, $startRow_RS_Search, $maxRows_RS_Search);
$RS_Search = mysql_query($query_limit_RS_Search, $dconn) or die(mysql_error());
$row_RS_Search = mysql_fetch_assoc($RS_Search);

if (isset($_GET['totalRows_RS_Search'])) {
  $totalRows_RS_Search = $_GET['totalRows_RS_Search'];
} else {
  $all_RS_Search = mysql_query($query_RS_Search);
  $totalRows_RS_Search = mysql_num_rows($all_RS_Search);
}
$totalPages_RS_Search = ceil($totalRows_RS_Search/$maxRows_RS_Search)-1;

$queryString_RS_Search = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_RS_Search") == false && 
        stristr($param, "totalRows_RS_Search") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_RS_Search = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_RS_Search = sprintf("&totalRows_RS_Search=%d%s", $totalRows_RS_Search, $queryString_RS_Search);

按如下方式触发分页:

<td><?php if ($pageNum_RS_Search > 0) { // Show if not first page ?>
             <a href="<?php printf("%s?pageNum_RS_Search=%d%s", $currentPage, 0, $queryString_RS_Search); ?>">First</a>
             <?php } // Show if not first page ?></td>
         <td><?php if ($pageNum_RS_Search > 0) { // Show if not first page ?>
             <a href="<?php printf("%s?pageNum_RS_Search=%d%s", $currentPage, max(0, $pageNum_RS_Search - 1), $queryString_RS_Search); ?>">Previous</a>
             <?php } // Show if not first page ?></td>
         <td><?php if ($pageNum_RS_Search < $totalPages_RS_Search) { // Show if not last page ?>
             <a href="<?php printf("%s?pageNum_RS_Search=%d%s", $currentPage, min($totalPages_RS_Search, $pageNum_RS_Search + 1), $queryString_RS_Search); ?>">Next</a>
             <?php } // Show if not last page ?></td>
         <td><?php if ($pageNum_RS_Search < $totalPages_RS_Search) { // Show if not last page ?>
             <a href="<?php printf("%s?pageNum_RS_Search=%d%s", $currentPage, $totalPages_RS_Search, $queryString_RS_Search); ?>">Last</a>
               <?php } // Show if not last page ?></td>

2 个答案:

答案 0 :(得分:0)

当您重定向页面时,您总是松开变量$ _POST。

你可以先让你的php工作,然后用php函数Header()

重定向

答案 1 :(得分:0)

是的,当您使用规则中的R标志进行重定向或从PHP重定向时,POST内容确实会丢失。

如果您想在POST操作中使用漂亮的网址,最好像这样更改action的{​​{1}}部分:

form

您的规则的更正版本也是:

<form id="searchbox" action="/Search" method="post">
      <input id="search" name="Search" type="text" placeholder="Search Products">

  <input id="submit" type="submit" value="Search" ></form>

RewriteCond %{REQUEST_METHOD} !POST rewritecond %{THE_REQUEST} \s/+search\.php[\s?] [NC] rewriterule ^ /Search/? [R=301,L] rewritecond %{REQUEST_FILENAME} !-d rewritecond %{REQUEST_FILENAME} !-f rewriterule ^Search/?$ /search.php [NC,L,QSA] RewriteCond %{REQUEST_METHOD} !POST rewritecond %{THE_REQUEST} \s/+search\.php\?pageNum_RS_Search=([^\s&]+)&totalRows_RS_Search=([^\s&]+) [NC] rewriterule ^ /Search/%2/%1? [R=301,L] rewritecond %{REQUEST_FILENAME} !-d rewritecond %{REQUEST_FILENAME} !-f rewriterule ^Search/([^/]+)/([^/]+)/?$ /search.php?pageNum_RS_Search=$2&totalRows_RS_Search=$1 [L,QSA] 将跳过RewriteCond %{REQUEST_METHOD} !POST次请求的重定向规则。