close ad
WARNING PC USERS: Do Not Install the DREAMWEAVER CC 2017 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

set cookie to remember language choice

Thread began 11/11/2016 1:29 pm by MagsW | Last modified 3/02/2017 4:24 pm by Ray Borduin | 76 views | 5 replies |

MagsW

set cookie to remember language choice

I have a dual language website (English/Russian) which has a language selection dropdown in the header of every page:

<ul>
<li class="active"><a href="/en/<?php echo basename($_SERVER['PHP_SELF'])?>">ENGLISH</a></li>
<li><a href="/ru/<?php echo basename($_SERVER['PHP_SELF'])?>">РУССКИЙ</a></li>
</ul>

Visitors are currently automatically redirected to domain.com/en/ via the .htaccess file:

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ en [L]

If the visitor then chooses Russian, how would I go about setting a cookie to remember this choice for their next visit? Would I
need to alter the .htaccess file as well?

Sign in to reply to this post

Ray BorduinWebAssist

You could add another url parameter to trigger the cookie setting like:

php:
<ul>

<li class="active"><a href="/en/<?php echo basename($_SERVER['PHP_SELF'])?>?lang=en">ENGLISH</a></li>
<li><a href="/ru/<?php echo basename($_SERVER['PHP_SELF'])?>?lang=ru">РУССКИЙ</a></li>
</ul>


Then add an include to the top of each page that uses the webassist server behavior to set a cookie value when the remember url parameter is present.

In the same include you can add code to redirect to the correct language if the cookie is set. The include file will look something like:

php:
<?php

if ((isset($_GET['lang']) && $_GET['lang'] != "")) {
    
setcookie("langpref""".$_GET['lang']  .""time()+(60*60*24*30), "/"""0);
}
?>
<?php 
if (isset($_COOKIE['langpref'])) {
    if (
strpos($_SERVER['PHP_SELF'],$_COOKIE['langpref']) === false) {
      
header("location: /" $_COOKIE['langpref'] ."/"basename($_SERVER['PHP_SELF']));
    }
}
?>
Sign in to reply to this post

MagsW

Works like a dream, thank you Ray!

Sign in to reply to this post

MagsW

Hi Ray, further to the above, our client now wants to send out an email with a list of links to both the English and Russian pages of the site. However, they've discovered that something weird is happening with the URL parameter, but not on all the pages, which is very strange because all pages use the same shared header.php file which contains the cookie. Could you take a look and let me know what might be causing this? Details in PM.

Sign in to reply to this post

Ray BorduinWebAssist

Try this in the include file:

php:
<?php

if ((isset($_GET['lang']) && $_GET['lang'] != "")) {
    
setcookie("langpref""".$_GET['lang']  .""time()+(60*60*24*30), "/"""0);
    
$_COOKIE['langpref'] = $_GET['lang'];
}
?>
<?php 
if (isset($_COOKIE['langpref'])) {
    if (
strpos($_SERVER['PHP_SELF'],$_COOKIE['langpref']) === false) {
      
header("location: /" $_COOKIE['langpref'] ."/"basename($_SERVER['PHP_SELF']));
    }
}
?>
Sign in to reply to this post

MagsW

That seems to work OK for me, thanks!

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...