PHP获取当前网址/替换网址query中的值

项目中用到的两个函数,自己写了下,放这做为备份吧。

一是PHP获取当前页面的网址:


//获得当前的脚本网址
function GetCurUrl(){
    if(!empty($_SERVER["REQUEST_URI"])){
      $scriptName = $_SERVER["REQUEST_URI"];
      $nowurl = $scriptName;
   }else{
      $scriptName = $_SERVER["PHP_SELF"];
      if(empty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;
      else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
   }
   return $nowurl;
}

另一个是PHP替换网址中query部分的某变量的值比如 ,我们要设$url中的key=321;

其实有几种情况:

$url=’www.sina.com/a.php?key=330′; (转化为www.sina.com/a.php?key=321);

或$url=’www.sina.com/a.php’; (转化为www.sina.com/a.php?key=321);

或$url=’www.sina.com/a.php?cat=2′;  (转化为www.sina.com/a.php?cat=2&key=321)

等等。虽然情况很多,但PHP处理起来非常简单,如下:


/* 将URL中的某参数设为某值*/
function url_set_value($url,$key,$value)
{
   $a=explode('?',$url);
   $url_f=$a[0];
   $query=$a[1];
   parse_str($query,$arr);
   $arr[$key]=$value;
   return $url_f.'?'.http_build_query($arr);
}


( ! ) Warning: Missing argument 1 for cwppos_show_review(), called in /data/www/enjoyphp/wp-content/themes/flat/content-single.php on line 29 and defined in /data/www/enjoyphp/wp-content/plugins/wp-product-review/includes/legacy.php on line 18
Call Stack
#TimeMemoryFunctionLocation
10.0000354840{main}( ).../index.php:0
20.0001355128require( '/data/www/enjoyphp/wp-blog-header.php' ).../index.php:17
30.170510163712require_once( '/data/www/enjoyphp/wp-includes/template-loader.php' ).../wp-blog-header.php:19
40.171810234856include( '/data/www/enjoyphp/wp-content/themes/flat/single.php' ).../template-loader.php:74
50.254310800704get_template_part( ).../single.php:5
60.254310801224locate_template( ).../general-template.php:167
70.254310801320load_template( ).../template.php:643
80.254410801704require( '/data/www/enjoyphp/wp-content/themes/flat/content-single.php' ).../template.php:686
90.259710786816cwppos_show_review( ).../content-single.php:29

发表评论

电子邮件地址不会被公开。 必填项已用*标注