当前位置:网站首页>PHP determines whether the user has logged in. If logged in, the home page will be displayed. If not, enter the login page or registration page
PHP determines whether the user has logged in. If logged in, the home page will be displayed. If not, enter the login page or registration page
2022-07-29 04:45:00 【The breeze also misses the rain】
login.php
I use it here session Instead of cookie, Think the same ,session The code is simpler to operate , Easier to demonstrate .
<?php
$user = ... ...; // Here is the data about users queried by the database
if ($user != null || $user != "") {
// Here are the settings cookie, It needs to be opened before use cookie, I used it here session, Think the same ,session concise , Easy to demonstrate .
//setcookie("user", $user['user_id'], time() + 1 * 24 * 3600);
// Turn on session
session_start();
// In the data
$_SESSION['user'] = $user['user_id'];
// After saving, jump to the home page
echo "<script>window.location.href='index.php'</script>";
} else {
echo "<script>alert(' Wrong account or password !')</script>";
}
?>index.php
This is the home page , Judgment is needed , If you pass session User information is found , Then the corresponding content is displayed .
?php
// Injected into database operations PHP file ( It is easier to operate the database ), I have previously posted a blog about database static tools ,
// Blog links :https://blog.csdn.net/qfxl0724/article/details/125847351
include_once "DBHelper.php";
// Use session To determine whether the user is logged in
session_start();
// Use variables to receive session Information of users in
$user = $_SESSION['user'];
if ($user == null || $user == '') {
echo "<a class='login' href='login.php'> Sign in </a>";
echo "<a class='enroll' href='enroll.php'> register </a>";
} else {
echo "<a class='myspace' href='user/index.php'> My home page </a>";
}
?>边栏推荐
猜你喜欢
随机推荐
[C language] PTA 7-47 binary leading zero
Idea small settings
(heap sort) heap sort is super detailed, I don't believe you can't (C language code implementation)
ssm整合增删改查
def fasterrcnn_resnet50_fpn()实例测试
Mongo Shell交互式命令窗口
安装spinning up教程里与mujoco对应的gym,报错mjpro150
Leetcode (Sword finger offer) - 53 - I. find the number I in the sorted array
Webrtc realizes simple audio and video call function
Makefile(make)常见规则(二)
MySQL - 深入解析MySQL索引数据结构
Mysql:The user specified as a definer (‘root‘@‘%‘) does not exist 的解决办法
[c language] use the reverse order output of the linked list (bidirectional linked list)
STL source code analysis (Hou Jie) notes -- Classification and testing of stl containers
[c language] PTA 7-63 falling ball
The daily life of programmers
EF core: one to one, many to many configuration
PHP判断用户是否已经登录,如果登录则显示首页,如果未登录则进入登录页面或注册页面
2022杭电多校联赛第四场 题解
Flutter实战-请求封装(二)之dio









