php 各种环境变量集合贴

__DIR__

  • __DIR__ 返回 自身路径
    • 示例:echo __DIR__;
    • 输出:D:\ECOVACS\PHPServer\httpd-2.4.41-o111c-x64-vc15-r2\Apache24\htdocs\wdstest\wp-content\plugins\xyx115\ui

__File__

  • __File__ 返回 自身完整路径
    • 示例:echo __FILE__;
    • 输出:D:\ECOVACS\PHPServer\httpd-2.4.41-o111c-x64-vc15-r2\Apache24\htdocs\wdstest\wp-content\plugins\xyx115\ui\tablepress_table.php
    • 示例:echo dirname(echo __FILE__);
    • 输出:D:\ECOVACS\PHPServer\httpd-2.4.41-o111c-x64-vc15-r2\Apache24\htdocs\wdstest\wp-content\plugins\xyx115\ui
    • 示例:echo basename(echo __FILE__);
    • 输出:tablepress_table.php
    • 示例:echo pathinfo(__FILE__, PATHINFO_FILENAME);
    • 输出:tablepress_table
    • 示例:echo substr(basename(__FILE__), strrpos(basename(__FILE__), ".") + 1);
    • 输出:PHP
    • 示例:echo preg_replace("/\.[^.]*$/", "", __FILE__);
    • 输出:D:\ECOVACS\PHPServer\httpd-2.4.41-o111c-x64-vc15-r2\Apache24\htdocs\wdstest\wp-content\plugins\xyx115\ui\tablepress_table
    • 示例:echo preg_replace("/\.[^.]*$/", "", basename(__FILE__));
    • 输出:tablepress_table

pathinfo

<?php 



$File = __FILE__;// 返回 自身完整路径
$File_info = pathinfo($File);
dump($File_info);



?>
<?php 



Array
(
	[dirname] => D:\ECOVACS\PHPServer\httpd-2.4.41-o111c-x64-vc15-r2\Apache24\htdocs\wdstest\wp-content\plugins\xyx115\ui
	[basename] => tablepress_table.php
	[extension] => php
	[filename] => tablepress_table
)



?>

parse_url

<?php 



$Url = "http://test.com/?myp02=%e6%b5%8b%e8%af%95%e7%bd%91%e7%82%b935#myindex1";
$Url_info = parse_url($Url);
dump($Url_info);



?>
<?php 



	Array
	(
		[scheme] => http
		[host] => test.com
		[path] => /
		[query] => myp02=%e6%b5%8b%e8%af%95%e7%bd%91%e7%82%b935
		[fragment] => myindex1
	)



?>

服务器变量

<?php 



echo '</br>'.$_SERVER["DOCUMENT_ROOT"];
// 输出:D:/ECOVACS/PHPServer/httpd-2.4.41-o111c-x64-vc15-r2/Apache24/htdocs
echo '</br>'.$_SERVER["SERVER_ADDR"];
// 输出:10.90.26.151
echo '</br>'.$_SERVER["SERVER_PORT"];
// 输出:9005
echo '</br>'.$_SERVER["REQUEST_SCHEME"];
// 输出:http
echo '</br>'.$_SERVER["SERVER_PROTOCOL"];
// 输出:HTTP/1.1
echo '</br>'.$_SERVER['HTTP_HOST'];
// 输出:221.224.159.150:9005
echo '</br>'.$_SERVER["ERVER_NAME"];
// 输出:
echo '</br>'.$_SERVER["REQUEST_URI"];
// 输出:/wdstest/?tablepress_table=%E4%B8%9A%E5%8A%A1%E6%95%B4%E7%90%86-%E5%8D%97%E6%BA%AA%E7%A8%8E%E5%8A%A1%E5%B1%801.xlsx
echo '</br>'.$_SERVER["QUERY_STRING"];
// 输出:tablepress_table=%E4%B8%9A%E5%8A%A1%E6%95%B4%E7%90%86-%E5%8D%97%E6%BA%AA%E7%A8%8E%E5%8A%A1%E5%B1%801.xlsx
echo '</br>'.$_SERVER["SCRIPT_FILENAME"];
// 输出:D:/ECOVACS/PHPServer/httpd-2.4.41-o111c-x64-vc15-r2/Apache24/htdocs/wdstest/index.php
echo '</br>'.$_SERVER["PHP_SELF"];
// 输出:/wdstest/index.php
echo '</br>'.parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
// 输出:/wdstest/



?>

URL

<?php 




/**
 * 
 * 获取当前点击的链接完整URL(支持http和https)
 * 
**/

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$actual_link = 'http' . (($_SERVER['HTTPS'] == 'on') ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $actual_link;
// 输出:http://221.224.159.150:9005/wdstest/?tablepress_table=%E4%B8%9A%E5%8A%A1%E6%95%B4%E7%90%86-%E5%8D%97%E6%BA%AA%E7%A8%8E%E5%8A%A1%E5%B1%801.xlsx&id=15

echo '</br>'. (isset($_SERVER['HTTPS']) ? "https://" : "http://");
// 输出:http://

echo '</br>'. $_SERVER['HTTP_HOST'];
// 输出:221.224.159.150:9005

echo '</br>'. $_SERVER['SERVER_PORT'];
// 输出:9005

echo '</br>'. $_SERVER['REQUEST_URI'];
// 输出:/wdstest/?tablepress_table=%E4%B8%9A%E5%8A%A1%E6%95%B4%E7%90%86-%E5%8D%97%E6%BA%AA%E7%A8%8E%E5%8A%A1%E5%B1%801.xlsx&id=15

echo '</br>'. $_SERVER['QUERY_STRING'];
// 输出:tablepress_table=%E4%B8%9A%E5%8A%A1%E6%95%B4%E7%90%86-%E5%8D%97%E6%BA%AA%E7%A8%8E%E5%8A%A1%E5%B1%801.xlsx&id=15

echo '</br>'.get_permalink(get_the_ID());
// 返回示例:http://221.224.159.150:9005/wdstest/?tablepress_table=%e4%b8%9a%e5%8a%a1%e6%95%b4%e7%90%86-%e5%8d%97%e6%ba%aa%e7%a8%8e%e5%8a%a1%e5%b1%801-xlsx


global $wp;
echo home_url($wp->request);
// 返回示例:http://221.224.159.150:9005/wdstest

global $wp;  
echo '</br>'. home_url(add_query_arg(array($_GET), $wp->request));
// 返回示例:http://221.224.159.150:9005/wdstest/?tablepress_table=业务整理1.xlsx&id=15






?>
<?php 




/**
 * 
 * 返回当前访问的页面URL。
 * 调用示例:$absolute_url = full_url( $_SERVER );echo full_url( $_SERVER );
 * 返回示例:http://221.224.159.150:9005/wdstest/?tablepress_table=%E4%B8%9A%E5%8A%A1%E6%95%B4%E7%90%86-%E5%8D%97%E6%BA%AA%E7%A8%8E%E5%8A%A1%E5%B1%801.xlsx&id=15
 * 
**/
function url_origin( $s, $use_forwarded_host = false )
{
    $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
    $sp       = strtolower( $s['SERVER_PROTOCOL'] );
    $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
    $port     = $s['SERVER_PORT'];
    $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
    $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
    $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
    return $protocol . '://' . $host;
}

function full_url( $s, $use_forwarded_host = false )
{
    return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}




?>
<?php 







/**
 * 
 * 返回当前访问的页面URL。
 * 调用示例:echo current_location();
 * 返回示例:http://221.224.159.150:9005/wdstest/?tablepress_table=%E4%B8%9A%E5%8A%A1%E6%95%B4%E7%90%86-%E5%8D%97%E6%BA%AA%E7%A8%8E%E5%8A%A1%E5%B1%801.xlsx&id=15
 * 
**/
function current_location()
{
    if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $protocol = 'https://';
    } else {
        $protocol = 'http://';
    }
    return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}







?>
<?php 





/**
 * 
 * 返回当前访问的页面URL。
 * 调用示例:echo get_wp_current_url();
 * 返回示例:http://221.224.159.150:9005/wdstest/?tablepress_table=业务整理1.xlsx&id=15
 * 
**/
function get_wp_current_url(){
    global $wp;
    if('' === get_option('permalink_structure')) return home_url(add_query_arg(array($_GET), $wp->request));
        else return home_url(trailingslashit(add_query_arg(array($_GET), $wp->request)));
}







?>
Scroll to Top