Store GET variable in url

I have a slightly weird question, I'm sure most of you understand that such questions arise from certain situations that the developer has no control over!

I would like to figure out how to always store a query parameter in a url. If the parameter is not set, I would like to add the param = something parameter to the default URL

I asked a previous question related to this and was able to use htaccess to add the default request - but that only works with the initial request, whereas I need to make sure it's always present in the url.

I am thinking of using a cookie set with PHP and then requesting using .htaccess.

So my question is, is this possible, and if there is a better way to do it?

+3


source to share


4 answers


Without changing all the urls, I can't see that this is possible, and as discussed, this is a very hacky way to do it.

I would suggest that you give them a dedicated button that they use to copy the link. Make them use this button to copy the link instead of the URL. Then you can manage the data properly without hacking the website.

Edit . You can add JQuery to add ALL links with your parameter. Have you thought about it?



$(document).ready(function() {
  $('a[href]').each(function() {
    this.href = this.href + '?something=<?php echo $_SESSION["myparam"]; ?>'
  });
});

      

You can get the parameter from the page's bootstrap / session, but this will require your users to enable JS (which most people do now).

ps Uncharted semi-pseudocode. I can test it correctly if you decide to go this route.

+1


source


$_GET

and a $_POST

superglobal that is not intended to be used in this manner. And I don't know why this is necessary, but anyway you can get the required functionality using Session or Cookies . What you are trying to do is not practical. The industry standard is the use of session and cookies. Try them, I'm sure this will help you for sure. If you have any questions, please let me know.



0


source


If you are using Apache 2, you can add a rewrite rule that adds a GET parameter for every request that does not have "param = ..." in its url. Something like (untested):

RewriteCond %{QUERY_STRING} !(\A|&)param=
RewriteRule (.*) $1?param=defaultvalue [QSA]

      

See modrewrite file for details . You can add another one RewriteCond

in %{REQUEST_URI}

if you want to restrict this to some url.

0


source


I only learned php about 3 months ago, so I hope I won't guide you along the way, here's what I'll try.


Edit: It has been about 5 months since I learned php. My previous version before this edit only "built" get to append (and it probably didn't work).

if (!isset($_GET['param'])) {$_GET['param'] = 'something';} 

$var = '';
while ($other_gets = $_GET)
{
    foreach ($other_gets as $key=>$value)
    {
    if ($key = 'something') {}//do nothing we want the other gets
    else {$var .= '&'.$key.'='.$value;}
    }
}
$get = '?'.$user_param.$other_gets;

      

preg_replace your extensions

$string = 'your webpage before output';
$patterns = array();
$patterns[0] = '/.html/';
$patterns[1] = '/.php/';
$patterns[2] = '/yoursite.com /';//with space
$replacements = array();
$replacements[0] = '.html'.$get;
$replacements[1] = '.php'.$get;
$replacements[2] = 'yoursite.com/index.php'.$get;

      

Then print your line

echo preg_replace($patterns, $replacements, $string);

      

0


source







All Articles