当前位置:网站首页>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>";
}
?>边栏推荐
- Oracle 插入数据
- ios面试准备 - objective-c篇
- Nail dialog text converted to pictures cannot be copied and pasted on the document
- File operation (Advanced C language)
- The daily life of programmers
- Mysql:The user specified as a definer (‘root‘@‘%‘) does not exist 的解决办法
- Simply change the picture color
- There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)
- Corresponding order of 18 and 25coco data of openpose and joint points
- [C language] PTA 7-47 binary leading zero
猜你喜欢

Google browser opens the web page and out of memory appears

On quotation
![Understand the Internet giant [the war between China and Taiwan] and the development thinking of China and Taiwan](/img/6c/f24407133663c0e19d6fa05c611341.png)
Understand the Internet giant [the war between China and Taiwan] and the development thinking of China and Taiwan

论pyscript使用感想(实现office预览)

Flutter实战-请求封装(二)之dio

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

Flutter 手势监听和画板实现

MySQL - deep parsing of MySQL index data structure

The third ACM program design competition of Wuhan University of Engineering

Detailed comparison of break and continue functions
随机推荐
UE 在场景或UMG中播放视频
Pytoch automatic mixing accuracy (AMP) training
The most complete NLP Chinese and English stop words list in the whole station (including punctuation marks, which can be copied directly)
删除word文档中的空白页
读懂 互联网巨头 【中台之战】 以及 中台 发展思维
Christmas tree web page and Christmas tree application
常见的限流方式
Use of construction methods
STL source code analysis (Hou Jie) notes -- Classification and testing of stl containers
Tower of Hanoi classic recursion problem (C language implementation)
Flutter 手势监听和画板实现
Idea small settings
Deploy Jenkins using containers
I++ and ++i details
File operation (Advanced C language)
Delete blank pages in word documents
Webrtc realizes simple audio and video call function
Log configuration logback
6. Pytest generates an allure Report
pyscript无法引入包