我确信这是一个基本问题,而且我确信答案也是基本的 - 如果是这样的道歉,但是我很难将我的实际例子与我读过的理论联系起来。
我正在使用PHPAuth项目进行网站身份验证。在笔记中让我困惑的一点是login方法的描述:
使用系统验证用户。注意:您需要接受 返回会话哈希并创建会话cookie ,方法确实如此 不要这样做。
如果成功登录返回:
Array ( [error] =>
[message] => You are now logged in.
[hash] => 374d0de4f97b96b6665c23aa0998dbae1f790fe6
[expire] => 0
)
我该如何处理此信息以允许下一页查看用户是否已登录?
<?php
require 'vendor/autoload.php';
include("dbconnect-user.php");
$config = new PHPAuth\Config($dbh);
$auth = new PHPAuth\Auth($dbh, $config);
if (!$auth->isLogged()) {
header('HTTP/1.0 403 Forbidden');
echo "Forbidden";
exit();
}
echo "you are logged in";
?>
我假设上面的代码将处理会话cookie,因为这是代码几乎与PHPAuth项目给出的示例完全一样,具体来说,我问的是上面引用中粗体文本的含义。
仅供参考,检查烹饪的班级功能如下:
/**
* Returns is user logged in
* @return boolean
*/
public function isLogged() {
return (isset($_COOKIE[$this->config->cookie_name]) && $this->checkSession($_COOKIE[$this->config->cookie_name]));
}
/**
* Returns current session hash
* @return string
*/
public function getSessionHash(){
return $_COOKIE[$this->config->cookie_name];
}
答案 0 :(得分:2)
我之前的回答回答了我的问题,但是我发布了这个答案,希望它可以帮助其他人使用PHPAuth项目。
这是我的登录页面:
<?php
require 'vendor/autoload.php';
include("dbconnect-user.php"); //this contains my own database connection code
$config = new PHPAuth\Config($dbh);
$auth = new PHPAuth\Auth($dbh, $config);
if ($auth->isLogged()) {
echo "You are already signed up and logged in";
exit();
}
$msg = null;
$showform = true;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//check for errors
if (empty($_POST["email"]) ||
empty($_POST["pwd"]) ) {
$msg = "All fields are required.";
} else { //all good - proceed
$result = $auth->login($_POST["email"], $_POST["pwd"]);
//check for success
if ($result["error"] == 1) {
$msg = $result["message"];
} else {
//success
$msg = $result["message"];
$showform = false;
setcookie($config->cookie_name, $result['hash'], time()+3600, "/"); //NOTE: the time can be set with config from the cookie_forget or cookie_remember settings in the PHPAuth config table
}
}
}
?>
<html>
<head></head>
<body>
<?php
if ($showform == true) { ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<div>
<label for="email">Email:</label>
<div>
<input type="email" name="email" id="email" placeholder="Enter email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>">
</div>
</div>
<div>
<label for="pwd">Password:</label>
<div>
<input type="password" name="pwd" id="pwd" placeholder="Enter password">
</div>
</div>
<div>
<button type="submit" name="submit">Submit</button>
</div>
<?php echo $msg; ?>
</form><?php
} else {
echo $msg . '<br />';
}
?>
</body>
</html>
这是一个安全页面:
<?php
require 'vendor/autoload.php';
include("dbconnect-user.php"); //this contains my own database connection code
$config = new PHPAuth\Config($dbh);
$auth = new PHPAuth\Auth($dbh, $config);
if (!$auth->isLogged()) {
header('HTTP/1.0 403 Forbidden');
echo "Forbidden";
exit();
}
echo "You are logged in";
?>
这是一个退出脚本:
<?php
require 'vendor/autoload.php';
include("dbconnect-user.php"); //this contains my own database connection code
$config = new PHPAuth\Config($dbh);
$auth = new PHPAuth\Auth($dbh, $config);
echo $auth->logout($_COOKIE['authID']);
?>
答案 1 :(得分:0)
基本答案似乎是:
显然,我们需要创建会话cookie。在这种特殊情况下,cookie名称必须是存储在配置数据库中的名称,&#34; authID&#34;。
要执行此操作,我只需使用setcookie
,请参阅http://php.net/manual/en/function.setcookie.php
在我的测试示例中,成功登录后,我设置了cookie,然后在脚本页面上重定向,如下所示:
setcookie('authID', $_GET['h'], time() + (86400 * 30), "/");
header('Location: main.php');