繁体   English   中英

PHP简单登录系统-500错误

[英]PHP Simple Login System - 500 Error

因此,我正在制作一个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();
    }
} ?>

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

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

<?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();
}
?>

我注意到的第一件事是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”。

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

在旁边的话题。 if-else的用途是if-else 它们都重定向到同一页面。 也许这只是为了测试,还没有定义$_GET变量来显示消息或类似的东西?

更新。 我以某种方式错过了Artaexe的答案,所以这篇文章毫无用处...

首先,您应该将代码编写得更简洁一些,更容易阅读。 您的根目录中是否有.htaccess文件? 还是在其他任何目录中? 这是一个隐藏文件,如果是这样,请在此处发布该文件的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM