关于apache+php用户验证

ops/2024/9/23 3:27:33/

一.直接在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/ops/30261.html

相关文章

【PyTorch与深度学习】4、PyTorch的Dataset与DataLoader详细使用教程

课程地址 最近做实验发现自己还是基础框架上掌握得不好,于是开始重学一遍PyTorch框架,这个是课程笔记,这个课还是讲的简略,我半小时的课听了一个半小时。 1. Dataset与DataLoader Dataset类是处理单个训练样本的,也就…

人工智能_大模型044_模型微调004_随机梯度下降优化_常见损失计算算法_手写简单神经网络_实现手写体识别---人工智能工作笔记0179

然后对于,梯度下降,为了让训练的速度更好,更快的下降,又做了很多算法,可以看到 这里要知道Transformer中最常用的Adam 和 AdamW这两种算法. 当然,这些算法都是用于优化神经网络中的参数,以最小化损失函数。下面我会尽量以通俗易懂的方式解释它们的原理和适用场景。 1. **L-…

JAVA:jsp+springboot 配置maven兼容版本

Java17 maven依赖&#xff1a;如果中央库和其他镜像找不到包&#xff0c; 可以访问下面的网址找替代包 <!-- Maven Repository: Search/Browse/Explore (mvnrepository.com) -->spring-boot版本号3.2.51.无需配置驱动&#xff0c;有内置数据库驱动 2.能自动扫描配置类。b…

欧拉回路(leetcode 重新安排行程)

先学习一下欧拉回路是怎么一回事。 对于图中这七个节点&#xff0c;从节点1出发&#xff0c;最终要到达节点1&#xff0c;并且每条路只能走一次&#xff0c;且每条路都得走过一次。 使用dfs&#xff0c;如果算法按照字典序的排列方式选择下一个节点。 第一部分&#xff1a;那…

分布式WEB应用中会话管理的变迁之路

优质博文&#xff1a;IT-BLOG-CN Session一词直译为“会话”&#xff0c;意指有始有终的一系列动作&#xff0f;消息。Session是Web应用蓬勃发展的产物之一&#xff0c;在Web应用中隐含有“面向连接”和“状态保持”两个含义&#xff0c;同时也指代了Web服务器与客户端之间进行…

C++ 智能指针

智能指针是针对内存泄漏的问题进行处理。 场景1 我们写一个模拟除法函数&#xff0c;调用一下。该函数会对除数为0的情况抛异常&#xff1a; #include<iostream> using namespace std;double chu(int a, int b) {if (b 0){throw invalid_argument("除数不能为0&a…

煤矿综合自动化智能监控系统

系统概述 建设煤矿井上下工业环网、工业数据集成平台、排水、供电、运输、通风、压风、瓦斯抽放、采掘、智能洗煤厂等智能自动化控制系统&#xff0c;利用多种软硬件接口(OPC协议、驱动通讯、数据库、文本文件、DDE/NETDDE、子网等)&#xff0c;构建全矿井统一、稳定、高效的数…

BJFUOJ-C++程序设计-实验3-继承和虚函数

A TableTennisPlayer 答案&#xff1a; #include<iostream> #include<cstring> using namespace std;class TableTennisPlayer{ private:string firstname;string lastname;bool hasTable;public:TableTennisPlayer(const string &, const string &, bool…