【问题标题】:Unusual behaviour of Htaccess URL-RewriteHtaccess URL-Rewrite 的异常行为
【发布时间】:2014-02-16 06:57:38
【问题描述】:

我正在使用 jQuery Autocomplete(从数据库中获取值)并向用户显示数据库中的结果。搜索结果是可点击的,当他们点击时,它们被发送到一个名为 product_display.php 的页面,其中 id 作为请求参数。 因此,例如,它将是这样的:- product_display.php?id=2

在product_display.php 页面上,有3 个单选按钮 和3 个表单(每个单选按钮一个,表单在单选时动态打开)。每个表单都有一个单独的提交按钮。点击提交后,它会转到另一个页面,并打印一个名为 cond 的隐藏变量的值。

在我在 htaccess 文件中引入 Url-Rewrite 以将 *product_display.php?id=2* 转换为 /products/2

之前,一切正常

当这个 url-rewrite 开始工作时,结果并不好。一旦我单击与任何单选按钮有关的任何表单的提交按钮,它就会再次显示 product_display 页面上的选项(但标题显示不同的页面)。 如何解决这个问题。

jQuery 自动完成。

<script>
  $(function() {
    function log( message ) {
      $( "<div>" ).text( message ).prependTo( "#log" );
      $( "#log" ).scrollTop( 0 );
    }

    $( "#birds" ).autocomplete({
      source: "search.php",
      minLength: 2,
      select: function( event, ui ) {


        log( ui.item ? "Selected: " + ui.item.value + " aka " + ui.item.label :
          "Nothing selected, input was " + this.actor );
         window.location.href = './products/' + ui.item.value;
         //window.location.href = 'product_display.php?id=' + ui.item.value;
       // document.testForm.action = "pretravel.php?id="+ui.item.value;
        //document.testForm.submit();
      }
    });
  });

products_display.php

<div id="credit-card">
        <section id="content">


     <input type="radio" id="radio1" name="radios" value="radio1" checked>
   <label for="radio1">Working</label>

<input type="radio" id="radio2" name="radios" value="radio2">
   <label for="radio2">Working - Damaged</label>

    <input type="radio" id="radio3" name="radios" value="radio3">
   <label for="radio3">Not Working</label>

            <form action="offer_show.php" method="post" id="working">
<input type="hidden" name="cond" value="working" id="cond">
<input type="submit" name="submit">
            </form>

    <form action="offer_show.php" method="post" id="workingdamaged">        
DEbit Card payment here
<input type="hidden" name="cond" value="workingdamaged" id="cond">
<input type="submit" name="submit">
    </form>



    <form action="offer_show.php" method="post" id="nonworking">
                <input type="hidden" name="cond" value="damaged" id="cond">
                      <input type="submit" name="submit">
            </form>




        </section>

    </div>


</body>

<script type="text/javascript">
var radios = document.getElementsByName("radios");
var working =  document.getElementById("working");
var workingdamaged =  document.getElementById("workingdamaged");
var nonworking =  document.getElementById("nonworking");
working.style.display = 'block';   // show
workingdamaged.style.display = 'none';
nonworking.style.display = 'none';// hide
for(var i = 0; i < radios.length; i++) {
    radios[i].onclick = function() {
        var val = this.value;
        if(val == 'radio1')
        {
            working.style.display = 'block';
            workingdamaged.style.display = 'none';
            nonworking.style.display = 'none';
        }
        else if(val == 'radio2')
        {
            working.style.display = 'none';
            workingdamaged.style.display = 'block';
            nonworking.style.display = 'none';
        }
        else if(val == 'radio3')
        {
            working.style.display = 'none';
            workingdamaged.style.display = 'none';
            nonworking.style.display = 'block';
        }    

    }
}
</script>

htaccess

RewriteRule ^products/(.+)$ product_display.php?id=$1 [L,QSA]

offer_show.php(点击提交后打开的页面)

<?php
echo $_REQUEST['cond'];



?>

【问题讨论】:

  • 没什么大不了的,但您可能应该将您的 RewriteRule 更改为这个(接近开头):^products/(\d+)$ 只允许 ID 字段中的数字。语义正确,有助于再次保护 SQL 注入,因为您不能只用数字编写注入代码。
  • 工作完美。感谢您完整阅读这篇文章。我只是想知道是否有人会通过这么长的帖子:)
  • 等等,问题解决了吗?如果是这样,我会将其添加为答案。

标签: javascript php jquery html .htaccess


【解决方案1】:

这可能是一个相对/绝对 URL 的事情。你的表单有这个作为它的动作:

action="offer_show.php"

这意味着当您转到:/products/123 时,论坛将提交给/products/offer_show.php,然后将其重写为/product_display.php?id=offer_show.php

尝试将表单的操作更改为绝对 URL:

action="/offer_show.php"    

您可能还想添加

<base href="/" />

到您的页面标题。

【讨论】:

    猜你喜欢
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2020-02-13
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多