Skip to content Skip to sidebar Skip to footer

Remembering Users Selection From One Page To The Next

on my website, the user has several options such as language, date and region. when the user clicks anyone of those all is fine, the website adapts to the specific choices, but whe

Solution 1:

The session can be used at this situation. You can assign these values in the session variable and can access on all the pages of application.

$_SESSION['somevar']='someval';

Solution 2:

You should take a serious look at using gettext for manageable localization support. Instead of doing conditionals for every piece of your page, you'll simply call a function like <?php _('Welcome!') ?> and it will be automatically translated according to your language setting.

  1. Download the library
  2. Follow these instructions

Solution 3:

Sessions or Cookies are definitely the best way to do it: The thing is that you have to have a way to connect it with the user. Technically you could store it in a database and either store a cookie on the user's computer that says which database entry to read or even store the user's ip and relate it to the user that way (although that's a pretty unreliable method). But that's all probably an overkill for what you want to do.

Here is how to incorporate sessions into your code:

Posting only one language

For a start lets reformat what you already have. You have $_POST['languages'] which is actually an array that contains one result. In that case you may as well make it a string and call it $_POST['language']

You also must remember to post it correctly (in the singular) when the user originally chooses his language.

First off you have to start the session at the top of the page:

session_start();

Next you have to save the user's selection to a session variable.

We are going to check if the user has selected a language (from the $_POST variable) and, if yes, assign it to a $_SESSION variable.

if ($_POST['language']) {
    $_SESSION['language'] = $_POST['language'];
}

The session variable functions identical to any other variable, but it will carry on from page to page until the session ends.

Now we are going to check what the language is

This is pretty simple. In your example you checked what the language was by finding what the user had 'posted':

isset($_POST['languages'])

In our new code, we are going to check the session variable, which we have just saved:

$_SESSION['language']

Choosing the right language

No need to make $_POST['languages'] an array and then use a foreach to loop through it.

A simpler way of doing it would be like this:

switch($_SESSION['language']) {
    case"german": //do german suffcase"english": //do english suff
    etc...
}

Additionally, you first checked if the session tag was empty and then put a value if it wasn't. You can skip that step and instead put inside the switch() { } at the end, after all the cases:

default: //do defaultlanguage stuff

Solution 4:

As a session:

session_start(); //put this at the top of your script on all pages that need to rememberif(isset($_POST['languages']))
{
    $_SESSION['languages'] = $_POST['languages'];
}

Then change:

foreach ($_POST['languages'] as$language) {

to

foreach ($_SESSION['languages'] as $language) {

As a cookie:

if(isset($_POST['languages']))
{
    setcookie('language', $_POST['languages'], 7776000) //sets cookie for 90 days, or 7776000 seconds
}

and change foreach ($_POST['languages'] as $language) {

to

foreach ($_COOKIE['languages'] as$language) {

I believe this answer is complete... do you intend to support multiple languages?

Solution 5:

<?php
 session_start();
if (isset($_GET['lang'])) {
  $_SESSION['lang'] = $_GET['lang'];
}
if (isset($_SESSION['lang'])) {
  define(LANGUAGE, $_SESSION['lang']);
}
else {
  define(LANGUAGE, 'en_GB');
}
include'languages/'.LANGUAGE.'.php';
?>

Post a Comment for "Remembering Users Selection From One Page To The Next"