How to create a simple PHP cookie language?

I am trying to configure, as I thought, a simple language switch. I thought I would use PHP cookies, but they do not behave as intended.

I read several cookie guides and looked at a few similar examples here on StackOverflow, but there must be something missing for me because it cannot make it work correctly.

I set the language by passing it as a URL variable (lang = en or lang = ru). Everything seems to be all right. However, the code that I have at that moment that sets the cookie seems to be one step behind, so it doesn't matter initially (I would like it to be "en" by default), then if the user presses "ENG", the button still does not matter, and then, if the user clicks on Russian, the value is displayed as "en", and then, if I press the "ENG" button again, the value is displayed as "ru".

Here is the code I combined:

if( $_GET['lang'] ) { $lang = (string)$_GET['lang']; setcookie( 'lang', $lang, time() + 60*60*24*30 ); } elseif( !isset($_COOKIE['lang']) ) { $lang = 'en'; } else { $lang = $_COOKIE['lang']; } 

Once I get this working, I intend to use a cookie value to display an English or Russian menu using some conditional PHP.

Thanks.

+6
source share
5 answers

Thanks for all the suggestions - @Mob set me up in the right direction i.e. processed the cookie on another page, and then sent back to the first.

I thought a little more and experimented, and I finally decided this. I will post the code below if anyone else wants to use this.

On the main page, enter the following:

 <form action="language_switcher.php" method="post"> <select name="lang"> <option value="en"<?php if( $_COOKIE["language"] == "en" ) { echo " selected"; } ?>>English</option> <option value="ru"<?php if( $_COOKIE["language"] == "ru" ) { echo " selected"; } ?>>Russian</option> </select> <input type="submit" value="Select Language"> </form> <p>Language: <?php if( isset( $_COOKIE["language"] ) ) { echo $_COOKIE["language"]; } else { echo "<em>not set</em>"; } ?></p> 

Then, in another file named 'language_switcher.php', enter the following code:

 $lang = "en"; if( isset( $_POST["lang"] ) ) { $lang = $_POST["lang"]; setcookie ( 'language', $lang, time() + 60*60*24*30, '/', 'mydomain.com'); header( "Location: /previous_page_url.php" ); } 

The user selects a language and clicks "Select Language". The form then submits the form value to 'language_switcher.php', which sets the cookie, and then sends the user to the previous page.

Done! :)

+6
source
 if ( !empty($_GET['language']) ) { $_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'nl'; } else if ( empty($_COOKIE['language']) ) { $_COOKIE['language'] = 'nl'; } setcookie('language', $_COOKIE['language']); 
+3
source

The cookie is not available until the settings page is reloaded or another page is accessible (in other words, you cannot set and access the cookie on the same page).

Check this code:

 if( $_GET['lang'] ) { $lang = (string)$_GET['lang']; setcookie( 'lang', $lang, time() + 60*60*24*30,'/' ); } elseif( !$_GET['lang']) ) { $lang = 'en'; } else { $lang = $_GET['lang']; } header("Location: redirect_file.php") 

Then in redirect_file.php you are redirected back to the cookie page. Perform some checks if you want to avoid loop forwarding.

+2
source

Try it,

 if ( !isset( $_GET['lang'] ) ) { if ( isset( $_COOKIE['lang'] ) ) { $lang = $_COOKIE['lang']; } else { $lang = 'en'; } } else { $lang = (string)$_GET['lang']; setcookie( 'lang', $lang, time() + 60*60*24*30 ); } 

If the lang directive is not set to GET, check if there is a cookie.

If it uses its value or defaults to 'en'. If the lang directive is set, set the cookie.

This is almost the same code, but slightly optimized. (It’s best to set conditions that seem the most at the top of ifs.

0
source

I used PHP $ _SERVER ['PHP_SELF'] to refresh the current page and take into account the selected language.

An example of the attached code. file name: language_switcher.php

 <?php error_reporting(E_ERROR); $lang = "en"; if( isset( $_COOKIE["language"] ) ) { $lang = $_COOKIE["language"]; } if( isset( $_POST["lang"] ) ) { $lang = $_POST["lang"]; setcookie ( 'language', $lang, time() + 60*60*24*30, '/','localhost'); $refresh = $_SERVER['PHP_SELF']; header( "Location: $refresh"); } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Test Page Language Toggle</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <?php // Text definitions $text = array( 'en' => array( 'selectlang' => 'Select Language', 'lingua' => 'Language: ', 'filename' => 'You are in this location: ' ), 'fr' => array( 'selectlang' => 'Séléctionner', 'lingua' => 'Langue: ', 'filename' => 'Vous lisez ce fichier: ' ) ); ?> <form action="language_switcher.php" method="post"> <select name="lang"> <option value="en"<?php if( $_COOKIE["language"] == "en" ) { echo "selected"; } ?>>English</option> <option value="fr"<?php if( $_COOKIE["language"] == "fr" ) { echo " selected"; } ?>>Français</option> </select> <input type="submit" value="<?php echo $text[$lang][selectlang]; ?>"> </form> <p><?php echo $text[$lang][lingua]; if( isset( $_COOKIE["language"] ) ) { echo $_COOKIE["language"]; } else { echo "<em>not set</em>"; } ?></p> <br> <p><?php echo $text[$lang][filename] . $_SERVER['PHP_SELF']; ?></p> </body> </html> 
0
source

Source: https://habr.com/ru/post/899437/


All Articles