php获取当前页面完整url地址实例

在php中我们要获取 当前页面完整url地址需要使用到几个常用的php全局变量函数了,主要是以$_SERVER[]这些变量,下面我来给各位看一个获取当前页面完整url地址程序吧。

先来看一些PHP变量:

$_SERVER[ 'SERVER_NAME' ] #当前运行脚本所在服务器主机的名称。

$_SERVER[ 'QUERY_STRING' ] #查询(query)的字符串。   

$_SERVER[ 'HTTP_HOST' ] #当前请求的 Host: 头部的内容。   

$_SERVER[ 'HTTP_REFERER' ] #链接到当前页面的前一页面的 URL 地址。   

$_SERVER[ 'SERVER_PORT' ] #服务器所使用的端口   

$_SERVER[ 'REQUEST_URI' ] #访问此页面所需的 URI。  

有了些面函数我们就可以开始了,先来看一些base方法.

baseUrl的两种方法

方法一代码如下:

  1. // baseUrl 
  2. function baseUrl($uri=''){ 
  3.  $baseUrl = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https://' : 'http://'
  4.  $baseUrl .= isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : getenv('HTTP_HOST'); 
  5.  $baseUrl .= isset($_SERVER['SCRIPT_NAME']) ? dirname($_SERVER['SCRIPT_NAME']) : dirname(getenv('SCRIPT_NAME')); 
  6.  return $baseUrl.'/'.$uri

方法二代码如下:

  1. /** 
  2.     * Suppose, you are browsing in your localhost 
  3.     * http://localhost/myproject/index.php?id=8 
  4.     */ 
  5.    function baseUrl() 
  6.    { 
  7.        // output: /myproject/index.php 
  8.        $currentPath = $_SERVER['PHP_SELF']; 
  9.        // output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index ) 
  10.        $pathInfo = pathinfo($currentPath); 
  11.        // output: localhost 
  12.        $hostName = $_SERVER['HTTP_HOST']; 
  13.        // output: http:// 
  14.        $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://' ? 'https://' : 'http://'
  15.        // return: http://localhost/myproject/ 
  16.        return $protocol.$hostName.$pathInfo['dirname']."/"
  17.    } 

方法三代码如下:

  1. <?php 
  2.  /** 
  3.   *@author mckee 
  4.   *@blog  http://www.phpfensi.com 
  5.   */ 
  6.  function get_page_url(){ 
  7.   $url = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') ? 'https://' : 'http://'
  8.   $url .= $_SERVER['HTTP_HOST']; 
  9.   $url .= isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : urlencode($_SERVER['PHP_SELF']) . '?' . urlencode($_SERVER['QUERY_STRING']); 
  10.   return $url
  11.  } 
  12.  echo get_page_url(); 
  13. ?> 

下面说明一下获取当前页面完整路径的方法,代码如下:

  1. <?php 
  2. function getFullUrl(){ 
  3.     # 解决通用问题 
  4.     $requestUri = ''
  5.     if (isset($_SERVER['REQUEST_URI'])) { #$_SERVER["REQUEST_URI"] 只有 apache 才支持, 
  6.         $requestUri = $_SERVER['REQUEST_URI']; 
  7.     } else { 
  8.         if (isset($_SERVER['argv'])) { 
  9.             $requestUri = $_SERVER['PHP_SELF'] .'?'$_SERVER['argv'][0]; 
  10.         } else if(isset($_SERVER['QUERY_STRING'])) { 
  11.             $requestUri = $_SERVER['PHP_SELF'] .'?'$_SERVER['QUERY_STRING']; 
  12.         } 
  13.     } 
  14. //    echo $requestUri.'<br />'; 
  15.     $scheme = emptyempty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""
  16.     $protocol = strstr(strtolower($_SERVER["SERVER_PROTOCOL"]), "/",true) . $scheme
  17.     $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
  18.     # 获取的完整url 
  19.     $_fullUrl = $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $requestUri
  20.     return $_fullUrl

echo getFullUrl();注:由于php没有内置的函数,我们需要对url上的参数进行组合,从而实现整个url.

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php获取当前页面完整url地址实例

79 评论

  1. aldactone 100mg drug diflucan 200mg us order fluconazole 200mg pills

  2. fildena 100mg price nolvadex order robaxin online order

  3. cialis 10mg uk Buy branded viagra buy sildenafil 50mg without prescription

  4. order imuran 100 mcg generic protonix 20mg drug cheap generic sildenafil

  5. oral fluvoxamine cymbalta sale buy glucotrol generic

  6. order linezolid 600mg generic online casino usa online slot machines real money

  7. professional letter writing services buying a term paper sildenafil citrate 50mg

  8. tadalafil 20mg tablet buy tadalafil cheap generic sildenafil

  9. buy naprosyn 500mg online cheap lansoprazole uk oral lansoprazole

  10. Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across. Onwin , Onwin Giriş , Onwin Güncel Giriş , Onwin Yeni Adres , onwin

  11. Hackdra has revolutionized the web3 with its Free cyber security trainings, ethical hacking courses and artificial intelligence courses.

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡