dongyou2305 2012-08-15 23:35
浏览 44
已采纳

为简单的php密码创建会话

I need to be able to store the password submitted in a session, as at present using the php_self method the page is just reloading as if the user has not entered the password correctly (this is happening in Firefox, it actually works fine with no session in Chrome / Safari) and so I believe a session is needed for it to work in FF, but I'm newish to PHP and not sure how I can go about this, here's the code:

Thanks in advance

<?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"){ 

?>

With 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条回答 默认 最新

  • douzuo0002 2012-08-16 00:12
    关注

    If you hit the button, then $_POST['submit_pwd_x'] and $_POST['submit_pwd_y'] should be set, not $_POST['submit_pwd']

    If you hit "enter" then $_POST['submit_pwd'], $_POST['submit_pwd_x'] or $_POST['submit_pwd_y'] should NOT be set - but sometimes are depending on the browser.

    You can't rely on either, so your check if (isset($_POST['submit_pwd'])) will be unreliable

    The trick is to add a hidden field in the form, and check for sbmission of that hidden field in $_POST.


    More details on the answer (based on comment):

    PHP is

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

    HTML is

    <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> 
    

    Edit:

    If you don't click on the button (hit enter):

    • IE will send none
    • FF,Opera will send the submit_pwd_x=0 and submit_pwd_y=0
    • Chrome,Safari will send the submit_pwd_x=0, submit_pwd_y=0 and submit_pwd

    If you click the button:

    • IE will send submit_pwd_x=X and submit_pwd_y=Y
    • FF,Opera will send submit_pwd_x=X and submit_pwd_y=Y
    • Chrome,Safari will send the submit_pwd_x=X, submit_pwd_y=Y and submit_pwd
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用