Alexander Holsgrove
Alexander Holsgrove

Reputation: 1803

Keep a GET variable in URL

I have a bit of an odd question, I'm sure most of you realise that these sorts of questions arise out of certain situations that a developer has no control over!

I would like to work out how to keep a querystring parameter in the URL at all times. If the parameter is not set, I'd like a default to be appended to the URL ?param=something

I asked a previous question relating to this and have been able to use htaccess to add a default query - but this only works fir the initial request whereas I need to ensure it is always present in the address.

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

So, I am asking if this is possible and if there is a better way of doing this?

Upvotes: 3

Views: 2532

Answers (4)

Shaggin Shea
Shaggin Shea

Reputation: 141

I only learned php about 3 months ago so I hope I am not leading you down a path, this is what I would try.


Edit: It has been about 5 months now since I learned php. My Previous version before this edit only "built" the 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 string

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

Upvotes: 0

user699082
user699082

Reputation:

If you are using Apache 2, then you could add a rewrite rule that appends a GET parameter to each request that doesn't have "param=..." in its URL. Something like (untested):

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

See modrewrite 's doc for details. You can add another RewriteCond on %{REQUEST_URI} if you want to limit this to some URL.

Upvotes: 0

webnoob
webnoob

Reputation: 15934

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

I would suggest you give them a "special" button that they use for copying a link. Make them use this button to copy the link instead of the URL. You can then control the data properly without hacking the website.

Edit: You "could" add some JQuery in to append ALL links with your param. Have you thought about this?

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

You could get the param from the initial page load / session but this would require your users to have JS enabled (Which most people do now).

p.s Untested semi-pseudo code. I can test it properly if you decide to go down this route.

Upvotes: 1

Techie
Techie

Reputation: 45124

$_GET and $_POST superglobal variable those are not meant to used like this. And I don't know why is it required but anyway you can get the required functionality using Session or Cookies. What you are trying to do is not practical. Industry standard is to use session and cookie. Try them out I'm sure it will helps you out for sure. If you have any issues let me know.

Upvotes: 0

Related Questions