How to get referer url without the id?
I use this code to get referer u开发者_C百科rl:
<?php echo $_SERVER['HTTP_REFERER']; ?>
And the output is:
http://example.com/application/view/viewleave.php?idleave=1
How should i do to get this?
http://example.com/application/view/viewleave.php
You could lop off everything after the ?
character.
$text = "http://example.com/application/view/viewleave.php?idleave=1";
$output = substr($text,0,strrpos($text,'?'));
echo $output;
edit: Like someone else said, if the string doesn't contain '?', then $output will be empty. So you could check for that first using strrpos()
Use:
$url = parse_url('http://example.com/application/view/viewleave.php?idleave=1');
and you'll get something like this:
Array
(
[scheme] => http
[host] => example.com
[path] => /application/view/viewleave.php
[query] => idleave=1
)
and merge.
Just check the string for a '?' character. If it exists take the substring before it, else take the whole string.
echo substr($_SERVER['HTTP_REFERER'],0,strpos($_SERVER['HTTP_REFERER'],"?"));
I'd say
str_replace('?'.parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), '', $_SERVER['HTTP_REFERER']);
Thanks all for the help. I got it solve after reading your reply.
<?php
$referer = $_SERVER['HTTP_REFERER'];
$pos = strpos($referer, '?');
if ($pos !== false) {
$output = substr($referer,0,strrpos($referer,'?'));
}
else
{
$output = $referer;
}
echo $output;
?>
精彩评论