Thanks for the tip re urlencode(). I had tried that before I posted, but I was doing it at the wrong side of the process. I was using it in my switch file on the referrer being returned to location: instead of on the link I was passing to my switch file. When I changed my link to...
Your very problem is that you are NOT encoding the URL data, so the browser is trying to do it for you, only it can't be sure whether & is supposed to be data or is supposed to separate your URL arguments.
http://php.net/urlencode
<a href="/switch.php?userLangChoice=fr&sender=<?php echo urlencode($_SERVER['REQUEST_URI']); ?>">French</a>
...the problem was fixed. DOH!
You may also want to just include the switch.php whenever userLangChoice
is set in the URL:
----------- langchoic.inc ------------- <?php if (isset($_GET['userLangChoice'])){ setCookie($_GET['userLangChoice']); } ?>
You can simply: <?php include 'langchoice.inc'?> on every page or in a
globals file you already include, and then you're not wasting a bunch of
HTTP connections bouncing around with the header or worrying about your
URL getting munged.
<?php include 'langchoice.inc'; echo "<a href=\"$_SERVER[PHP_SELF]?userLangChoice=fr\">French</a>"; echo "<a href=\"$_SERVER[PHP_SELF]?userLangChoice=en\">English</a>"; ?>
I tried a few quick tests with this method and variations of it. I could get it to set a cookie like '[en] => ' or '[fr] => ' but I could not get it to set a cookie like '[userLang] => en'. Even if I worked that out though, there is a disadvantage to this approach, in that (AFAIK) the page has to be refreshed before the server will be aware of the new cookie value on the client, not desirable in this situation where I'm offering bilingual content.
My thoughts in using the switch.php file was to set the cookie, make the server aware of it so it can deliver appropriate content, and return to the URL intact at the end of the process, without any new vars in it.
With your reminder about urlencode, this is all playing nicely now.
Thanks, verdon
Verdon Vaillancourt wrote:I am trying to build a simple mechanism to allow visitors to set a site
preference (stored in a cookie) by clicking on a link. I want the
cookie set and the original page reloaded with the new cookie set, when
a visitor clicks on the link.
My link looks like this...
<a href="/switch.php?userLangChoice=fr&sender=<?php echo $_SERVER['REQUEST_URI']; ?>">French</a>
My file switch.php looks like this...
<?php
setcookie("userLang", $userLangChoice);
if ($sender == "") $sender = "index.php"; else $sender = "$sender";
header("location:".$sender);
?>
Now, for the most part this works fine, but in some cases, my referring
URL ($sender) is being truncated. Simple URLs such as
'/listingsearch.php?Category%5B%5D=Hunting' work fine, although it is
being returned as '/listingsearch.php?Category[]=Hunting'. More complex
URLs like
'/listingsearch.php?
Accommodation%5B%5D=Outpost&Category%5B%5D=Fishing&Region%5B%5D=North-
West' are being truncated at the first variable down to
'/listingsearch.php?Accommodation[]=Outpost'
Is there something I can do to make sure the referring URL is not truncated and it would also be nice if it was left alone and not encoded or decoded.
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php