1

My ultimate goal is to strip the query string from the url using php. So I don't want to remove it before PHP has a change to use the query string parameters.

I want to take: "http://www.mysite.com?id=3993993"

and do something with $_GET['id'] using PHP,

and then remove "?id=3993993" from the url leaving us with "http://www.mysite.com".

Any ideas on how this can be done?

1
  • So you want to redirect from the URL with an ID to one without? Why is the ID there in the first place, are you doing anything with it? Commented Jan 20, 2012 at 15:57

7 Answers 7

2

You can retrieve the value from $_GET and store it in $_SESSION, then redirect:

// Load the id from GET into SESSION
session_start();
$_SESSION['id'] = isset($_GET['id']) ? $_GET['id'] : NULL;

// Reidrect to the new URL
if (isset($_GET['id'])) {
  header("Location: http://www.mysite.com");
  exit();
}

// Reload the value from $_SESSION if it is there (and not also in $_GET)
if (!empty($_SESSION['id']) && !isset($_GET['id'])) {
  $id = $_SESSION['id'];
  // Remove it from SESSION to prevent accidental reloads
  unset($_SESSION['id']);
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 but I hope the OP is not going to use this for site navigation as the back-button will not work anymore.
0

you can do it via PHP function Header

Comments

0

You can redirect to http://www.mysite.com:

header('Location: http://www.mysite.com');

Note, you cannot do this after you have outputted any content, so if you want to generate output based on that id, you got a problem. You could store the id in a session, and read and process it after the redirect, but this won't work if the client has disabled cookies.

Comments

0

Do a redirect and check the referrer. You could also save it to a cookie or a $_SESSION variable just to be safe.

Comments

0

The only way this can be done is if you get the PHP to "remember" the value of "id" because once it redirects to just "mysite" it won't have data anymore.

What I would use is a Cookie or Session data which is data that is persistent even if the page refreshes.

I would do something like:

$_SESSION['tempid'] = $_GET['id']; //for remembering the data
header("Location: index.php"); //redirect

Then you can refer to this variable whenever you like using the session variable:

$dosomethingwith = $_SESSION['tempid'];
unset($_SESSION['tempid']);

You might want to clear the session variable after you have used the tempid

Comments

0

The only way I can think to do that is to capture any GET values, store them in session, then redirect to the same page without the params set.

if(!empty($_GET)) {
    $_SESSION['GET'] = $_GET;
    $url = parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);
    header('Location: '.$url);
    die();
}


if(!empty($_SESSION['GET'])) {
    $_GET = $_SESSION['GET'];
    unset($_SESSION['GET']);
}

Comments

0

I wouldn't advise you to do this, but, one solution might be set a cookie, register a session variable that contains the query parameter, then redirect the user from http://www.mysite.com/?id=3993993 page to http://www.mysite.com/

<?php 
session_start();
$_COOKIE['something'] = md5($whatever);
$_SESSION['query'] = $_SERVER['QUERY_STRING'];

if ($_SERVER['QUERY_STRING'] != '/')
   header("Location: /");

if ( isset ($_SESSION['query']) ){
   /* display your page */
}
?>

I strongly advise you NOT to use this: most search engine bots would do not accept cookies.

I did not test this script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.