[英]How can I remove invalid querystring using php header location
I have this invalid link hard coded in software which I cannot modify. 我使用无法修改的软件对这个无效链接进行了硬编码。
http://www.16start.com/results.php?cof=GALT:#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search
http://www.16start.com/results.php?cof=GALT:#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search
I would like to use php header location to redirect it to a valid URL which does not contain the querystring. 我想使用php标头位置将其重定向到不包含查询字符串的有效URL。 I'd like to pass just the parameter q=.
我只想传递参数q =。
I've tried 我试过了
$q = $_GET['q'];
header ("Location: http://www.newURL.com/results.php?" . $q . "");
But it's just passing the invalid querystring to the new location in addition to modifying it in a strange way 但这只是将无效的查询字符串传递到新位置,除了以一种奇怪的方式进行修改之外
This is the destination location I get, which is also invalid 这是我到达的目的地,也无效
http://www.newURL.com/results.php?#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search
http://www.newURL.com/results.php?#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search
That's because #
is seen as the start of a fragment identifier and confuses the parser. 这是因为
#
被看作是片段标识符的开始,并混淆了解析器。
You can take the easy-way as Stretch suggested but you should be aware that q
is the last query parameter in your URL. 您可以按照Stretch所建议的那样轻松地进行操作,但应注意
q
是URL中的最后一个查询参数。 Therefore, it might be better to fix the URL and extract the query parameters in a safer way: 因此,以更安全的方式修复URL并提取查询参数可能会更好:
<?php
$url = "http://www.16start.com/results.php?cof=GALT:#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1&q=search";
// Replace # with its HTML entity:
$url = str_replace('#', "%23", $url);
// Extract the query part from the URL
$query = parse_url($url, PHP_URL_QUERY);
// From here on you could prepend the new url
$newUrl = "http://www.newURL.com/results.php?" . $query;
var_dump($newUrl);
// Or you can even go further and convert the query part into an array
parse_str($query, $params);
var_dump($params);
?>
Output 输出量
string 'http://www.newURL.com/results.php?cof=GALT:%23FFFFFF;GL:1;DIV:%23FFFFFF;FORID:1&q=search' (length=88)
array
'cof' => string 'GALT:#FFFFFF;GL:1;DIV:#FFFFFF;FORID:1' (length=37)
'q' => string 'search' (length=6)
Update 更新资料
After your comments, it seems that the URL is not available as a string
in your script and you want to get it from the browser. 发表评论后,URL似乎无法在脚本中作为
string
使用,您想从浏览器中获取它。
The bad news is that PHP will not receive the fragment part (everything after the #), because it is not sent to the server. 坏消息是PHP将不会收到片段部分(#之后的所有内容),因为它不会发送到服务器。 You can verify this if you check the network tab in the Development tools of your browser F12 .
如果您在浏览器F12的“开发”工具中选中“网络”选项卡,则可以验证这一点。
In this case, you'll have to host a page at http://www.16start.com/results.php
that contains some client-side JavaScript for parsing the fragment and redirecting the user. 在这种情况下,您必须在
http://www.16start.com/results.php
上托管一个页面,其中包含一些客户端JavaScript,用于解析片段和重定向用户。
one way could be to use strstr()
to get everything after (and including q=
) in the string. 一种方法是使用
strstr()
获取字符串中的所有内容(包括q=
)。
So: 所以:
$q=strstr($_GET['q'],'q=');
Give that a whirl 旋转一下
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.