开发者

php session variable lost

开发者 https://www.devze.com 2023-04-12 04:53 出处:网络
I work with php5 and apache2 on my development machine. The production server is apparently similar. I have a script set_language.php that creates a session variable according to the language chosen:

I work with php5 and apache2 on my development machine. The production server is apparently similar.

I have a script set_language.php that creates a session variable according to the language chosen:

<?php
session_start();
$back = $_SERVER['HTTP_REFERER'];

    if (isset($_GET['lang'])) {
            if ($_GET['lang'] == 'fr')
            $_SESSION['lang'] = 'fr';
        else if ($_GET['lang'] == 'en')
            $_SESSION['lang'] = 'en';
    }
    header( "Location: $back" ) ;
    exit();
?>

Then I include the code below on all pages:

session_start();
if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'en') {
    require('lang_en.php');
}
else if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'fr') {
require('lang_fr.php');
}

Than I can get language-dependent variables with:

echo $lang[sometexttotranslate];

On the development server it works as expected. I click on the link to set the language and my session variables contain lang=en or fr

On the production server:

$_SESSION['lang'] = dn or dr (should be en or fr)

echo $lang[sometexttotranslate] = d (should be a translated text)

Any idea where the "d"'s come from?

开发者_如何学JAVA

This is not a redirection/header/session_start() problem.


print_r($_SESSION) on first load:

Array ( [lang] => fr )

print_r($_SESSION) after a click on the english link (set_lang.php above):

Array ( [lang] => dn )

print_r($_SESSION) after a click on the french link (set_lang.php above):

Array ( [lang] => dr )

print_r($_SESSION) after login:

Array ( [lang] => dr [valid] => 1 [pseudo] => GYC [uid] => 3 )


Now this is something,

print_r($_SESSION) after a click on the english link (set_lang.php above):

Array ( [lang] => dn )

your problem starts here in set_lang.php . Maybe a session is started from a file that is included and this should not happen!

If set_lang is not included make sure a start_session exist there, also put a print_r there too, you could even compare the session ids too:

let's say this is index.php

<?php
session_start();  
?>
<a href='set_lang.php?sess=<?PHP echo session_id();?>'>lang</a>

and this set_lang.php

<?PHP  
session_start(); 
echo 'this is session id from index.php: ',$_GET['sess'],
'and this is session id in set_lang.php:',session_id(),
'and this is the print_r:<br><pre>';
print_r($_SESSION);
?>

you should see the same session id.

But while all these might help you I will take it a step further, use single entry point, do not call php script directly like this

<a href=somescript.php>link</a>

(unless you are doing some ajax) instead you always call some php script through index.php like this:

<a href=index.php?target=somescript.php>link</a>

This way you will have the session start placed only once in the index.php, the login check can be done in index.php too.

=======================Edit More Info Added========================================= A fast google search.. found this small single entry tutorial http://www.renownedmedia.com/blog/php-navigation-system-using-single-entry-point/ there could be better ones.

Even better it would be switching to an mvc framework (that includes the single entry point philosophy) but because the learning curve is more steep at least start with the single entry point.

============ReEdit more info added again========================================== A good option then would be to replicate this problem again but out of your project context, take as less as possible code from your project in new created files and try to create the problem again. keep it as simple as possible just try to create the error again

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号