0

I'm starting with PHP, so I have no idea what's going wrong. I'm trying to update some value in my $_SESSION var using a function, but is not working. First of all, I started the session and set a default value to the session if it is "undefined" in my header.php:

  <?php
    session_start();
    if (!isset($_SESSION["locale"])) {
      $_SESSION["locale"] = 'pt_BR';
    }
  ?>

Then I have some ajax which calls my PHP function.

  $('#change_locale_br').click(function() {
    $.ajax({
      type: "POST",
      url: "i18n/i18n_functions.php",
      data: { action: "changelocale", new_locale: "pt_BR" },
      success: function() {
      }
    });
  });

After in the php file, I have the following code:

  <?php
    function changelocale($l) {
      $_SESSION["locale"] = $l;
    }

    if(isset($_POST['action']) && $_POST['action'] == 'changelocale'){
        $l = $_POST['new_locale'];
        changelocale($l);
    }
  ?>

So the changelocale function is being called with the different values (pt_BR, es and etc), but after called it doesn't really change the $_SESSION["locale"] value.

I made a lot of research and NONE of the suggestions made here or in the PHP documentation page worked. Does someone knows what could be done in this case?

Thanks in advance.

  • You've edited your post. Did you try `session_start()`? Are you getting any error messages after [turning error reporting on](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)? – Blue Jun 17 '17 at 23:23
  • Yes, I did in the header.php (which is included in every any other pages). And I don't get any error messages (even after turning the error reporting on). – Ana Carolina Manzan Jun 17 '17 at 23:26
  • What happens when you `print_r($_SESSION)`? Try printing it before/after setting it, and make sure you see the updated value in your post response (From javascript). – Blue Jun 17 '17 at 23:28
  • It shows the correct values, like this: `Array ( [locale] => es )` I've added an php echo in the page to show the current value for this session locale, but when I refresh the page, it shows the default value (even if the response shows a different value) – Ana Carolina Manzan Jun 17 '17 at 23:31
  • Check out the [`session_id()`](https://secure.php.net/manual/en/function.session-id.php) function. Make sure your session cookie is correctly being saved, and that you have the same session ID between pages. [This post](https://stackoverflow.com/questions/35960067/how-to-debug-session-login-bugs-in-php) may offer some assistance with session bugs. – Blue Jun 17 '17 at 23:33
  • Well, I checked the session_id and it matches for every page. I read somewhere that when we handle sessions across directories in the application we might face some trouble if don't set the session.cookie_domain correctly. I've tried this (for my localhost using xampp and windows), restarted the server, cleared the session data but still didn't work. Do you know if there is any other thing I have to set? – Ana Carolina Manzan Jun 19 '17 at 02:55

2 Answers2

3

You need to include session_start() at the top of your page, which you intend to set or access $_SESSION information.

Blue
  • 22,608
  • 7
  • 62
  • 92
0

Here is the solution: I called session_write_close(); (which is used to assure session data will be "unlocked", since php doesn't allow concurrent writes) right after setting the default values for the session in my header.php:

  <?php
    ini_set('session.cookie_domain', 'localhost');
    if(!isset($_SESSION)) session_start();

    if (!isset($_SESSION["locale"])) {
      $_SESSION["locale"] = 'pt_BR';
    }
    session_write_close();
  ?>

Then in my changelocale.php I called the session_start() and at the end called the session_write_close(). It's like this now:

  <?php
    if(!isset($_SESSION)) session_start();

    function changelocale($new_locale) {
      $_SESSION["locale"] = $new_locale;
    }

    if(isset($_POST['action']) && $_POST['action'] == 'changelocale'){
        $l = $_POST['new_locale'];
        changelocale($l);
    }
    session_write_close();
  ?>