关于apache+php用户验证

server/2024/9/22 15:37:40/

一.直接在apache配置配置用户信息

1.apache配置可以参考外部文档

https://developer.aliyun.com/article/507049

2.上面配置好在php获取用户信息(登录apache会拦截)

     $userName = $_SERVER['PHP_AUTH_USER'];$password = $_SERVER['PHP_AUTH_PW'];

二.上面直接用apache配置登录拦截,项目所有的路由都被登录拦截,但是有些路由不希望被登录拦截只能在php做登录拦截

1.apache只管用.htpasswd 配置用户密码就行

2.直接在php用exec来执行验证账号和密码登录,用$_SESSION来记录登录用户

   //这里做登录拦截,首页/index会被isLogin登录拦截public function isLogin(){//点击退出登录时if(!empty($_SESSION['logout'])){$this->linkLoginPage();$_SESSION['logout'] = false;exit;}//没有登录成功,重新登录if (!isset($_SERVER['PHP_AUTH_USER'])) {$this->linkLoginPage();exit;} else {$userName = $_SERVER['PHP_AUTH_USER'];$password = $_SERVER['PHP_AUTH_PW'];//如果登录的用户跟之前session的用户一样,就不需要验证了,证明之前已经验证过账号密码了,否则验证登录账号和密码是否正确if(!(!empty($_SESSION['username']) && $_SESSION['username'] == $userName)){exec("htpasswd -vb /etc/httpd/.htpasswd $userName $password",$output,$code);/*  状态码解释(0是成功,1一般可能是文件没有权限,文件/etc/httpd/.htpasswd,文件没权限的话设置apache:chown apache:apache /etc/httpd/.htpasswd或者设置为最高权限:chmod 777 /etc/httpd/.htpasswd):*  (*    查看命令来源:whereis htpasswd(例如返回:/usr/bin/htpasswd /usr/share/man/man1/htpasswd.1.gz)*    可以解压看一下/usr/share/man/man1/htpasswd.1.gz命令code说明,解压后里面文件有下面说明:*       htpasswd returns a zero status ("true") if the username and password have been successfully added or updated in the \fIpasswdfile\fR\&. htpasswd returns 1 if it encounters some problem accessing files, 2 if there was a syntax problem with the command line, 3 if the password was entered interactively and the verification entry didn't match, 4 if its operation was interrupted, 5 if a value is too long (username, filename, password, or final computed record), 6 if the username contains illegal characters (see the Restrictions section), and 7 if the file is not a valid password file\&.*  )** 备注:*      1.在机器直接执行命令是有输出的,但在代码执行我看$output输出是空的*      2.关于exec命令有疑问可以看一下博客:https://blog.itpub.net/8227599/viewspace-934479/*/if($code ===0){$_SESSION['username'] = $userName;}else{$this->linkLoginPage();exit;}}}}public function linkLoginPage(){header('WWW-Authenticate: Basic realm="My Realm"');header('HTTP/1.0 401 Unauthorized');}/** 路由:/logout* 退出登录*/public function logout(){unset($_SESSION['username']);//标记状态$_SESSION['logout'] = true;//跳转回首页,让首页执行重新登录,如果在当前路由执行登录,登录成功还是在当前路由header("location:/index");}


http://www.ppmy.cn/server/25467.html

相关文章

计算机常识 | 快速格式化、擦除格式化、覆盖格式化 | 直连电脑可相互ping通

文章目录 一、快速格式化、擦除格式化、覆盖格式化二、两台没有联网的设备通过网线直接相连能够相互ping通的原因 一、快速格式化、擦除格式化、覆盖格式化 快速格式化、擦除格式化和覆盖格式化是针对计算机存储设备(如硬盘驱动器或固态硬盘)上数据删除和…

P9586 「MXOI Round 2」游戏

「MXOI Round 2」游戏 题目描述 小 C 和小 D 正在玩一款蒸蒸日上的游戏。 这款游戏共有 3 3 3 种手牌:杀、闪、斩。他们的用途分别如下: 杀:对对方使用,对方需要使用一张闪,否则对方输掉游戏;或回应对方…

基于VMD-CNN-BiLSTM-Attention组合模型时间序列预测

VMD-CNN-BiLSTM-Attention组合模型是一种复杂的神经网络结构,用于时间序列预测。让我逐步解释这个模型的每个组成部分以及它们是如何结合在一起的: VMD(Variational Mode Decomposition):VMD是一种信号处理技术&#x…

串口通信标准RS232 RS485 RS422的区别

很多工程师经常把RS-232、RS-422、RS-485称为通讯协议,其实这是不对的,它们仅仅是关于串口通讯的一个机械和电气接口标准(顶多是网络协议中的物理层),不是通讯协议,那它们又有哪些区别呢: 第一…

Python搭建http下载服务器

import http.server import socketserverPORT 8002Handler http.server.SimpleHTTPRequestHandlerwith socketserver.TCPServer(("", PORT), Handler) as httpd:print("serving at port", PORT)httpd.serve_forever()使用: 保存为httpserver.…

webpack3插件CommonChunkPlugin分离vantUI和echarts,问题的webpackJsonp is not defined解决!!!

webpack3插件CommonChunkPlugin分离vantUI和echarts和报错webpackJsonp is not defined的解决 前景:因为项目使用的webpack3开发的场景,打包后的vendor很大,如图显示 如果不做gzip处理的话,大小在2M多,gzip后的大小是…

【uniapp/ucharts】采用 uniapp 框架的 h5 应用使用 ucharts(没有 uni_modules)

这种情况无法直接从 dcloud 平台上一键下载导入,所以应该在官网推荐的 git 仓库去单独下载: https://gitee.com/uCharts/uCharts/tree/master/uni-app/uCharts-%E7%BB%84%E4%BB%B6/qiun-data-charts(%E9%9D%9Euni_modules) 下载的文件是如图所示的路径&…

Kubernetes 声明式语言 YAML

什么是 YAML YAML(YAML Ain’t Markup Language)是一种可读的数据序列化语言,通常用于配置文件、数据序列化和交换格式。YAML 的设计目标是易读易写,并且能够映射到动态语言中的数据结构 YA加粗样式ML 是 JSON 的超集&#xff0…