当前位置:网站首页>PHP判断用户是否已经登录,如果登录则显示首页,如果未登录则进入登录页面或注册页面
PHP判断用户是否已经登录,如果登录则显示首页,如果未登录则进入登录页面或注册页面
2022-07-29 04:42:00 【清风亦思雨】
login.php
我这里演示用的session代替cookie,思想一样,session操作起来代码更简洁,较容易演示。
<?php
$user = ... ...; //这里是数据库查询的关于用户的数据
if ($user != null || $user != "") {
//这里是设置cookie,使用前需要打开cookie,我这里用的session,思想一样,session简洁,容易演示。
//setcookie("user", $user['user_id'], time() + 1 * 24 * 3600);
//开启session
session_start();
//存入数据
$_SESSION['user'] = $user['user_id'];
//存入后跳转到首页
echo "<script>window.location.href='index.php'</script>";
} else {
echo "<script>alert('账号或密码错误!')</script>";
}
?>index.php
这里是首页,需要有判断来进行,如果通过session查询到有用户信息,则显示对应内容。
?php
//注入数据库操作的PHP文件(操作数据库更加简便),我之前发过关于数据库静态工具类的博客,
//博客链接:https://blog.csdn.net/qfxl0724/article/details/125847351
include_once "DBHelper.php";
//使用session来判断用户是否登录
session_start();
//使用变量接收session中用户的信息
$user = $_SESSION['user'];
if ($user == null || $user == '') {
echo "<a class='login' href='login.php'>登录</a>";
echo "<a class='enroll' href='enroll.php'>注册</a>";
} else {
echo "<a class='myspace' href='user/index.php'>我的首页</a>";
}
?>边栏推荐
- New year's greetings from programmers
- Pyscript cannot import package
- pyscript无法引入包
- Star a pathfinding in LAYA
- Pycharm reports an error when connecting to the virtual machine database
- MySQL - clustered index and secondary index
- EF core: one to one, many to many configuration
- Oracle insert data
- Use of torch.optim optimizer in pytorch
- Detailed comparison of break and continue functions
猜你喜欢

(heap sort) heap sort is super detailed, I don't believe you can't (C language code implementation)

JVM (heap and stack) memory allocation

央企建筑企业数字化转型核心特征是什么?

使用更灵活、更方便的罗氏线圈

STL source code analysis (Hou Jie) notes -- Classification and testing of stl containers

恒星科通邀您“湘”约第24届中国高速公路信息化大会暨技术产品展示会

What is the difference between field, variable and property

【Express连接MySQL数据库】

Make a virtual human with zego avatar | virtual anchor live broadcast solution

Flutter 手势监听和画板实现
随机推荐
Pyqt5 learning pit encounter and pit drainage (1) unable to open designer.exe
es6和commonjs对导入导出的值修改是否影响原模块
Pyqt5 learning pit encounter and pit drainage (2) buttons in qformlayout layout cannot be displayed
leetcode 686.重复叠加字符串 KMP方法(C语言实现)
命令行交互工具(最新版) inquirer 实用教程
Basic grammar of C language
[C] PTA 6-8 finding the height of binary tree
[C language] PTA 7-47 binary leading zero
TypeError: Cannot read properties of undefined (reading ‘then‘)
pulsar起client客户端时(client,producer,consumer)各个配置
iOS面试准备 - ios篇
Webrtc realizes simple audio and video call function
iOS面试准备 - 其他篇
Log configuration logback
Vscode configuration makefile compilation
Common current limiting methods
Understand the Internet giant [the war between China and Taiwan] and the development thinking of China and Taiwan
Pyscript cannot import package
leetcode 763. Partition Labels 划分字母区间(中等)
Vscode one click compilation and debugging