关于apache+php用户验证

embedded/2024/10/11 7:34:50/

一.直接在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/embedded/26272.html

相关文章

Python.第六章(函数)

# 第六章 函数# 在Python语言中,定义函数的语法格式如下: # def 函数名([参数列表]): # 函数体# [注意] # (1)圆括号内是形参列表,如果有多个参数则使用逗号分隔开, # 即使该函数 # 不需要接收任何参数,也必须保留一对空的圆…

阿里云详细介绍,与AWS和GCP比较

一、阿里云详解 阿里云(Alibaba Cloud),也被称为阿里巴巴云计算,是中国最大的云服务提供商,同时在全球范围内也具有显著的市场影响力。自2009年成立以来,阿里云已经发展成为一个提供全方位服务的云平台&am…

使用ganache实现Web3js和区块链交互的步骤 及问题解决:Command ‘express’ not found等

Web3js和区块链交互 做一个简单的dapp 1.express安装 sudo npm install express -g 出现问题:Command ‘express’ not found, 解决:在安装express时增加generator参数: npm install -g express-generator 成功后使用 express -e MyDa…

DS高阶:图论基础知识

一、图的基本概念及相关名词解释 1.1 图的基本概念 图是比线性表和树更为复杂且抽象的结,和以往所学结构不同的是图是一种表示型的结构,也就是说他更关注的是元素与元素之间的关系。下面进入正题。 图是由顶点集合及顶点间的关系组成的一种数据结构&…

react中useReducer如何使用

useReducer 是 React 提供的一个用于状态管理的 Hook,它接收一个 reducer 函数和初始状态作为参数,并返回当前状态以及一个 dispatch 函数。这个 Hook 适用于管理复杂或嵌套的状态对象,它提供了一种更加结构化的更新状态的方法。 useReducer是…

centos按照mysql

mysql 下载 步骤: Select Operating System: Linux - Generic 下载Linux - Generic (glibc 2.12) (x86, 64-bit), Compressed TAR Archive 5.7.38 643.6M 下载地址: https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.ta…

基于Spring Boot的校园博客系统设计与实现

基于Spring Boot的校园博客系统设计与实现 开发语言:Java框架:springbootJDK版本:JDK1.8数据库工具:Navicat11开发软件:eclipse/myeclipse/idea 系统部分展示 系统功能界面图,在系统首页可以查看首页、文…

OpenStack云计算(十四)——综合演练手动部署OpenStack,

本项目的项目实训可以完全参考教材配套讲解的详细步骤实施,总体来说实训工作量较大,可根据需要选做,重点观看配套的微课视频。 项目实训一 【实训题目】 搭建OpenStack云平台基础环境 【实训目的】 掌握OpenStack基础环境的安装和配置方…