Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

php session variable lost

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?

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 )

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...