【发布时间】:2018-09-29 00:16:52
【问题描述】:
人
我正在尝试学习如何使用 slim 框架为网站创建注册和身份验证。我的目标是为 POST 制作一个处理程序,并在经过适当身份验证后,网站将保存用户名和名称。这样当用户下次访问该页面时,网站会通过说出他们的名字来问候他们。我将通过两个单独的 PHP 文件执行此操作,但我相信问题出在这个 PHP 文件中。
这是我目前拥有的:
$app->post('/users', function (Request $request, Response $response, array $args) {
$user = array('username'=> $_POST['username'], 'password' => $_POST['password'], 'name' => $_POST['name']);
$res = saveUser($user);
if($result === true) { return $response->withRedirect('login.html', 302); }
return $response->withRedirect('registration.html#',$result,302); });
$app->post('/auth', function (Request $request, Response $response, array $args) {
if(isset($_POST['username']) {
return $response->withRedirect('welcome.php', 302);
}
if(authUser($_POST['username'], $_POST['password']) === true) {
$_SESSION["username"] = $_POST['username'];
$_SESSION["name"] = $_POST['name'];
return $response->withRedirect('welcome.php', 302);
}
else { //authentication doesn't work, destroy session and go to login page
session_destroy();
return $response->withRedirect('login.html',302);
}
据我了解,用户名、密码和用户的真实姓名应该保存在_POST 中。但是,当我使用时:
var_dump($_POST);
密码和用户名是唯一在被调用时显示的。这让我相信这就是为什么我的“welcome.php”不欢迎用户。
这里是我的welcome.php的内容:
<?php session_start(); ?>
!DOCTYPE html
<title> Welcome! </title>
<h1> Welcome Page </h1>
<section>
<p>
<?php
if(isset($_SESSION['name'])) { echo "Grettings " . $_SESSION['name']. "! ";}
?> Click <a href="login.html">here to login</a> OR <a href="registration.html"> here for registration</a>.
</p>
</section>
我认为我的错误一定是我尝试调用它的方式或在 isset 函数中,但同样,我不知道为什么名称没有正确保存。
【问题讨论】:
标签: php authentication post slim