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 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊