为简单的php密码创建会话

时间:2012-08-15 23:35:56

标签: php session passwords

我需要能够存储在会话中提交的密码,因为目前使用php_self方法页面正在重新加载,好像用户没有正确输入密码(这发生在Firefox中,它实际上工作正常在Chrome / Safari中没有会话)因此我认为它需要一个会话才能在FF中工作,但我对PHP很新,不知道如何解决这个问题,这里是代码:

提前致谢

<?php
session_start();

$Password = 'hello';

   if (isset($_POST['submit_pwd'])){
      $pass = isset($_POST['passwd']) ? $_POST['passwd'] : '';


      if ($pass != $Password) {
         showForm("error", "WRONG PASSWORD");
         exit();     
      }
   } else {
      showForm();
      exit();
   }

function showForm($Inputclass="mister", $Placeholder="PLEASE ENTER PASSWORD"){ 

?>

使用HTML:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd" id="pwd">


 <input class="<?php echo $Inputclass; ?>" name="passwd" type="password" size="79" style="margin-left: -100px;" placeholder="<?php echo $Placeholder; ?>"/>
         <br/>
             <input class="text" name="submit_pwd" value="Login" type="image" src="loginkey.png" style="position: relative; left: 240px; top: -35px;">

      </form>

1 个答案:

答案 0 :(得分:1)

如果按下按钮,则应设置$ _POST [&#39; submit_pwd_x&#39;]和$ _POST [&#39; submit_pwd_y&#39;],而不是$ _POST [&#39; submit_pwd&#39 ]

如果你点击&#34;输入&#34;然后$ _POST [&#39; submit_pwd&#39;],$ _POST [&#39; submit_pwd_x&#39;]或$ _POST [&#39; submit_pwd_y&#39;]不应设置 - 但有时取决于浏览器。

你不能依赖任何一个,所以你的检查是否(isset($ _ POST [&#39; submit_pwd&#39;]))将是不可靠的

诀窍是在表单中添加一个隐藏字段,并在$ _POST中检查该隐藏字段的sbmission。


答案的更多细节(基于评论):

PHP

if (isset($_POST['formsubmitted'])){ 
    ....
}

HTML

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd" id="pwd"> 
     <input class="<?php echo $Inputclass; ?>" name="passwd" type="password" size="79" style="margin-left: -100px;" placeholder="<?php echo $Placeholder; ?>"/> 
     <br/>
     <input type="hidden" name="formsubmitted" value="1" /> 
     <input class="text" name="submit_pwd" value="Login" type="image" src="loginkey.png" style="position: relative; left: 240px; top: -35px;"> 

</form> 

编辑:

如果您没有点击按钮(按回车键):

  • IE将不发送任何内容
  • FF,Opera将发送submit_pwd_x = 0和submit_pwd_y = 0
  • Chrome,Safari会发送submit_pwd_x = 0,submit_pwd_y = 0和submit_pwd

如果单击按钮:

  • IE将发送submit_pwd_x = X和submit_pwd_y = Y
  • FF,Opera将发送submit_pwd_x = X和submit_pwd_y = Y
  • Chrome,Safari会发送submit_pwd_x = X,submit_pwd_y = Y和submit_pwd