doubi2014 2015-12-08 22:07
浏览 13
已采纳

PHP登录凭据即使它们是正确的也不会被验证

Beginner question. I need to create a admin login for a blog but I'm having trouble getting it to accept the credentials. The login name is "user" and the password is "pwed" just for testing purposes. When I try to login it says my credentials are invalid even though they aren't. If successful, it just simply moves on to the admin page. It should be pretty simple mistake, which I'm looking over, but I was hoping maybe one of you could help me out.

Here's the login form

<form id="form1" name="form1" method="post" action="">
                <p>
                    <label for="name">Account Name:</label>
                    <input type="text" name="login" id="login" /><br/>
                    <label for="name">Password:</label>
                    <input type="text" name="password" id="name" />
                </p>
                <p>
                    <input type="submit" name="Submit1" value="Next &gt;" />
                </p>
            </form>

Here's the PHP

<?php

        /* Check to see if there's a form submission */
        if (array_key_exists('Submit1', $_POST)) { // each page needs a different name for the submit button
            session_start();

            // clear any existing session variables because this is the very first page of the survey
            $_SESSION = array();


            /* set required fields */
            //  must be an array, even if only one item is required
            //  if no fields are required, an empty array is needed. Otherwise, in_array() later in the script will generate an error
            $required = array('name', 'password');



            $_SESSION['missing'] = array();

            // process the $_POST variables and save user input in the $_SESSION array
            foreach ($_POST as $key => $value) {
                // assign to temporary variable and strip whitespace if not an array
                $temp = is_array($value) ? $value : trim($value);
                // if empty and required, add to $_SESSION['missing'] array
                if (empty($temp) && in_array($key, $required)) {
                    array_push($_SESSION['missing'], $key);
                }
                // otherwise, assign to a variable of the same name as $key
                else {
                    $_SESSION[$key] = $temp;
                }
            }
            // if no required fields are missing, redirect to the next page
            if (!$_SESSION['missing']) {

              if  ($_SESSION['name'] == "user" && $_SESSION['password'] == "pwed"){
                  // set a variable to control access to other pages, this variable will be checked on all other survey pages
                  $_SESSION['access'] = true;

                  header('Location: svar02.php');
                  exit;

              }
                else {
                    echo "We're sorry your login details seem to be incorrect";
                }
            }
            else{
                echo "It seems you've left a field blank";
            }
            //}
        }
        ?>
</div>
  • 写回答

1条回答 默认 最新

  • dongzhan1492 2015-12-08 22:19
    关注

    I think the issue is with the following bit

    $required = array('name', 'password');
    

    It's requiring a field named name but your form has login so try:

    $required = array('login', 'password');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?