1.获取url
//不包括协议的完整链接
public function getPageUrl(){
$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;
}
//获取网站协议
public function getHttpProtocol(){
return ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
}
$pageUrl = getPageUrl();
$httpProtocol = getHttpProtocol();
$currentPageUrl = httpProtocol.'://'.$pageUrl;
echo currentPageUrl;
2.过滤url参数
/*
* 过滤url参数
* @params:
* $url:完整url链接
* $params:要过滤的参数列表
*/
public function filterParamFromUrl( $url, $params = [] ) {
$base_url = strtok($url, '?'); // 获取除参数的url信息
$parsed_url = parse_url($url); // 解析url信息
$query = $parsed_url['query']; // 获取请求参数
parse_str( $query, $parameters ); // 将请求参数的字符串转换成数组赋值到Parameters
foreach ($params as $param){
unset( $parameters[$param] ); // 删除指定参数
}
$new_query = http_build_query($parameters); // 将参数转换成字符串
if(!empty($new_query)){
return $base_url.'?'.$new_query; // 返回新url
}else{
return $base_url; // 返回新url
}
}
echo filterParamFromUrl('https://www.baidu.com/apptest?ticket=c0dd804f-685e-4b9d-b731-128495aa1385&app=123',
['ticket'] );