0

表格:

<form action="/HW2/controllers/login.php" id = "login-box" method = "post">
        Username: <input type="text" name='user'><br>
        Password: <input type="text" name='pwd'><br>
        <input type="submit">
    </form>

那个行动:

<?php
require("../config/config.php");
if(isset($_POST['user']) && isset($_POST['pwd'])){
    if(strcmp($_POST['user'], USERNAME) == 0 && strcmp($_POST['pwd'], 
        PASSWORD) == 0){
        session_start();
        $_SESSION['logon'] = true;
        echo "success";
        //header("location: /HW2/index.php?view=loggedin");
    }else{
        print_r("Sorry, your username and/or password are invalid. Try Again?");
        //header("location: /HW2/index.php?view=loginPage");
        }
}else{
    print_r("Post data not sent.");
}
?>

我尝试了许多不同的方法,所有结果都导致“未发送发布数据。”(如果我尝试访问 $_POST 值,又名“未定义索引:”)一直试图找到我搞砸了好几个小时的东西,任何帮助表示赞赏,我是 php 的彻底磨砂,这可能是非常简单和愚蠢的事情。

提前致谢!

4

3 回答 3

0

我认为不需要使用最后一个“else”

仅使用

if(isset($_POST['user']) && isset($_POST['pwd'])){
if(strcmp($_POST['user'], USERNAME) == 0 && strcmp($_POST['pwd'], 
    PASSWORD) == 0){
    session_start();
    $_SESSION['logon'] = true;
    echo "success";
    //header("location: /HW2/index.php?view=loggedin");
}else{
    print_r("Sorry, your username and/or password are invalid. Try Again?");
    //header("location: /HW2/index.php?view=loginPage");
    }

}

于 2013-10-02T12:47:36.020 回答
0
<?php
error_reporting(E_ALL ^ E_NOTICE);

require("../config/config.php");
if(isset($_POST['user']) && isset($_POST['pwd'])){
    if(strcmp($_POST['user'], USERNAME) == 0 && strcmp($_POST['pwd'], 
        PASSWORD) == 0){
        session_start();
        $_SESSION['logon'] = true;
        echo "success";
        //header("location: /HW2/index.php?view=loggedin");
    }else{
        print_r("Sorry, your username and/or password are invalid. Try Again?");
        //header("location: /HW2/index.php?view=loginPage");
        }
}else{
    print_r("Post data not sent.");
}
?>

错误报告(E_ALL ^ E_NOTICE);显示错误但隐藏通知。未定义的索引表明 $_POST 变量在没有值的情况下被调用,因此这是第一个选项。第二种选择是修改代码。

风景:

<form action="/HW2/controllers/login.php" id = "login-box" method = "post">
        Username: <input type="text" name='user'><br>
        Password: <input type="text" name='pwd'><br>
        <input type="submit" name = "submit" value = "Submit">
    </form>

php代码:

<?php
require("../config/config.php");
if(isset($_POST['submit'])){
    if(strcmp($_POST['user'], USERNAME) == 0 && strcmp($_POST['pwd'], 
        PASSWORD) == 0){
        session_start();
        $_SESSION['logon'] = true;
        echo "success";
        //header("location: /HW2/index.php?view=loggedin");
    }else{
        print_r("Sorry, your username and/or password are invalid. Try Again?");
        //header("location: /HW2/index.php?view=loginPage");
        }
}else{
    print_r("Post data not sent.");
}?>
于 2013-10-02T12:51:31.117 回答
0

嘿,谢谢大家的回答,事实证明,在我重新启动计算机并小睡后,我的代码工作正常。

我的猜测是 chrome 或 apache 错误地处理了 $_POST 变量,但我不是很有经验,所以我只是在吐槽。无论哪种方式,我的代码都在做我现在想要的。

无论如何感谢您的帮助!

于 2013-10-02T21:06:19.753 回答