Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with sessions

Status
Not open for further replies.

arlequin

Programmer
Sep 21, 1999
232
UY
Hello, there.

I've been reading faq434-2036 about Session Handling but still can work this issue out...

I am converting a static site into a dynamic one. One of the features involves i18n.
Example: I have a link on every PHP page which allows the user to switch languages in this way:

Code:
<a href="<?=$_SERVER['PHP_SELF']?>?lang=es">espa&ntilde;ol</a>
Code:
<a href="<?=$_SERVER['PHP_SELF']?>?lang=en">english</a>

In this way, I am using GET to send the [tt]lang[/tt] variable to the page itself when reloading.

On top of every site's page I include the following

Code:
<?
  require_once "inc/sess.php";
?>

...where I handle the session in the following way:

Code:
<?
///  sess.php

session_start();
$lang = $_GET['lang'];
if(!isset($_SESSION['language']) ||
   $lang!=$_SESSION['language']) {
   if( isset($lang) ) {
      $_SESSION['language'] = $lang;
   } else {
      $_SESSION['language'] = 'es';	
   }
}
?>

Doing this, I set Spanish as the default language or set the session variable to the language specified in the [tt]lang[/tt] variable.

I require the same code snippet on every page but when navigate to other page, the language is not set and drops to the default: Spanish....

I assume it's my logic proble, 'cause I've read PHP documentation about Sessions and it's very simple [sad]
and it makes me feel still more stupid [rednose]

Thanks In Advance

Arlequín
arlequin_AT_montevideo_DOT_com_DOT_uy
 
You've shown the code which stores the value in a session variable. How are you using that value in that session variable in your code?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214,

Thanks for replying.

In fact the code I use on every page is the same I posted.

I am sorry, I don't understando what do you need... :-(

Arlequín
arlequin_AT_montevideo_DOT_com_DOT_uy
 
The code you've posted stores the value in a session variable. The problem you describe could be caused by your application improperly storing the value, or by improperly trying to use that value later on.

Just storing the value in a session is not enough. In successive scripts, you need to use that value. How are you doing that?



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Oh, I understand now :)

This way:

Code:
require_once "inc/i18n/".$_SESSION['language'].".php";

Arlequín
arlequin_AT_montevideo_DOT_com_DOT_uy
 
Yes, of course.

It loads [tt]inc/i18n/es.php[/tt] for Spanish and [tt]inc/i18n/en.php[/tt] for English

Arlequín
arlequin_AT_montevideo_DOT_com_DOT_uy
 
When I navigate to another page...
The session value does not exist anymore...

Arlequín
arlequin_AT_montevideo_DOT_com_DOT_uy
 
session_start() must be invoked in every script which will manipulate session data. Are these other scripts invoking session_start()?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yes:

arlequin said:
On top of every site's page I include the following
Code:
<?
  require_once "inc/sess.php";
?>

Arlequín
arlequin_AT_montevideo_DOT_com_DOT_uy
 
Let's simplify the problem.

Here's two scripts:

session_set.php:
Code:
<?php
session_start();

$_SESSION['foo'] = 1;

print '<html><body><a href="session_read.php">click here</a></body></html>';
?>[code]

[b]session_read.php[/b]:
[code]<?php
session_start();

print '<html><body><pre>';

print_r ($_SESSION);

print '</pre></body></html>';
?>

If you create the two scripts, point your web browser to set_session.php, then click on the link, what you do get?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
As expected, I get the following....

[tt]
Array
(
[foo] => 1
)
[/tt]


Anyway, I don't understand why my [tt]$_SESSION['language'][/tt] variable value is not alive when I navigate...

Perhaps my logic is bad...

Arlequín
arlequin_AT_montevideo_DOT_com_DOT_uy
 
Yes, of course.

Arlequín
arlequin_AT_montevideo_DOT_com_DOT_uy
 
The I don't know what is causing the problem.

I recommend that you footprint your code to see what exactly is going on. Judicious use of error_log() to track what's going on in your code might be helpful.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thank you very much. I'll investigate this.

Arlequín
arlequin_AT_montevideo_DOT_com_DOT_uy
 
Do you have a paranoid firewall?

Try this code:
Code:
echo "<a href=\"{$_SERVER['PHP_SELF']}?=<?=SID?>&amp;lang=es\" title=\"espa&ntilde;ol, sen&ntilde;rita\">espa&ntilde;ol</a>";

if it works, also put the SID in your form actions..

If it works, your problem was doe to your firewall blocking the session cookie.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top