我在Powerpoint演示文稿中有一些链接,但是点击这些链接时,它们会向URL中添加一个返回参数。然而,该返回参数导致我的Joomla站点的MVC模式出现问题。
有没有一种有效的使用PHP去除此返回参数的方法?
例如:
http://mydomain.example/index.php?id=115&Itemid=283&return=aHR0cDovL2NvbW11bml0
我在Powerpoint演示文稿中有一些链接,但是点击这些链接时,它们会向URL中添加一个返回参数。然而,该返回参数导致我的Joomla站点的MVC模式出现问题。
有没有一种有效的使用PHP去除此返回参数的方法?
例如:
http://mydomain.example/index.php?id=115&Itemid=283&return=aHR0cDovL2NvbW11bml0
最安全的“正确”方法如下:
parse_url()
将url解析为数组parse_str()
将其解构为数组unset()
从数组中删除要删除的查询参数http_build_query()
重新构建原始url快速且简单的方法是使用字符串搜索/替换和/或正则表达式来删除值。
$parsedurl['query']
是一个单字符串,包含所有参数。 - ajax333221在另一个帖子中,Justin建议最快的方法是使用strtok()
$url = strtok($url, '?');
点击此链接查看他的完整答案以及速度测试结果:https://dev59.com/zXM_5IYBdhLWcg3wvFzJ#1251650
return
而不是删除所有GET变量。 - mgutt这是为了补充Marc B的答案,附带一个示例。虽然看起来有些冗长,但这是一种安全的方法来移除参数。在这个示例中,我们移除了page_number
参数。
<?php
$x = 'http://url.example/search/?location=london&page_number=1';
$parsed = parse_url($x);
$query = $parsed['query'];
parse_str($query, $params);
unset($params['page_number']);
$string = http_build_query($params);
var_dump($string);
function removeParam($url, $param) {
$url = preg_replace('/(&|\?)'.preg_quote($param).'=[^&]*$/', '', $url);
$url = preg_replace('/(&|\?)'.preg_quote($param).'=[^&]*&/', '$1', $url);
return $url;
}
parse_str($queryString, $vars);
unset($vars['return']);
$queryString = http_build_query($vars);
parse_str
函数可以将查询字符串解析为参数数组,http_build_query
函数则可以将参数数组转换为查询字符串。
在精简Sergey Telshevsky的回答后,按照Marc B的方案实现。
function strip_param_from_url($url, $param)
{
$base_url = strtok($url, '?'); // Get the base URL
$parsed_url = parse_url($url); // Parse it
// Add missing {
if(array_key_exists('query',$parsed_url)) { // Only execute if there are parameters
$query = $parsed_url['query']; // Get the query string
parse_str($query, $parameters); // Convert Parameters into array
unset($parameters[$param]); // Delete the one you want
$new_query = http_build_query($parameters); // Rebuilt query string
$url =$base_url.'?'.$new_query; // Finally URL is ready
}
return $url;
}
// Usage
echo strip_param_from_url( 'http://url.example/search/?location=london&page_number=1', 'location' )
preg_replace
函数,例如:$new_url = preg_replace('/&?return=[^&]*/', '', $old_url);
function reduce_query($uri = '') {
$kill_params = array('gclid');
$uri_array = parse_url($uri);
if (isset($uri_array['query'])) {
// Do the chopping.
$params = array();
foreach (explode('&', $uri_array['query']) as $param) {
$item = explode('=', $param);
if (!in_array($item[0], $kill_params)) {
$params[$item[0]] = isset($item[1]) ? $item[1] : '';
}
}
// Sort the parameter array to maximize cache hits.
ksort($params);
// Build new URL (no hosts, domains, or fragments involved).
$new_uri = '';
if ($uri_array['path']) {
$new_uri = $uri_array['path'];
}
if (count($params) > 0) {
// Wish there was a more elegant option.
$new_uri .= '?' . urldecode(http_build_query($params));
}
return $new_uri;
}
return $uri;
}
$_SERVER['REQUEST_URI'] = reduce_query($_SERVER['REQUEST_URI']);
call_user_func(function($uri) {
$kill_params = array('gclid');
$uri_array = parse_url($uri);
if (isset($uri_array['query'])) {
// Do the chopping.
$params = array();
foreach (explode('&', $uri_array['query']) as $param) {
$item = explode('=', $param);
if (!in_array($item[0], $kill_params)) {
$params[$item[0]] = isset($item[1]) ? $item[1] : '';
}
}
// Sort the parameter array to maximize cache hits.
ksort($params);
// Build new URL (no hosts, domains, or fragments involved).
$new_uri = '';
if ($uri_array['path']) {
$new_uri = $uri_array['path'];
}
if (count($params) > 0) {
// Wish there was a more elegant option.
$new_uri .= '?' . urldecode(http_build_query($params));
}
// Update server variable.
$_SERVER['REQUEST_URI'] = $new_uri;
}
}, $_SERVER['REQUEST_URI']);
urldecode()
避免通过http_build_query()
函数进行两次编码。
注意:已更新使用ksort()
以允许没有值的参数不会出现错误。$link = 'http://mydomain.example/index.php?id=115&Itemid=283&return=aHR0cDovL2NvbW11bml0';
$linkParts = explode('&return=', $link);
$link = $linkParts[0];
return
在最后一个位置,如果不是这样的话,你将失去return
参数中的所有其他参数。 - Dr. House哇,这里有很多例子。我提供了一个具有错误处理功能的示例。它将重建并返回整个 URL,同时删除要被删除的查询字符串参数。它还提供了一个在运行时构建当前 URL 的额外功能。测试过,可行!
感谢 Mark B 提供的方法。这是对 tpow 最初问题“去除此返回参数”的完整解决方案,可能对初学者很有用,避免 PHP 的陷阱。 :-)
<?php
function currenturl_without_queryparam( $queryparamkey ) {
$current_url = current_url();
$parsed_url = parse_url( $current_url );
if( array_key_exists( 'query', $parsed_url )) {
$query_portion = $parsed_url['query'];
} else {
return $current_url;
}
parse_str( $query_portion, $query_array );
if( array_key_exists( $queryparamkey , $query_array ) ) {
unset( $query_array[$queryparamkey] );
$q = ( count( $query_array ) === 0 ) ? '' : '?';
return $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'] . $q . http_build_query( $query_array );
} else {
return $current_url;
}
}
function current_url() {
$current_url = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
return $current_url;
}
echo currenturl_without_queryparam( 'key' );
?>