Hi everyone,
i was wondering if you could help me out, i'm working on a project where the user can login from any part of the site and a function is called when the button is clicked.
The problem is if the user for example is on this url
and the user wants to login the url would become

That works fine but when i did $_SERVER i get the full url, how can i remove the &action=login after the user has logged in!

Thanks for looking.

7 Years
Discussion Span
Last Post by emhmk1



$referrer = str_replace('&action=login', '', $_SERVER['HTTP_REFERER']);

Edited by Atli: n/a


Thanks Atli that worked an absolute peach.

Now, i have one last query for today.

As stated before i'm using (in the url) action=login
now when on a profile page (/profile.php?id=12) i have the login button link as &action=login however there are also pages eg contact.php that dont require the '&' they need the '?action=login'
What would be the best solution for this?

Many thanks again


In that case you could just change it to:

$referrer = str_replace('?action=login', '', $_SERVER['HTTP_REFERER']);

Doing this would also work in both cases:

$referrer = str_replace('action=login', '', $_SERVER['HTTP_REFERER']);

However, this leaves both URL strings with an extra ? or & char.

You could also just do this properly and use the parse_url function. Break the URL down into it's basic parts and re-build it from scratch.
Like, for example, if you put this function somewhere at the top of you page:

function clear_param($url, $param) {
    // Divide the url into its components.
    $parts = parse_url($url);

    // Remove the 'action=login' from the query string
    if($parts['query'] != '') {
        $get_params = explode('&', $parts['query']);
        foreach($get_params as $_index => $_param) {
            if($_param == $param) {
        $parts['query'] = '?' . implode('&', $get_params);

    // Rebuild and return the url.
    return "{$parts['scheme']}://{$parts['host']}{$parts['path']}{$parts['query']}";

Then you could just do:

$referer = clear_param($_SERVER['HTTP_REFERER'], 'action=login');

It might need some minor fine-tuning to fit your server, but it should work fine.


Many thanks again, it worked right away like a charm.

I'm sorry to say i have one last question.

When calling the login i'm currently using &action=login for when the user is in the profile.php?id=123 etc etc but can i determine within the anchor if i need to call &action=login or ?action=login

Sorry to be a pain!


When calling the login i'm currently using &action=login for when the user is in the profile.php?id=123 etc etc but can i determine within the anchor if i need to call &action=login or ?action=login

I'm not entirely sure what you mean, but maybe something like this would help?

// Create "key=value" strings from all the GET
// parameters and put them into an array.
$params = array();
foreach($_GET as $_key => $_value) {
    // The "urlencode" function makes sure the
    // values are safe to put into the URL.
    $_key = urlencode($_key);
    $_value = urlencode($_value);
    $params[] = "$_key=$_value";

// Add the "action=login" parameter to the array.
$params[] = 'action=login';

// Build a ULR-like query string from the array.
// (Note that '&' is the HTML entity for '&', which you
//  should always use when printint a '&' into HTML)
$param_string = '?' . implode("&", $params);

// Generate the URL
echo "<a href='{$_SERVER['PHP_SELF']}{$param_string}'>Linkage</a>";

I can't thank you enough Atli, You just saved me a major headache

Thank you

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.