繁体   English   中英

使用斜杠获取PHP中的URL参数

[英]Get URL Parameter in PHP using slash

会使用类似http:// localhost / biyahero / user / mark这样的URL参数,而不是使用此http://localhost/biyahero/user.php?name = mark

我已经试过了

 $test=explode( '/', $_SERVER['REQUEST_URI'] );
echo $test[3]; 

它工作正常,但是当我即时访问实际页面时,css和js均无法正常工作。 我想念什么?

您必须使用绝对URL来加载css和js文件。

而不是像这样的相对URL:

../../path/to/cssFile.css

使用绝对网址:

/path/to/cssFile.css

您需要服务器的PATH_INFO变量才能使其正常工作。 但是请注意,您还需要相应地配置服务器。

然后在PHP中可以这样调用pathinfo()

<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>

Apache的服务器配置

# Set document root to be "webroot"
DocumentRoot "path/to/webroot"
<Directory "path/to/webroot">
    # use mod_rewrite for pretty URL support
    RewriteEngine on
    # If a directory or a file exists, use the request directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Otherwise forward the request to index.php
    RewriteRule . index.php

    # ...other settings...
</Directory>

和Nginx:

server {
    # ...

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    # ...
}

警告:如果配置不正确,可能会引起一些安全问题,请确保:

  1. 应用此规则后,将永远无法访问用户上传文件
  2. 设置["cgi.fix_pathinfo = 0;" in php.ini][2] ["cgi.fix_pathinfo = 0;" in php.ini][2]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM