0

First of all a little bit of background on my setup:

  • I have a local domain name set up, projects.lumesse.com.
  • PHP version is 5.4.16 running on IIS 7.
  • Testing in Chrome (latest stable build).

The problem is as follows:

I have a function called 'getVariable' as follows:

function getVariable($name, $default = "") {
    if(isset($_POST[$name])) return $_POST[$name];
    if(isset($_GET[$name])) return $_GET[$name];
    if(isset($_SESSION[$name])) return $_SESSION[$name];
    return $default;
}

Going through this function line by line it returns the post variable if it exists, followed by the get variable, followed by the session variable followed by the default (if none of the others exist).

In another include, which is included directly after the functions include, I have the following line:

$_SESSION["Language"] = getVariable("Language", "FR");

This works fine if I put ?Language=DE - the site displays in German as expected. However from the line above I'd expect that the language is persisted if I strip off the querystring.

The first time this page is hit, with ?Language=DE, it should return the get variable. The line above then sets the session variable. Any calls to this function after this would therefore return the session variable, right?

It is actually returning FR, the default, in the case that no language parameter exists, even if I've set the language beforehand.

Any ideas on what I'm missing would be much appreciated.

ClarkeyBoy
  • 4,934
  • 12
  • 49
  • 64
  • This abstraction somehow frustrates me. Can't you do it the simple way `if(!isset($_SESSION['Language'])) { $_SESSION['Language'] = $_GET['Language']; } else { $_SESSION['Language'] = 'FR'; }` – Royal Bg May 04 '14 at 12:19
  • Just to be sure : are you sure you did not forget to initiate the session with `session_start()` beforehand? – pixeline May 04 '14 at 12:20
  • Ok I feel dumb now - forgot the all important `session_start()`. Feel like such a noob. – ClarkeyBoy May 04 '14 at 12:20
  • @RoyalBg: I could do it that way but having to do that for all variables (bearing in mind this is a HUGE application) is just long - `getVariable` saves so much time and ensures that any updates to the way variables are picked up can be made just once. For example in the future I may want to pick up a default value for the current user from the DB. – ClarkeyBoy May 04 '14 at 12:22
  • possible duplicate of [Session lost after page redirect in php](http://stackoverflow.com/questions/3023353/session-lost-after-page-redirect-in-php) – Ejaz May 04 '14 at 12:24
  • @ClarkeyBoy it could be shortened with the ternary operator. However, maybe there are better ways to abstract it, this function could lead to unpredictable results, as you are expecting the variable you assign, to recieve the value of one of the superglobals, if it is set, with a precedence. And, also, seems to be much hard to debug, as you don't know where it fails. Tbh, I tested your code and first time I recieved your results, however, 10 sec later I had a red alert in my head, that I also forgot session_start() :) Maybe you need to automate this too – Royal Bg May 04 '14 at 12:27
  • @RoyalBg: Thanks for the info. I will look into ways to improve it in the future but this will have to do for now - too much to do, too little time. Not sure how I'd go about automating session_start() besides putting that line at the top of "includes.php" (which is the first thing to be included in all pages) - this is how I've implemented it now. – ClarkeyBoy May 04 '14 at 12:30
  • Fair enough. If it's put into a file/function that is called on each request :-) – Royal Bg May 04 '14 at 12:31

1 Answers1

0

Found the answer - I'm used to coding in classic ASP and .net which don't need a line to initialise the session. Solution was to put session_start() in the main includes file.

ClarkeyBoy
  • 4,934
  • 12
  • 49
  • 64
  • wow classic ASP :) Are there still sites that maintain a 10 years dead technology :))) – Royal Bg May 04 '14 at 12:28
  • lol yeah - one of our products is written in classic ASP. There have been debates about rebuilding it in another language but it's so stable that we kinda don't see a need to do so. – ClarkeyBoy May 04 '14 at 12:34