简体   繁体   中英

hide parameter (query string) in url (htaccess) but send this parameter to a page (php)

How to hide parameters (query_string) in url but send them to page hidden, not visible in address bar
example:
I have this:

http://localhost/index.php?sent=true

i want:

http://localhost/index.php

but i want to get sent parameter in PHP $_GET['sent']

i tried to redirect to page with no parameters(no query_string) [R] and after that give page some parameters (silently) without [R] redirection (without changing adress itself)

i tried to use RewriteRule in htaccess with different flags and different ways but nothing worked

i learned that if i want to change address in browser, i must use R flag with L flag in one RewriteRule line - other way it does not work - adress does not change

but when i use [L] flag i cannot go to next line(next RewriteRule) and append any query_string to address without [R] redirection

if i don't put an [L] flag in first line (with [R]) i don't have new address but if i do - i can't use [C] flag or even [N] flag


Here is why:

on my page i use header('Location: ...?sent=true') function after successful data sent from Form i redirect to ?sent=true because of i don't want to user refresh page and send the same data again

but after redirect to ?sent=true i don't want to have that query_string in address bar there are more forms on that page that will send other POST data and refresh page with this ugly query_string there

how to do that?

You could just set it so that if you visit a script with ?sent=true once it has finished, it redirects the user

eg

if(!empty($_GET['sent'])&&$_GET['sent']) {
    //do stuff
    header("Location: http://localhost/index.php");
}

You can't do this with htaccess as you need to pass to PHP, it's passed to PHP after redirection so if you redirect with htaccess the value will never get to PHP.


Further question, the only way to do this then is to redirect the user with a javascript form

<?php if(!empty($_GET['sent'])&&$_GET['sent']) { ?>
<form action='http://localhost/index.php' method='post' name='frm'>
    <?php
    foreach ($_GET as $a => $b) {
        echo "<input type='hidden' name='".$a."' value='".$b."'>";
    }
    ?>
    </form>
    <script language="JavaScript">
        document.frm.submit();
    </script>
<?php } ?>

Cookies may be a solution for this as you can use them to pass data from pages to pages and there are not visible, at least not easily for common users.

Or if possible for you, $_SESSION is also a good solution to send data from one page to the other.

You could assign the variable in apache:

SetEnvIf Request_URI "\sent=true$" SENT=1

then retrieve it in PHP like:

if (isset($_SERVER['SENT'])) {
   echo "Received sent variable from apache, value is " . $_SERVER['SENT'];
} else {
   echo "not received sent variable from apache";
}

UPDATE :
You need to set AllowOverride FileInfo or AllowOverride ALL in your httpd.conf file or .htaccess file, like this example:

<Directory /home>
 AllowOverride FileInfo
</Directory>

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