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
http://localhost/profile.php?id=12
and the user wants to login the url would become
http://localhost/profile.php?id=12&action=login

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.

Recommended Answers

All 6 Replies

Hey.

Perhaps:

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

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) {
                unset($get_params[$_index]);
            }
        }
        $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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.