当前位置:网站首页>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>";
}
?>边栏推荐
- def fasterrcnn_resnet50_fpn()实例测试
- i++与++i详解
- Install the gym corresponding to mujoco in the spinning up tutorial, and the error mjpro150 is reported
- 钉钉对话框文子转换成图片 不能复制粘贴到文档上
- Exception handling: pyemd or pyemd not found
- iOS面试准备 - ios篇
- img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
- Simply change the picture color
- Classes and objects (III)
- LeetCode_ Stack topics
猜你喜欢

如何避免示波器电流探头损坏

6. Pytest generates an allure Report

UE plays video in scene or UMG

Ethernet of network

Correct user dragging method

MySQL - clustered index and secondary index

On the use of pyscript (realizing office preview)

WebRTC实现简单音视频通话功能
![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

ssm整合增删改查
随机推荐
Integration of Nan causes in pytorch training model
Pytorch GPU and CPU models load each other
OpenCV环境搭建
TypeError: Cannot read properties of undefined (reading ‘then‘)
File operation (Advanced C language)
Use of construction methods
Dabao and Erbao
Basic operation of queue
MySQL - deep parsing of MySQL index data structure
iOS面试准备 - ios篇
STL source code analysis (Hou Jie) notes -- Classification and testing of stl containers
Basic grammar of C language
On quotation
用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
[c language] PTA 7-51 sum the first n terms of odd part sequence
LeetCode_ Stack topics
def fasterrcnn_resnet50_fpn()实例测试
Christmas tree web page and Christmas tree application
Whether the modification of import and export values by ES6 and commonjs affects the original module
Make a virtual human with zego avatar | virtual anchor live broadcast solution