php get path

WBOY
Release: 2016-08-08 09:22:11
Original
1171 people have browsed it

php gets the url path of the current page

#测试网址:     http://localhost/blog/testurl.php?id=5
 
//获取域名或主机地址 
echo $_SERVER['HTTP_HOST'].""; #localhost
 
//获取网页地址 
echo $_SERVER['PHP_SELF'].""; #/blog/testurl.php
 
//获取网址参数 
echo $_SERVER["QUERY_STRING"].""; #id=5
 
//获取用户代理 
echo $_SERVER['HTTP_REFERER'].""; 
 
//获取完整的url
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
#http://localhost/blog/testurl.php?id=5
 
//包含端口号的完整url
echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
#http://localhost:80/blog/testurl.php?id=5
 
//只取路径
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; 
echo dirname($url);
#http://localhost/blog
Copy after login

php gets the absolute path of the current file

<?php 
  echo __FILE__ ; // 取得当前文件的绝对地址,结果:D:\www\test.php 
  echo dirname(__FILE__); // 取得当前文件所在的绝对目录,结果:D:\www\ 
  echo dirname(dirname(__FILE__)); //取得当前文件的上一层目录名,结果:D:\ 
?> 
Copy after login
usage tips,
dirname(__FILE__) gets the absolute path of the current file, that is to say, compared to the relative path, search The speed is the fastest.
If you repeat it once, you can move the directory up a level:
For example: $d = dirname(dirname(__FILE__));
In fact, you give a directory as a parameter to dirname(). Because dirname() returns the last directory without \ or /
, so when it is used repeatedly, it can be considered that dirname() treats the lowest directory as a file name. Return to
the upper-level directory of the current directory as usual. By repeating this way, you will get its upper-level directory.
Include the file that got the upper-level directory
include(dirname(__FILE__).'/../filename.php'); The path of
__FILE__ is the file where the current code is located
dirname(dirname(__FILE__)); what you get is the directory name of the layer above the file
dirname(__FILE__); what you get is the directory name of the layer where the file is

The above introduces the PHP acquisition path, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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