简体   繁体   中英

How to Hide URL parameters with php redirect

I read this article: http://smallbusiness.chron.com/hiding-url-parameters-php-redirect-33163.html which explains how to but I'm don't understand how you redirect with the header as they say in there.

For storing in sessions this is code I use

session_start();  
function input_val($key, $remember = true) {  //use input_val('nameofinputfield')as value to be able to store in session
    $value='';
    if(isset($_REQUEST[$key])) {
       $value = $_REQUEST[$key];
       //Store value in session if remember = true 
       if($remember) {
         $_SESSION[$key] = $value;
       }
       return $value;
    } else {
      //Return session data
      return isset($_SESSION[$key]) ? $_SESSION[$key] : $value;
    }
}

I will try to explain it as simple as I can, it's not that difficult. When a user is in a session with the php he stores a little blob of text in the browser, this text is like a user ID that lasts until he close his browser. The php script can tell and extract information from the server, like parameters, by knowing his ID. This is not a cross-server feature though and it isn't persistent, unlike cookies, the expiration on the sessions are normally short and they expire when the user closes his browser.

Also it is recommended not to use it to store Get information like Page number, because it can't be re-referenced.

Let's say, you want to pass a username and email parameter from your script1.php to script2.php. If you are using POST method, in the URL the parameters won't show, and you can access your passed variables through $_POST global variable. But, if you want to use GET method for any reason, or you want to store data in $_SESSION you can do this.

You can try to use this in your script2.php:

session_start();
if (count($_GET)) {
    foreach ($_GET as $key => $value) {
        $_SESSION[$key] = $value;
    }
    header("Location: " . $_SERVER["PHP_SELF"]);
}
//At here, you can access all of your parameters from $_SESSOION variable
var_dump($_SESSION);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM