PHP登录身份验证不起作用 [英] PHP login authentication not working

查看:146
本文介绍了PHP登录身份验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个.php文件。所有的HTML和PHP,没有SQL,不会需要/使用它。一个是登录页面,另一个是目的地。当我在日志中记录我设置的详细信息时,我无法到达目的地。如您所见, session_start(); 显然位于顶部。我甚至把它放在与开放的php标签相同的行上,没有任何区别。以下是这两个页面的代码:

I have two .php files. All html and php, no SQL and will not be needing/using it. One is the login page, the other is the destination. When I put in the log in details I have set, I can't get to the destination. As you can see, session_start(); is clearly at the top. I even put it right on the same line as the opening php tag and no difference was made. Here's the code for both pages:

登录:

<?php
session_start();

$username="testu";
$password="testp";
$_SESSION['logged_in']=false;   

if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
    header("Location: dest.php");
    exit;
}

if (isset($_POST['user']) && isset($_POST['pass'])) {
    if ($_POST['user'] == $username && $_POST['pass'] == $password) {
        $_SESSION['logged_in'] = true;
        header("Location: dest.php");
        exit;
    }
}
?>

<!DOCTYPE html>

<html lang="en">

<head>
    <title>A title</title>
</head>

<body>
    <form action="dest.php" method="post" style="font-    family:calibri;position:absolute;top:40%;left:35%;">
        Username: <input type="text" name="user"/><br><br>
        Password: <input type="password" name="pass"     style="position:relative;left:5px;"/><br><br><br>
    <input type="submit" value="Submit" style="position:relative;left:115px;"/>
    </form>

</body>

</html>

目的地:

(php tag here)
session_start();

if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == false) {
    header("Location: login.php");
    exit;
}   
?>

<!DOCTYPE html>

<html lang="en">

<head>
    <title>A title</title>
</head>

<body>
    <a href="login.php">Log out</a>
</body>

</html>

我注意到当我在目标文件上注释掉php代码时,我可以访问dest。 PHP。问题是,我可以访问任何登录详细信息,或者根本没有。它要么无效,要么有效。我怎样才能获得我已经开始工作的细节?我觉得这个问题在登录页面脚本中。非常感谢任何能够帮助我解决这个问题的人。

I noticed that when I commented out the php code on the destination file, I could access the dest.php. Issue is, I could access is with any login details, or none at all. It's either nothing works, or anything works. How can I get the details I have set to work? I feel the issue is in the login page script. Many thanks in advance to anyone who can help me resolve this.

注意:我不得不重新发布这个,因为我最后一个问题被标记为与我完全无关的事情的副本问题,谢谢。

Note: I had to repost this because my last question was marked as a duplicate of something completely irrelevant to my issue, thank you.

推荐答案

@Sean打败我,好工作: - )

如果您删除 action =dest.php,它应该可以工作。现在您将表单发送到不检查用户名和密码值的页面,因此未设置会话变量。

If you remove the action="dest.php" it should work. Right now you are sending the form to a page that does not check the values of the username and password, thus the session variable is not set.

<form method="post">
Username: <input type="text" name="user"/>
....
</form>

这篇关于PHP登录身份验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆