Example to solve the problem that nginx does not support pathinfo in TP

*文
Release: 2023-03-18 16:04:01
Original
1767 people have browsed it

nginx是不支持TP中pathinfo的,如何解决呢?本文主要介绍如何解决当Nginx不支持pathinfo问题时该如何解决。希望对大家有所帮助。

下面小编通过文字加代码的方式给大家详解下,具体内容如下:

其实,要解决nginx不支持pathinfo的问题,有两个解决思路,一是不使用pathinfo模式,二是修改nginx的配置文件,使它支持pathinfo。为了使问题简单化,我选择了第一种方式,因为就第二种方式,我查了很多资料,发现大家的方法不尽相同,有的还差别很大,容易造成误导,所以我选择从简出发,选择普通模式,虽然有一定的风险。当把index.php对应的前台代码修改完毕之后,发现前台基本正常,可是后台仍然出现重定向的问题。折腾了半天之后,我才想到看一下日志文件,原来是编辑器的问题,看来日志文件真的很重要,以前一直不重视。在config.php文件的第一行出现了输出,

在sublime下,一般会为UTF-8文件添加BOM头,这个BOM头在window下通常是看不见的,可以通过其他的编辑器查看到,Linux下也可以直接看到,通常显示出来是一个乱码字符,把这个字符删除即可,或者简单一点,直接在第一行回车,再删除就可以了。到这里,后台基本可以访问了。

1.在登录的时候,我是通过外部js文件发送Ajax请求进行验证的,在js与ThinkPHP模块函数通信遇到了点问题,一直不知道正确的路径该怎么写,也没有查到相关资料,只能各种试,好在找到了解决办法,通过直接带上入口文件名的方式

var url="system.php?m=Login&a=doLog"; 
 $.post(url,{"staffname":$staffname,"staffpwd":$staffpwd,"verifycode":$verifycode},function(data){ 
  if(data=="codeerr"){ 
   alert("验证码错误!"); 
  }else if(data=="authempty"){ 
   alert("请输入用户名或密码!") 
  }else if(data=="autherr"){ 
   alert("用户名或密码错误!"); 
  }else if(data=="success"){ 
   alert("登录成功!"); 
   location.href="system.php?m=Index&a=index"; //访问首页 
  }
Copy after login

当然,此为普通模式下的访问方式,如果是pathinfo的话,只需要把红色部分如下修改即可

var url="doLog"; 
 $.post(url,{"staffname":$staffname,"staffpwd":$staffpwd,"verifycode":$verifycode},function(data){ 
  if(data=="codeerr"){ 
    alert("验证码错误!"); 
  }else if(data=="authempty"){ 
    alert("请输入用户名或密码!") 
  }else if(data=="autherr"){ 
    alert("用户名或密码错误!"); 
  }else if(data=="success"){ 
    alert("登录成功!"); 
    location.href="../Index/index"; //跳转首页,访问其他模块的方法
Copy after login

2.下载文件的时候,总是莫名多出许多html的东西,原因是缓冲区没有清空,可以通过以下代码进行修改,不过这种方式实际上是下载的仍然是html格式的文件,只不过改了一下后缀名为xls而已,因而用excel打开的时候会提示格式问题,忽略即可。同时需要注意使用 icov()函数转换编码,因为xls默认编码格式并非utf-8.

ob_start(); 
ob_end_clean(); 
Header( "Content-type: application/octet-stream"); 
Header( "Accept-Ranges: bytes "); 
Header( "Content-type:application/vnd.ms-excel;charset=gb2312");  
Header( "Content-Disposition:attachment;filename={$filename}.xls");
Copy after login

3.在删除文件时会遇到路径问题,因为项目中使用的较多的是相对路径,即相对入口文件而言,但是删除文件则需要使用绝对路径,我并没有找到合适的解决方法,只好用了比较保守的方式

$path="./Public/uploads/";      
$path=str_replace("<a target="_blank" href="file:///" style="color:rgb(51,119,170); text-decoration:none">\\","/",realpath($path)."/</a>");  //获取绝对路径,并转换分隔符
Copy after login

4.在配置nginx和php方面,我使用了fastCGI的方式,将如下代码保存为cmd文件,直接点击运行就可以了
"F:\php\php-cgi.exe" -b 127.0.0.1:9000 -c "F:\php\php.ini" //后面是php文件的路径
然后在nginx的配置文件里加上几句话

location ~ \.php/?.* { 
   root   myapplications; 
   fastcgi_pass 127.0.0.1:9000; 
    fastcgi_index index.php; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
   include  fastcgi_params; 
   #定义变量 $path_info ,用于存放pathinfo信息 
    set $path_info ""; 
    #定义变量 $real_script_name,用于存放真实地址 
    set $real_script_name $fastcgi_script_name; 
    #如果地址与引号内的正则表达式匹配 
    if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") { 
      #将文件地址赋值给变量 $real_script_name 
      set $real_script_name $1; 
      #将文件地址后的参数赋值给变量 $path_info 
      set $path_info $2; 
    } 
    #配置fastcgi的一些参数 
    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name; 
    fastcgi_param SCRIPT_NAME $real_script_name; 
    fastcgi_param PATH_INFO $path_info; 
   }
Copy after login

相关推荐:

php pathinfo()函数获取文件路径信息用法总结

使用php pathinfo(), parse_url(), basename()函数解析URL实例讲解

[PHP] 命令行执行整合pathinfo模拟定时任务

The above is the detailed content of Example to solve the problem that nginx does not support pathinfo in TP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!