1

我想做的事

我正在使用登录表单创建一个单页成员区域。

登录后,用户(在此阶段)应该看到的唯一内容是退出按钮。

出了什么问题

只要数据库中有匹配项(例如用户名正确,或者用户名和密码都正确),就会有一个500 Server Error.

刷新页面,无论密码是否匹配,用户都会登录。

当用户单击注销链接时,还有一个500 Server Error.

编码

<?php 
session_start();

if(isset($_GET['logout'])&&$_GET['logout']==='true'){
    logout();
    header('location: ./');
}

if(isset($_POST['submit'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];

    $hostname = 'REDACTED';
    $username = 'REDACTED';
    $password = 'REDACTED';
    $dbname   = 'REDACTED';

    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    }

    catch(PDOException $e){
        echo($e->getMessage());
    }


    $stmt=$dbh->prepare('SELECT `password`, `id` FROM `tap_users` WHERE `email`=?');
    $stmt->execute(array($email));

    if($stmt->rowCount()==0){
        //incorrect username
        header('location: ./?error=user');
        exit();
    }
    $userData = $stmt->fetch(PDO::FETCH_ASSOC);

    if($password != $userData['password']) {
        //incorrect password
        header('location: ./?error=pass');
        exit();
    } else {
        validateUser($userData['id']);
        header('location: ./');
    }
}

?>

<head></head>

<body>

<?php
if(!isLoggedIn()) {
    show_login();
    exit();
}
?>

<a href="?logout=true">Logout</a>

</body>

</html>

<?php
//Show login form
function show_login(){
    echo    '<form method="post">
                <input type="text" name="email" placeholder="Email Address" />
                <input type="password" name="password" placeholder="Password" />
                <input type="submit" value="Submit" name="submit" />
            </form>

            </body>

            </html>';
}

//Check if a user is logged in
function isLoggedIn(){
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}

//validate the user
function validateUser($userid){
    //this is a security measure
    session_regenerate_id();
    $_SESSION['valid'] = 1;
    $_SESSION['userid'] = $userid;
}

//logout the user
function logout(){
    //destroy all of the session variables
    $_SESSION = array();
    session_destroy();
}
?>
4

2 回答 2

1

您所做的是如果用户登录正确,那么您将重定向到 header('location: ./'); 也许您没有 index.php 或目录索引在您的 apache 配置中已关闭。

做一件事改变它

标头('位置:index.php');

它会起作用的。

这与php或mysql无关,它是服务器配置错误。

于 2015-01-15T16:53:07.140 回答
0

我通过以下方式运行您的代码:在线 php 代码验证器语法似乎没有任何问题。问题可能来自文件的权限错误。该文件是否具有用户的执行权限。在你的 ftp 中确保权限设置为 755。这应该允许每个人阅读和执行 php 代码。如果这不是问题,请联系您的主机并询问错误,他们应该能够为您提供该信息。另一种选择是在 PHP 中创建自己的错误日志记录。您可以查看此旧堆栈溢出帖子,以继续使您的所有错误对单独的文件可见。

于 2015-01-15T16:53:40.620 回答