PHP获取当前页面(网页)完整URL链接地址代码

PHP获取当前页面(网页)完整URL链接地址代码

  • 正文概述
  • 在编写PHP程序的时候,经常要获取当前页面的URL,下面提供一些用于获取当前网页链接的代码函数。

    示例链接:https://www.zhutishou.com/example.php?id=1

    获取完整URL,包含URL参数

    最终获取的链接:https://www.zhutishou.com/example.php?id=1

    # 示例一
    # 输出方法:直接调用函数 echo curPageURL();
    function curPageURL() {
    	$pageURL = 'http';
    	if ($_SERVER["HTTPS"] == "on") {
    		$pageURL .= "s";
    	}
    	$pageURL .= "://";
    	if ($_SERVER["SERVER_PORT"] != "80") {
    		$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    	} else {
    		$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    	}
    	return $pageURL;
    }
    
    # 示例二
    # 输出方法:直接调用函数 echo get_urls();
    function get_urls() {
    	$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    	$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    	$path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
    	$relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
    	return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
    }
    

    获取无参数URL,问号前面的链接

    最终获取的链接:https://www.zhutishou.com/example.php

    # 示例一
    # 输出方法:直接调用函数 echo curPageURL();
    function curPageURL() {
    	$pageURL = 'http';
    	if ($_SERVER["HTTPS"] == "on") {
    		$pageURL .= "s";
    	}
    	$pageURL .= "://";
    	$this_page = $_SERVER["REQUEST_URI"];   
    	// 只取 ? 前面的内容
    	if (strpos($this_page, "?") !== false) {
    		$this_pages = explode("?", $this_page);
    		$this_page = reset($this_pages);
    	} 
    	if ($_SERVER["SERVER_PORT"] != "80") {
    		$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $this_page;
    	} else {
    		$pageURL .= $_SERVER["SERVER_NAME"] . $this_page;
    	}
    	return $pageURL;
    }
    
    # 示例二
    function curPageURL() {
    	$pageURL = 'http';	
    	if ($_SERVER["HTTPS"] == "on") {
    		$pageURL .= "s";
    	}	
    	$pageURL .= "://";
    	
    	if ($_SERVER["SERVER_PORT"] != "80") {
    		$pageURL .= $_SERVER["SERVER_NAME"].":" . $_SERVER["SERVER_PORT"] . $_SERVER['PHP_SELF'];
    	} else {
    		$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER['PHP_SELF'];
    	}
    	return $pageURL;
    }
    

    以下是PHP获取URL常用函数

    示例链接:https://www.zhutishou.com/shou/example.php?id=1

    # 获取域名或主机地址,不含HTTP/HTTPS协议,最终输出:www.zhutishou.com
    echo $_SERVER['HTTP_HOST'];
    # 拼接HTTP/HTTPS协议,输出:https://www.zhutishou.com
    echo 'https://' . $_SERVER['HTTP_HOST'];
    
    # 获取网页地址,输出:/shou/example.php
    echo $_SERVER['PHP_SELF'];
    
    # 获取URL参数,包括参数名,输出:id=1
    echo $_SERVER["QUERY_STRING"];
    
    # 获取用户代理
    echo $_SERVER['HTTP_REFERER'];
    
    # 获取完整的URL,输出:https://www.zhutishou.com/shou/example.php?id=1
    echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
    
    # 包含端口号的完整URL,输出:https://www.zhutishou.com:443/shou/example.php?id=1
    echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]
    
    # 获取路径,输出:https://www.zhutishou.com/shou
    $url = 'https://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
    echo dirname($url);
    纯净子主题,干净整洁,独立后台,集成自助广告系统。
    主题兽 » PHP获取当前页面(网页)完整URL链接地址代码

    发表评论