Cannot register $_SESSION var in IE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Cannot register $_SESSION var in IE

Post by papa »

Hi,

Code: Select all

<?php
 
session_start();
 
if(isset($_POST['lang'])) $_SESSION["lang"] = $_POST['lang'];
else $_SESSION["lang"] = "eng";
 
 
 
 
include_once("./inc/vars.inc");
include_once("./inc/header.inc");
include_once("./php/paintBall.php");
 
$paintBall = new paintBall();
 
?>
<div align="center">
<table border=0 width=550 height=100%>
<tr> 
<th colspan=2>PaintBall</th> </tr>
<tr>
 <td colspan=2><form method="post" action="index.php">
 <input type="image" src="./htdocs/img/swe.gif" name="lang" value="swe">
 <input type="image" src="./htdocs/img/eng.gif" name="lang" value="eng"> 
 </form></td>
Did a simple language selection. But it only works in Mozilla. I tried it in IE 6 but it doesn't seem to register the $_SESSION var. My problem seems to be that the form doesn't pass on the POST var.

Any suggestions how I should do for it to work in both browsers?

Probably very basic, but I'm stuck. :)
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Cannot register $_SESSION var in IE

Post by marcth »

Just to be sure, PHP doesn't really care what browser the client is using. If the code works in Firefox, it'll work in IE--the difference in behaviour you are noticing is probably due to the fact that one the firefox PHP session['lang'] is set and the one for IE is not.

First off, I wouldn't recommend accessing the session scope directly--Use a facade that has some convenience functions to make your life easier.

Code: Select all

// Facade to Session scope
class Session {
  public static function connect() {
    session_start();
  }
 
  public static function get($variable, $defaultValue=NULL) {
    return isset($_SESSION[$variable]) ? $_SESSION[$variable] : $defaultValue;
  }
 
  // Sets the session variable to defaultValue ONLY IF it is not already set.
  public static function init($variable, $defaultValue) {
    if(isset($_SESSION[$variable])) {
      return $_SESSION[$variable];
    } else {
      $_SESSION[$variable] = $defaultValue;
      return $_SESSION[$variable];
    }
  }
 
  public static function set($variable, $value) {
    $_SESSION[$variable] = $value;
  }
 
  public static function has($variable) {
    return isset($_SESSION[$variable]) ;
  }
 
  public static function remove($variable) {
    return unset($_SESSION[$variable]) ;
  }
 
}
Note: I use a similar class called Param to access get and post variables via the $_REQUEST scope.

Code: Select all

 
require_once('./inc/vars.inc');
require_once('./inc/header.inc');
require_once('./php/paintBall.php');
require_once('Session.php');
require_once('Param.php');
 
Session::connect();
Session::init('lang', Param::get('lang', 'eng')); // Pretty neat, eh?
 
$paintBall = new paintBall();
If you wanted to use your code, do it like this:

Code: Select all

 
  if(isset($_POST['lang'])) {
    $_SESSION["lang"] = $_POST['lang']; 
  } elseif(!isset($_SESSION['lang'])) {
    $_SESSION["lang"] = "eng";
  }
 
Attachments
Param.zip
Here is the Param class.
(1.04 KiB) Downloaded 59 times
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Cannot register $_SESSION var in IE

Post by papa »

Thank you very much for that elaborate answer!

I will use your suggestions and make a class for the session.

I also noticed that when using type="image" on my submit buttons it won't work with IE. So I temporary changed my language buttons to type=submit instead. And it seems to work fine. However I can't use my flags as intended. :P
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Cannot register $_SESSION var in IE

Post by marcth »

This might work:

Code: Select all

 
<button id="submitButton"><img src="" /></button>
 
Post Reply