cyberfly
cyberfly

Reputation: 5848

How to get referer url without the id?

I use this code to get referer url:

<?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

Upvotes: 1

Views: 3235

Answers (6)

cyberfly
cyberfly

Reputation: 5848

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;
?>

Upvotes: 0

Zahymaka
Zahymaka

Reputation: 6621

I'd say

str_replace('?'.parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), '', $_SERVER['HTTP_REFERER']);

Upvotes: 0

Arnaldof
Arnaldof

Reputation: 174

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.

Upvotes: 3

Randy the Dev
Randy the Dev

Reputation: 26700

echo substr($_SERVER['HTTP_REFERER'],0,strpos($_SERVER['HTTP_REFERER'],"?"));

Upvotes: 0

poundifdef
poundifdef

Reputation: 19353

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()

Upvotes: 3

Andrew Cooper
Andrew Cooper

Reputation: 32576

Just check the string for a '?' character. If it exists take the substring before it, else take the whole string.

Upvotes: 1

Related Questions