从会话中获取预设的用户名和密码不会验证

时间:2014-03-06 16:22:30

标签: php session

我有一个新网站,管理员登录后,他们必须更改默认值的用户名和密码。密码有效,但用户名没有。

请检查我的每个循环:

if (isset($_POST['btnLogin'])) {
    $adminfirstlogin = array('admin', 'admin');
    $_SESSION['Login'] = $adminfirstlogin;
    $_SESSION['Username'] = $_POST['Username'];
    $_SESSION['Password'] = $_POST['Password'];
    $_SESSION['Work'] = $_POST['Work'];

    $pass = 0; //boolean that will turn yes if username+password matches
    $use = 0;
    $name = $_POST['Username'];
    $password = $_POST['Password'];

    if (empty($name)) {
        echo 'Please enter your username';
    } elseif (empty($password)) {
        echo 'Please enter your password';
    }
    foreach ($_SESSION as $key => $value) //login
    {
        foreach ($value as $subkey => $subvalue) //admin=>admin
        {
            if ($subkey == $name) {

                if ($subvalue == $password) {

                    header('location:Admin.php');
                } else {
                    die('<p class="DBsuccess">The password you typed is incorrect</p>');
                }

            } else {
                die('<p class="DBsuccess">Your username is wrong</p>');
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

以下(成功)制作:

fred的
12345

在用户名和密码字段中输入fred12345

我不知道你是如何检查与现有条件陈述的比较,但从我收集的内容来看,这告诉我你可能正在检查数据库。

根据以下内容调试代码。 (更多代码如下)

<?php
session_start();
if (isset($_POST['btnLogin'])) {

    $_SESSION['Username'] = $_POST['Username'];
    $_SESSION['Password'] = $_POST['Password'];

    $name = $_SESSION['Username'];
    $password = $_SESSION['Password'];

echo $name;
echo $password;
}
?>

<form action="" method="post">
Username: 
<input type="text" name="Username">
<br>
Password: 
<input type="password" name="Password">
<br>
<input type="submit" name="btnLogin" value="Submit">
</form>

所以,必须有某些东西丢失,或者某处有拼写错误。

使用您现有的代码,我收到了相反的Your username is wrong而不是“密码”,因为您声明“但用户名不是”

我为自己的测试设置了一个自己的形式。这个,当我为这两个字段输入“admin”和“admin”时,您的表单可能有拼写错误或表单元素未命名。

<?php

session_start();

if (isset($_POST['btnLogin'])) {
    $adminfirstlogin = array('admin', 'admin');
    $_SESSION['Login'] = $adminfirstlogin;
    $_SESSION['Username'] = $_POST['Username'];
    $_SESSION['Password'] = $_POST['Password'];
    $_SESSION['Work'] = $_POST['Work'];

    $pass = 0; //boolean that will turn yes if username+password matches
    $use = 0;
    $name = $_POST['Username'];
    $password = $_POST['Password'];

    if (empty($name)) {
        echo 'Please enter your username';
    } elseif (empty($password)) {
        echo 'Please enter your password';
    }
    foreach ($_SESSION as $key => $value) //login
    {
        foreach ($value as $subkey => $subvalue) //admin=>admin
        {
            if ($subkey == $name) {

                if ($subvalue == $password) {

                 //   header('location:Admin.php');

echo "ADMIN section";

echo "<br>";
echo $_SESSION['Username']; // this echo'ed successfully
echo "<br>";
echo $_SESSION['Password']; // this echo'ed successfully

                } else {
                    die('<p class="DBsuccess">The password you typed is incorrect</p>');
                }

            } else {
                die('<p class="DBsuccess">Your username is wrong</p>');
            }
        }
    }
}

?>

<form action="" method="post">

Username: 
<input type="text" name="Username">
<br>
Password: 
<input type="password" name="Password">
<br>
<input type="submit" name="btnLogin" value="Submit">
</form>

答案 1 :(得分:0)

我刚刚修改了我的代码:

  $adminfirstlogin = array('Armin'=>'admin');

现在效果很好..