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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?