ganjan
ganjan

Reputation: 7616

How do I get values from url and append them to existing values in url (php)

I have a link in my page that look like this: a href=?command=value but when I click the link and the page reloads it first load another include php file. That redirect the user based on the cookie. like this: header('Location: ?lang='.$redirect); So when the page loads the ?command=value is gone.

I need to append &command=value in the redirecting include file so the url look like this: ?lang=en_US&command=value

Upvotes: 0

Views: 599

Answers (3)

Blizz
Blizz

Reputation: 8408

I like the http_build_query function the most:

$variables = $_GET;
$variables['lang'] = $redirect;
header('Location: ' . http_build_query($variables));

Like this you keep the existing variables, add your own and use the new query string for the redirect.

Upvotes: 7

Jim
Jim

Reputation: 18863

You would need to do something like:

header('Location: ?lang=' . $redirect . '&' . $_SERVER['QUERY_STRING']);

The above will keep any get data that is associated in tack. codersarepeople will only do the command.

Good luck!

EDIT:

One potential down side to this would be if the lang is already in the QUERY_STRING, it will also be appended, so be cautious of that, depending on the uses.

Upvotes: 0

codersarepeople
codersarepeople

Reputation: 2001

Change the redirect line to this:

header('Location: ?lang='.$redirect.'&command='.$_GET['command']);

Upvotes: 0

Related Questions