PHP简单登录系统 - 500错误

时间:2012-04-15 14:52:01

标签: php login system

所以,我正在制作一个SQL登录系统,我想我会从一些简单的东西开始,比如检查用户名和密码的IF,然后返回主索引在右上方问候。
我做了所有这些,它应该从我看到的工作,但当我提交登录表单时,服务器抛出HTTP Error 500 (Internal Server Error)。 我希望我能在这里得到一些帮助,因为我的IRC朋友无法找到解决方案。登录系统包含2个文件,其内容为:

的index.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
    <title>Kinz</title>
    <?php session_start(); ?>
    <link rel="stylesheet" href="stylesheet.css">
    <meta name="description" content="Kinz's personal website." />
    <meta name="keywords" content="kinz,php,login,fruity,css" />
    <meta name="author" content="Kinz" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
    <div id="mainbar">
        <label class="menubar">Home</label>

        <?php if ($_SESSION['loggedin'] == false || isset($_SESSION['loggedin']) == false) { ?>
        <form id="loginform" action="auth.php?login=true" method="POST">
            <input type="submit" class="login" id="login" name="loginSubmit" value="Login">
            <input type="password" class="login" id="password" name="password" value="Password">
            <input type="text" class="login" id="username" name="username" value="Username">
        </form> <?php } elseif ($_SESSION['loggedin'] == true) { ?>
            <label id="loginWelcome">Welcome, <?php echo $_SESSION['user']; ?>!</label> <?php } else { echo "Error."; } ?>
    </div>
    <div id="content">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>
</body>
</html>

auth.php:

<?php session_start();
$user = preg_replace("/[^a-zA-Z1-9]/", '', $_POST['username']);
$pass = preg_replace("/[^a-zA-Z1-9]/", '', $_POST['password']);
$hpass = md5($pass);

if (isset($_POST['loginSubmit']) && $_GET['login'] == "true") {
    if ($user == "Admin" && $pass == "Password123") {
        $_SESSION['loggedin'] = true;
        $_SESSION['user'] = $user;
        header("Location: index.php"); exit();
    } else {
            print("<h1><b>Login failure.</b></h1>"); sleep(10);
            header("Location: index.php"); exit();
            }

    if (!$_SESSION['loggedin']) {
        header("Location: index.php"); exit();
    } else {
            header("Location: index.php"); exit();
    }
} ?>

我在代码中找不到任何错误,我希望有人会这样做。任何帮助表示赞赏。

4 个答案:

答案 0 :(得分:3)

在正确缩进代码后,您会看到缺少}

<?php
 session_start();
 $user = preg_replace("/[^a-zA-Z1-9]/", '', $_POST['username']);
 $pass = preg_replace("/[^a-zA-Z1-9]/", '', $_POST['password']);
 $hpass = md5($pass);
 if (isset($_POST['loginSubmit']) && $_GET['login'] == "true") {
     if ($user == "Admin" && $pass == "Password123") {
         $_SESSION['loggedin'] = true;
         $_SESSION['user'] = $user;
         header("Location: index.php");
         exit();
     } else {
         print("<h1><b>Login failure.</b></h1>");
         sleep(10);
         header("Location: index.php");
         exit();
     }
     if (!$_SESSION['loggedin']) {
         header("Location: index.php");
         exit();
     } else {
         header("Location: index.php");
         exit();
     } // < Here's a missing } - Can't place two else-blocks after each other
} else {
     header("Location: index.php");
     exit();
}
?>

答案 1 :(得分:1)

我注意到的第一件事是session_start()不在你脚本的第一行。 session_start()必须始终在 发送任何输出之前执行

另外,为什么:

if (!$_SESSION['loggedin']) {
    header("Location: index.php");
    exit();
} else {
    header("Location: index.php");
    exit();
}/* THIS BIT */ else {
    header("Location: index.php");
    exit();
}/* TO HERE */

1 if语句中不能有2x“else”。

答案 2 :(得分:1)

尝试在页面顶部的索引文件中移动session_start()。 (它调用页眉。)

关于主题。您if-else的用途是什么?他们都重定向到同一页面。也许这只是用于测试,你还没有定义$_GET变量用于显示消息或类似的东西?

更新。我不知何故错过了Artaexe的回答,所以这篇文章没用......

答案 3 :(得分:0)

首先,你应该把你的代码编写得更清晰,阅读起来很容易。根目录中是否有.htaccess文件?或者在任何其他目录中?它是一个隐藏文件,如果是这样,请在此处发布该文件的内容。

相关问题