PHP gets current url path function and server variable introduction

WBOY
Release: 2016-07-25 09:00:00
Original
1066 people have browsed it
I will introduce to you the function to obtain the current url path in PHP and some server variables in PHP for reference and learning by friends in need.

Mainly use these: $_SERVER["QUERY_STRING"],$_SERVER["REQUEST_URI"],$_SERVER["SCRIPT_NAME"],$_SERVER["PHP_SELF"]

1,$_SERVER["QUERY_STRING"] Description: Query string

2,$_SERVER["REQUEST_URI"] Description: URI required to access this page

3,$_SERVER["SCRIPT_NAME"] Description: Contains the path of the current script

4,$_SERVER["PHP_SELF"] Description: The file name of the currently executing script

Example: 1. http://bbs.it-home.org/ (open the homepage directly) result:

$_SERVER["QUERY_STRING"] = ""
$_SERVER["REQUEST_URI"] = "/"
$_SERVER["SCRIPT_NAME"] = "/index.php"
$_SERVER["PHP_SELF"]     = "/index.php"
Copy after login

2, http://bbs.it-home.org/?p=222 (with query) result:

$_SERVER["QUERY_STRING"] = "p=222"
$_SERVER["REQUEST_URI"] = "/?p=222"
$_SERVER["SCRIPT_NAME"] = "/index.php"
$_SERVER["PHP_SELF"]     = "/index.php"
Copy after login

3, http://bbs.it-home.org/index.php?p=222&q=biuuu Result:

$_SERVER["QUERY_STRING"] = "p=222&q=biuuu"
$_SERVER["REQUEST_URI"] = "/index.php?p=222&q=biuuu"
$_SERVER["SCRIPT_NAME"] = "/index.php"
$_SERVER["PHP_SELF"]     = "/index.php"

$_SERVER["QUERY_STRING"]获取查询语句,实例中可知,获取的是?后面的值
$_SERVER["REQUEST_URI"] 获取http://bbs.it-home.org后面的值,包括/
$_SERVER["SCRIPT_NAME"] 获取当前脚本的路径,如:index.php
$_SERVER["PHP_SELF"] 当前正在执行脚本的文件名
Copy after login

Current url: "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']

Summary: A deep understanding of QUERY_STRING, REQUEST_URI, SCRIPT_NAME and PHP_SELF is recommended. It is recommended to have a deep understanding of the differences between the four variables QUERY_STRING, REQUEST_URI, SCRIPT_NAME and PHP_SELF in the $_SERVER function.

$_SERVER["REQUEST_URI"]: Get the complete url of the current request (except the domain name). . .

Processing skills in uchome system:

//处理REQUEST_URI
if(!isset($_SERVER['REQUEST_URI'])) {  
    $_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'];
    if(isset($_SERVER['QUERY_STRING'])) $_SERVER['REQUEST_URI'] .= '?'.$_SERVER['QUERY_STRING'];
}
if($_SERVER['REQUEST_URI']) {
    $temp = urldecode($_SERVER['REQUEST_URI']);
    if(strexists($temp, '<') || strexists($temp, '"')) {
        $_GET = shtmlspecialchars($_GET);//XSS
    }
}
Copy after login
<?php
echo $_SERVER['DOCUMENT_ROOT']."<br>"; //获得服务器文档根变量
echo $_SERVER['PHP_SELF']."<br>"; //获得执行该代码的文件服务器绝对路径的变量
echo __FILE__."<br>"; //获得文件的文件系统绝对路径的变量
echo dirname(__FILE__); //获得文件所在的文件夹路径的函数
?>
Copy after login

//server function $_SERVER["HTTP_REFERER"]=http://localhost/lianxi/ $_SERVER["HTTP_ACCEPT_LANGUAGE"]=zh-cn $_SERVER["HTTP_ACCEPT_ENCODING"]=gzip, deflate $_SERVER["HTTP_USER_AGENT"]=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727) $_SERVER["HTTP_HOST"]=localhost $_SERVER["HTTP_CONNECTION"]=Keep-Alive $_SERVER["PATH"]=C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:Program FilesCommon FilesAdobeAGL;C:Program FilesMySQLMySQL Server 5.0bin;C:php;C:phpext $_SERVER["SystemRoot"]=C:WINDOWS $_SERVER["COMSPEC"]=C:WINDOWSsystem32cmd.exe $_SERVER["PATHEXT"]=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH $_SERVER["WINDIR"]=C:WINDOWS $_SERVER["SERVER_SIGNATURE"]= Apache/2.0.55 (Win32) PHP/5.1.1 Server at localhost Port 80 \What server is used $_SERVER["SERVER_SOFTWARE"]=Apache/2.0.55 (Win32) PHP/5.1.1 $_SERVER["SERVER_NAME"]=localhost \server name $_SERVER["SERVER_ADDR"]=127.0.0.1 $_SERVER["SERVER_PORT"]=80 \server port $_SERVER["REMOTE_ADDR"]=127.0.0.1 $_SERVER["DOCUMENT_ROOT"]=D:/lianxi \Home directory of the website $_SERVER["SERVER_ADMIN"]=sss@163.com \The email address set when installing APACHE $_SERVER["SCRIPT_FILENAME"]=D:/lianxi/lianxi/servervalues.php \The absolute path of the current web page, $_SERVER["REMOTE_PORT"]=1076 \remote port $_SERVER["GATEWAY_INTERFACE"]=CGI/1.1 $_SERVER["SERVER_PROTOCOL"]=HTTP/1.1 $_SERVER["REQUEST_METHOD"]=GET $_SERVER["QUERY_STRING"]=\Get? The content after the number $_SERVER["REQUEST_URI"]=Example:/lianxi/servervalues.php?a=1&b=2 $_SERVER["SCRIPT_NAME"]=Example:/lianxi/servervalues.php $_SERVER["PHP_SELF"]=/lianxi/servervalues.php \returns the relative path of the current web page. $_SERVER["REQUEST_TIME"]=1179190013 \Running time unit is one hundred thousandth of a millisecond $_SERVER["argv"]=Array $_SERVER["argc"]=0 1.$_SERVER["QUERY_STRING"] Description: Query string 2.$_SERVER["REQUEST_URI"] Description: URI required to access this page 3.$_SERVER["SCRIPT_NAME"] Description: Contains the path of the current script 4.$_SERVER["PHP_SELF"] Description: The file name of the currently executing script


<?php
/**
__FILE__ ,
getcwd(),
$_SERVER["REQUEST_URI"],
$_SERVER["SCRIPT_NAME"],
$_SERVER["PHP_SELF"],
$_SERVER["SCRIPT_FILENAME"],
来观察一下这些变量或函数的异同.
假设有一个请求地址为: http://localhost:8080/test.php/age=20
而test.php 的完整路径是: D:/server/www/example/test.php
1) getcwd()
将得到浏览器请求的页面文件所在的目录. 即test.php 文件所在的目录: D:/server/www/example/ ,
如果在test.php 执行了 require 或 include 语句, 比如 inculde(”test_dir/test2.php”),
那么在 test2.php 里 getcwd()函数 返回的也将是 test.php 所在的目录.
2) __FILE__
一个魔术变量, 用它将得到 __FILE__ 变量所在文件的完整路径,
比如: test.php 里 __FILE__ 将得到 D:/server/www/example/test.php ,
test_dir/test2.php 里的 __FILE__ 将得到 D:/server/www/example/test_dir/test2.php

3) $_SERVER["SCRIPT_FILENAME"]
将得到浏览器请求的页面文件的完整路径.
test.php 和 test_dir/test2.php 里用 $_SERVER["SCRIPT_NAME"] 都将得到 D:/server/www/example/test.php.

4) $_SERVER["SCRIPT_NAME"]
将得到浏览器请求的页面文件的文件名,注意: 与 $_SERVER["SCRIPT_NAME"] 不同, 此变量只得到文件名而不包含路径,
在test.php 与 test_dir/test2.php 用$_SERVER["SCRIPT_NAME"] 得到的都将是 test.php.
当然, 在test.php 与 test_dir/test2.php 执行 basename($_SERVER["SCRIPT_FILENAME"]) 与 $_SERVER["SCRIPT_NAME"] 相同.
执行 在test.php 与 test_dir/test2.php 执行 realpath(”test.php”) 得到的结果与 $_SERVER["SCRIPT_FILENAME"] 相同.

5) $_SERVER["PHP_SELF"]
将得到浏览器请求页面的文件名, 并剥掉问号 ? 后的内容, 注意:不包含路径,
比如在客户端里请求 http://localhost:8080/test.php?age=20&name=Tom,
那么test.php 和 test_dir/test2.php 的 $_SERVER["PHP_SELF"] 都将得到 “test.php”。“age=20&name=Tom”被剥掉。
而如果客户端里请求 http://localhost:8080/test.php/age=20&name=Tom,
那么test.php 和 test_dir/test2.php 的 $_SERVER["PHP_SELF"] 都将得到 “test.php/age=20&name=Tom”。

6) $_SERVER["REQUEST_URI"]
将得到浏览器请求页面的文件名, 以及文件名之后的所有内容(注意: 井号 # 之后的内容将被略去),
比如在客户端里请求 http://localhost:8080/test.php?age=20&name=Tom,
那么test.php 和 test_dir/test2.php 的 $_SERVER["REUEST_URI"] 都将得到 “test.php”。“age=20&name=Tom”被剥掉。
而如果客户端里请求 http://localhost:8080/test.php/age=20&name=Tom,
那么test.php 和 test_dir/test2.php 的 $_SERVER["REQUEST_URI"] 都将得到 “test.php/age=20&name=Tom”。
*/
// test.php:
echo “test1.php variables <br />”;
echo “getcwd: “, getcwd(), “<br />”;
echo “__FILE__: “, __FILE__, “<br />”;
echo “REQUEST_URI: “, $_SERVER["REQUEST_URI"], “<br />”;
echo “SCRIPT_NAME: “, $_SERVER["SCRIPT_NAME"], “<br />”;
echo “PHP_SELF: “, $_SERVER["PHP_SELF"], “<br />”;
echo “SCRIPT_FILENAME “, $_SERVER["SCRIPT_FILENAME"] , “<br />”;

// 把 test2.php 包含进来, 在 test2.php 里输出上面的变量,看有什么不同:
include_once(”test2/test2.php”);
?>
Copy after login

The above is the entire content of today’s php tutorial. There are a lot of things and it’s a bit messy, but they are indeed good things. Let’s absorb the nutrition of knowledge with your heart, haha.



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