繁体   English   中英

在 Apache web 服务器上使用 PHP 登录

[英]Login with PHP on Apache web server

我是 PHP/Apache web 服务器的新手,所以请多多包涵。

我正在尝试使用 PHP 实现登录系统。 目前,表单将登录数据发送到名为auth.php的脚本。 然后,此脚本验证登录详细信息。 如果登录详细信息正确,则脚本使用readfile将所需的经过身份验证的页面发送给最终用户。 如果登录详细信息不正确,则脚本会将用户重定向回登录页面。

问题是readfile发送原始 PHP 页面而不运行 PHP 代码。 我想运行 PHP 然后发送 output 这个。 我最初的问题是如何做到这一点?

但是,我怀疑实际问题是我如何处理身份验证,并且我需要以不同的方式对用户进行身份验证。 如果是这种情况,请您指出如何执行此操作的指南的方向吗?

相关代码片段:

登录表单

  <form class="form-signin" method="post" action="./auth.php?target=dashboard">
    <h2 class="form-signin-heading">Admin Sign In</h2>
    <label for="inputEmail" class="sr-only">Email address</label>
    <input type="email" id="email" name="email" class="form-control" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" id="password" name="password" class="form-control" placeholder="Password" required>
    <div class="checkbox">
      <label>
        <input type="checkbox" name="rememberme" value="remember-me"> Remember me
      </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
  </form>

auth.php (nb哈希密码被禁用以进行测试)

<?php

        function authenticate_user(string $uname, string $pwd) {
                // return true or false

                $hashed = $pwd;//password_hash($pwd, null);

                $admin_uname = "test@example.com";
                $admin_pwd = "test";

                return $hashed === $admin_pwd and $uname === $admin_uname;
        }


        namespace admin;

        $dir = getcwd();
        $is_auth = authenticate_user($_POST["email"], $_POST["password"]);

        if($is_auth) {
                $extension = $_GET["target"].".php";
                $full = $dir."/".$extension;
                readfile($full);
        }
        else {
                $full = $_SERVER["PHP_SELF"]."/../?FAIL";
                //echo $full;
                header("Location: ".$full);
        }

?>

当前仪表板.php

<!DOCTYPE HTML>
<html>
<head>
<title>Dashboard</title>
</head>

<body>

        <p>Success</p>

<?php
        echo "<p>Flag</p>";
?>

</body>
</html>

当前 output

<html><head>
<title>Dashboard</title>
</head>

<body>

    <p>Success</p>

<!--?php
    echo "<p-->Flag<p></p>";
?&gt;

</body></html>

所需 output

<html><head>
<title>Dashboard</title>
</head>

<body>

    <p>Success</p>
    <p>Flag</p>

</body></html>

任何指示或建议将不胜感激,干杯:)

建议,您查看本教程,它有一个简单的结构化方法,用于使用 mysql 的 PHP 登录系统: 教程

暂无
暂无

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

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