我刚刚用PHP创建了一个完整的登录和注册系统,但我的问题是我还没有使用任何会话技术。我在PHP方面有点新手,以前从未使用过会话技术。我想做的是,用户注册并填写登录表单后,他们仍然停留在同一页上。因此,页面的一部分将是如果会话已登录,另一部分则是否则(用户未登录,因此显示登录表单)。有人可以告诉我如何入手吗?
我刚刚用PHP创建了一个完整的登录和注册系统,但我的问题是我还没有使用任何会话技术。我在PHP方面有点新手,以前从未使用过会话技术。我想做的是,用户注册并填写登录表单后,他们仍然停留在同一页上。因此,页面的一部分将是如果会话已登录,另一部分则是否则(用户未登录,因此显示登录表单)。有人可以告诉我如何入手吗?
session_start();
将用户ID放入会话中以跟踪登录用户。 $_SESSION['user'] = $user_id;
检查是否有用户已登录
if (isset($_SESSION['user'])) {
// logged in
} else {
// not logged in
}
查找已登录用户的ID
$_SESSION['user']
所以在你的页面上
<?php
session_start();
if (isset($_SESSION['user'])) {
?>
logged in HTML and code here
<?php
} else {
?>
Not logged in HTML and code here
<?php
}
以下是使用PHP编写的最简单的会话代码,共使用3个文件。
login.php
<?php session_start(); // session starts with the help of this function
if(isset($_SESSION['use'])) // Checking whether the session is already there or not if
// true then header redirect it to the home page directly
{
header("Location:home.php");
}
if(isset($_POST['login'])) // it checks whether the user clicked login button or not
{
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "Ank" && $pass == "1234") // username is set to "Ank" and Password
{ // is 1234 by default
$_SESSION['use']=$user;
echo '<script type="text/javascript"> window.open("home.php","_self");</script>'; // On Successful Login redirects to home.php
}
else
{
echo "invalid UserName or Password";
}
}
?>
<html>
<head>
<title> Login Page </title>
</head>
<body>
<form action="" method="post">
<table width="200" border="0">
<tr>
<td> UserName</td>
<td> <input type="text" name="user" > </td>
</tr>
<tr>
<td> PassWord </td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td> <input type="submit" name="login" value="LOGIN"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
home.php
<?php session_start(); ?>
<html>
<head>
<title> Home </title>
</head>
<body>
<?php
if(!isset($_SESSION['use'])) // If session is not set then redirect to Login Page
{
header("Location:Login.php");
}
echo $_SESSION['use'];
echo "Login Success";
echo "<a href='logout.php'> Logout</a> ";
?>
</body>
</html>
退出.php
<?php
session_start();
echo "Logout Successfully ";
session_destroy(); // function that Destroys Session
header("Location: Login.php");
?>
crypt()
哈希函数。这意味着如果凭据被泄露,密码不会轻易被获取。session_start()
。同样地,要销毁会话及其数据,您需要调用session_destroy()
(例如,当用户注销时)。// Begin the session
session_start();
// Use session variables
$_SESSION['userid'] = $userid;
// E.g. find if the user is logged in
if($_SESSION['userid']) {
// Logged in
}
else {
// Not logged in
}
// Destroy the session
if($log_out)
session_destroy();
crypt()
或其他 PHP 通用哈希函数时可能会犯的削弱安全性的错误。有关为什么要使用 password_hash()
而不是 crypt()
的详细信息,请参见 rfc:password_hash。 - Jody Bruchon我一直使用面向对象编程,并使用这个类来维护会话,所以你可以使用函数is_logged_in检查用户是否已登录,如果没有,则可以执行你想要的操作。
<?php
class Session
{
private $logged_in=false;
public $user_id;
function __construct() {
session_start();
$this->check_login();
if($this->logged_in) {
// actions to take right away if user is logged in
} else {
// actions to take right away if user is not logged in
}
}
public function is_logged_in() {
return $this->logged_in;
}
public function login($user) {
// database should find user based on username/password
if($user){
$this->user_id = $_SESSION['user_id'] = $user->id;
$this->logged_in = true;
}
}
public function logout() {
unset($_SESSION['user_id']);
unset($this->user_id);
$this->logged_in = false;
}
private function check_login() {
if(isset($_SESSION['user_id'])) {
$this->user_id = $_SESSION['user_id'];
$this->logged_in = true;
} else {
unset($this->user_id);
$this->logged_in = false;
}
}
}
$session = new Session();
?>
首先,使用此sandbox.io创建一个加盐密码$hash
,并确保用下面的$hash的***
替换结果字符串。
index.php
单页:
<?php
// stackoverflow.com/a/67661402/383904
session_start();
$username = "pony";
$salt = "PonieshaveunicornS"; // Use something really custom instead of ponies
$hash = "***"; // Replace *** with the result of: echo hash("sha256", "myPassword".$salt);
$url = strtok($_SERVER["PHP_SELF"], '?');
$errorMessage = ""; // Used for login error messages
// LOGOUT SYSTEM
if (isset($_GET["p"]) && $_GET["p"] == "logout"):
session_destroy();
header("Location: $url");
exit;
endif;
// LOGIN SYSTEM
if (isset($_POST['un']) && isset($_POST['pw'])):
sleep(3); // Makes it ages slower for rainbow attacks
if ($_POST['un'] == $username && hash("sha256", $_POST['pw'].$salt) == $hash):
$_SESSION['_reg'] = $_POST['un'];
header("Location: $url");
exit;
else:
$message = "Incorrect login data";
endif;
endif;
// DETERMINE A VALID LOGIN
$isLoggedIn = isset($_SESSION['_reg']) && $_SESSION['_reg'] == $username;
// YOUR CUSTOM LOGGED-IN METHODS AND STUFF
if ($isLoggedIn):
// I.e: your DB connections, functions, methods, etc...
endif;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My app</title>
</head>
<body>
<div id="app">
<?php if ($isLoggedIn): ?>
<h1>Welcome <?= $_SESSION["_reg"] ?></h1>
<a href="?p=logout">Logout</a>
<?php else: ?>
<h1>LogIn</h1>
<form action="./index.php" method="post">
<label><input type="text" name="un" placeholder="Username" /></label><br>
<label><input type="password" name="pw" placeholder="Password"/></label><br>
<input type="submit" id="submit" value="LOGIN" />
</form>
<b><?= $errorMessage ?></b>
<?php endif; ?>
</div>
</body>
</html>
免责声明:请注意上述内容
//start use session
$session_start();
extract($_POST);
//extract data from submit post
if(isset($submit))
{
if($user=="user" && $pass=="pass")
{
$_SESSION['user']= $user;
//if correct password and name store in session
}
else {
echo "Invalid user and password";
header("Locatin:form.php");
}
if(isset($_SESSION['user']))
{
//your home page code here
exit;
}
$session_start();
extract($_POST);
//extract data from submit post
if(isset($submit))
{
if($user=="user" && $pass=="pass")
{
$_SESSION['user']= $user;
//if correct password and name store in session
} else {
echo "Invalid user and password";
header("Locatin:form.php")
}
if(isset($_SESSION['user']))
{
}
你需要在页面顶部开始会话或在调用会话代码之前开始会话
session_start();
$session_start();
extract($_POST);
//extract data from submit post
if(isset($submit))
{
if($user=="user" && $pass=="pass")
{
$_SESSION['user']= $user;
//if correct password and name store in session
}
else {
echo "Invalid user and password";
header("Locatin:form.php");
}
if(isset($_SESSION['user']))
{
//your home page code here
exit;
}