Setting cookie and refreshing.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rob_Scott
Forum Newbie
Posts: 19
Joined: Mon Nov 14, 2011 6:08 am

Setting cookie and refreshing.

Post by Rob_Scott »

Hi,

I am trying to set a language cookie.

I have a row of different flags which all have values and set a language cookie.

However i can only redirect to the homepage, and i just want to refresh the current page.

Here is my php script......

<?php
setcookie("lang", urlencode($_GET["lang"]));
$url = "http://www.bertgroup.co.uk/?q=node/16";
header( "Location: $url" );
?>

And the html i am using to link to the script.......

<a class="no_underline" href="setlang.php?lang=EN"><img class="header_language_flags" src="images/flag_EN.jpg" value="EN"/>

<a class="no_underline" href="setlang.php?lang=FR"><img class="header_language_flags" src="images/flag_FR.jpg" value="FR">

Thanks,

Rob
User avatar
theserve
Forum Newbie
Posts: 24
Joined: Wed Jan 18, 2012 6:35 am
Location: London

Re: Setting cookie and refreshing.

Post by theserve »

You could possibly use the $_SERVER['HTTP_REFERRER'] value to redirect back to the page they came from.

Code: Select all

<?php
setcookie("lang", urlencode($_GET["lang"]));
$url = $_SERVER['HTTP_REFERRER'];
header( "Location: $url" );
?>
Rob_Scott
Forum Newbie
Posts: 19
Joined: Mon Nov 14, 2011 6:08 am

Re: Setting cookie and refreshing.

Post by Rob_Scott »

Thanks for the reply, I have used your piece of code and it just wants to refresh to www.bertgroup.co.uk/setlang.php?lang=EN,

Any ideas how to get around this?

Thanks.

Rob
User avatar
theserve
Forum Newbie
Posts: 24
Joined: Wed Jan 18, 2012 6:35 am
Location: London

Re: Setting cookie and refreshing.

Post by theserve »

Why don't you just include the code at the top of every page using a common file and catch a setlang variable. Then you can just make the link open the page you are on.

Code: Select all

if(!empty($_GET['setlang']))
{
setcookie("lang", urlencode($_GET["lang"]));
}
Post Reply