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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动