Tyler Jones
Tyler Jones

Reputation: 123

Can I modify the url using only PHP, not .htaccess?

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?

Upvotes: 1

Views: 1635

Answers (7)

moebius_eye
moebius_eye

Reputation: 584

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.

Upvotes: 0

ben
ben

Reputation: 1936

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']);
}

Upvotes: 0

rmorse
rmorse

Reputation: 736

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

Upvotes: 0

vomitcuddle
vomitcuddle

Reputation: 315

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

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270637

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']);
}

Upvotes: 2

GolezTrol
GolezTrol

Reputation: 116110

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.

Upvotes: 0

Pavel Janicek
Pavel Janicek

Reputation: 14738

you can do it via PHP function Header

Upvotes: 0

Related Questions