如果接收到值isset,则回显输入字段

时间:2013-07-22 09:39:36

标签: php html forms

我在网页上有一个搜索表单,我将根据推出的内容创建一个动态页面。我已经有了它的工作,但还有另一种形式,也有可选择的数据。我希望它仅在设置sku,sku2,txtKeyword2时显示隐藏的字段行。请在下面找到我到目前为止所尝试的内容。

<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME']?>">
  <table width="100%">
    <tr>
      <th><h3>Search    </h3>
            <div class="alert-box">Insert Text for alert box</div>
        <input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>" size="40">
        <?php if(isset($_GET['sku'])) echo '<input type="hidden" name="sku" value="'.$_GET['sku'].'">'?>
        <?php if(isset($_GET['sku2'])) echo '<input type="hidden" name="sku2" value="'.$_GET['sku2'].'">'?>
        <?php if(isset($_GET['txtKeyword2'])) echo '<input type="hidden" name="txtKeyword2" value="'.$_GET['txtKeyword2'].'">'?>
        <input class="alert button" type="submit" value="Search"></th>
    </tr>
  </table>
</form>

我想要的只是如果它们没有设置就不显示输入行。我想我做得对,但我不确定,因为我正在学习php。

我还尝试了以下代码,但它输出了以下网址。

index.php?txtKeyword=giro%25skyline&sku=%09%09<input+type%3D

我知道这不应该发生但它会使我的页面工作但是当我将数据输入到另一个搜索表单时,它会将输入行的一部分添加到url中。这是我尝试的代码:

<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME']?>">
  <table width="100%">
    <tr>
      <th><h3>Search    </h3>
            <div class="alert-box">Insert text for the alert box</div>
        <input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>" size="40">
        <input type="hidden" name="sku" value="<?php if(isset($_GET['sku'])) echo ''.$_GET['sku'].'">'?>
        <input type="hidden" name="txtKeyword2" value="<?php if(isset($_GET['txtKeyword2'])) echo ''.$_GET['txtKeyword2'].'">'?>
        <input type="hidden" name="sku2" value="<?php if(isset($_GET['sku2'])) echo ''.$_GET['sku2'].'">'?>
        <input class="alert button" type="submit" value="Search"></th>
    </tr>
  </table>
</form>

我真的很想知道如何解决这个问题。

谢谢Ryan

1 个答案:

答案 0 :(得分:0)

<?php echo isset($_GET['sku']) ? '<input type="hidden" name="sku" value="'.$_GET['sku'].'">' : ''; ?>

但是,仅供参考,根本不安全。

首先,该代码中可能存在两个漏洞:

  1. XSS: 如果$_GET['sku']类似于:"><script>alert('XSS');</script>,那么您的页面上会出现一个警告框,理论上它可用于网络钓鱼,cookie窃取,创建蠕虫(如SamiWorm)。修复/防止此问题的一种好方法是使用htmlentities将所有html字符编码到其关联实体中。

  2. SQL注入: 如果$_GET['sku']类似于' UNION ALL SELECT id,username,password FROM users LIMIT 0,1--那么你可能会使你的数据库被盗(特别是如果有人决定使用像SQLMap这样的工具,这是一个自动SQL注入工具)。修复此问题的一种好方法是在转义任何不允许的字符的参数上使用mysql_real_escape_string()

  3. 所以更好的方法是这样的:

     <?php echo isset($_GET['sku']) ? '<input type="hidden" name="sku" value="'.htmlentities(mysql_real_escape_string($_GET['sku'])).'">' : ''; ?>
    

    这就是您的代码无效的原因:

    1. 您试图让PHP评估您的结束html标记:您的php echo中的">将无效,因为它不知道如何解析它。

    2. 使用三元运算符比使用short-ifs更容易,因为imho,它们更容易为其他人读/写。

    3. 这是我的完整版本:

      <form name="frmSearch" method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
        <table width="100%">
          <tr>
            <th><h3>Search    </h3>
              <div class="alert-box">Insert text for the alert box</div>
              <input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo isset($_GET["txtKeyword"]) ? htmlentities(mysql_real_escape_string($_GET["txtKeyword"]))) : ''; ?>" size="40">
              <input type="hidden" name="sku" value="<?php echo isset($_GET['sku']) ? htmlentities(mysql_real_escape_string($_GET['sku'])) : ''; ?> ">
              <input type="hidden" name="txtKeyword2" value="<?php echo isset($_GET['txtKeyword2'])) ? htmlentities(mysql_real_escape_string($_GET['txtKeyword2'])); ?>">
              <input type="hidden" name="sku2" value="<?php echo isset($_GET['sku2']) ? htmlentities(mysql_real_escape_string($_GET['sku2'])); ?> ">
              <input class="alert button" type="submit" value="Search"></th>
          </tr>
        </table>
      </form>
      
相关问题