...

View Full Version : $_SESSION value gets cleared moving to another page



crmpicco
11-03-2007, 02:59 PM
I am setting a PHP session with the following code:



session_start();
$_SESSION["lang"] = "de";


This is set on a page called "index.php", on "index.php" there is a link to "contact.php". On "contact.php" I am checking for the session value, but it appears to be blank?

Simply using:
echo "<strong>HEADER SESSION -> " . $_SESSION["lang"] . "</strong>";


Is there something that is clearing it? I'm not using anything to clear the session value, just don't understand how it is behaving here?

Picco

abduraooft
11-03-2007, 03:11 PM
Have you forgotten to add session_start(); in contact.php ?

crmpicco
11-03-2007, 03:25 PM
do i need to set the session again? is it not already set if i set it for that users session?

_Aerospace_Eng_
11-03-2007, 03:28 PM
Yes you need it again. On any page you want to use sessions on you need session_start(); at the very top of the page before any output to the user. We wouldn't suggest it, if it wasn't needed.

crmpicco
11-03-2007, 04:04 PM
superb guys!!!!!

That was all that was needed:


session_start();

Many thanks!!

Picco

crmpicco
11-04-2007, 09:58 PM
ok, is there an issue with this code on PHP 4.4.4?? I have uploaded this code to the server and this appears to be another problem moving from a PHP 5 -> PHP 4 server.

It works fine on my local machine (PHP 5), but the session seems to be cleared even with using session_start() on the server (PHP 4).

Is this a known issue?

Picco

crmpicco
11-04-2007, 10:13 PM
if(!isset($_SESSION["lang"])) {
session_start();
$_SESSION["lang"] = 'de';

}
// redirect
header( "Location: de/index.php?lang=DE" ) ;
That is more or less what i do. Then, if i click a link on the page "de/index.php?lang=DE" the session is lost. Even although I have session_start() at the top of every script.

Picco

CFMaBiSmAd
11-04-2007, 10:27 PM
There are very few backward incompatible core differences between PHP4 and PHP5. 99.5+% of the time when code works on one server/version and not another it is due to php configuration differences.

In your case it is probably something the code is doing wrong that is masked on one of the servers.

Check your web server log for errors and/or turn on full php error reporting.

If there are no errors and you have variables being overwritten, it is usually because register globals are on and you have program/POST/GET/COOKIE/SESSION variables with the same name and they are overwriting whichever ones get populated last.

Edit: For your specific issue, I suspect that output buffering is on in php.ini for the system where it works, but not the other.

_Aerospace_Eng_
11-04-2007, 10:33 PM
Pretty sure you need to use session_start() before you try to check to see if the session exists or not

session_start();
if(!isset($_SESSION["lang"])) {
$_SESSION["lang"] = 'de';

}
// redirect
header( "Location: de/index.php?lang=DE" ) ;

crmpicco
11-04-2007, 11:02 PM
if it IS a setting in Turtle Hosting's php.ini that is a problem for me really isn't it? As i'm unable to gain access to that file to modify it.

Locally my current setting is:


; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP's output layer a
; bit. You can enable output buffering during runtime by calling the output
; buffering functions. You can also enable output buffering for all files by
; setting this directive to On. If you wish to limit the size of the buffer
; to a certain size - you can use a maximum number of bytes instead of 'On', as
; a value for this directive (e.g., output_buffering=4096).
output_buffering = 4096


Aerospace that piece of code seems to work fine, it's just when I move onto another page after the redirection it loses the session.

The phpinfo() function is up live at:
http://ayrshireminis.com/mini/phpinfo.php

Any help welcomed,
Picco

CFMaBiSmAd
11-04-2007, 11:25 PM
Your on line phpinfo() indicates that there is no value for output_buffering (I would assume this defaults to off) and register_globals are ON. Either of these could cause the symptoms you describe. However, the output_buffering being off would show up as errors in the web server log.

Either of these settings can usually be changed in a .htaccess file or a local php.ini file, depending on how php is invoked (cgi wrapper or server module.)

The solution if this is due to output_buffering is either - A) Correct the program logic or how the file got saved (UTF-8 encoded instead of ANSI/ASCII) so that it is not outputting content prior to outputting headers (any error message would help identify which of these is occurring), or B) Turn on output_buffering in a .htaccess file or a local php.ini file. You could also temporarily turn off output buffering on your development system to see if the symptoms are the same.

The solution if this is due to register_globals being on is either - A) Rename any program/POST/GET/COOKIE/SESSION variables that have the same name to having all unique names, or B) Turn off register_globals in a .htaccess file or a local php.ini file.

Posting your entire code would also help to answer the most important question of - are you outputting content to the browser prior to the session_start() statement.

crmpicco
11-05-2007, 08:31 PM
Thanks for all that info, i got a response back from my host saying that PHP 4 and PHP 5 are both enabled on the servers. Which is good. All I had to do was add a line in my .htaccess file:
AddHandler application/x-httpd-php5 .php

I'm now running PHP 5.1.6, which is good again, but I am still having my sessions cleared.

http://ayrshireminis.com/mini/phpinfo.php

I'm not outputing anything to the browser prior to setting session_start() in the code. Example:

galleries.php:


<?php
/*
galleries.php
Gallery that displays the images that have been uploaded to the site
13-Oct-2007
[Deutsch Version]
*/
include($_SERVER["DOCUMENT_ROOT"] . "/mini/inc/dbconn.php");

session_start();

$lang = $_GET["lang"];
$badimage = $_GET["badimage"];
$page = $_GET["page"];

//date_default_timezone_set('Europe/Berlin');
// Set the gloabal LC_TIME constant to german for the purpose of the date
setlocale(LC_ALL, "de_DE", "de_DE@euro", "deu", "deu_deu", "german");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>AyrshireMinis.com - a Mini E-Community</TITLE>
...................


The Session would be set like:


session_start();
if(!isset($_SESSION["lang"])) {
$_SESSION["lang"] = 'de';
}
// redirect
header( "Location: de/index.php?lang=DE" ) ;


When on de/index.php the session is still there, but when i click a link to go to galleries.php it loses the session value.

Also, I don't think I have access to a php.ini file, only .htaccess.

crmpicco
11-05-2007, 08:43 PM
Output Buffering is ON at my local machine

phpinfo();
======================================
output_buffering 4096 4096
output_handler no value no value
======================================

Can output buffering be turned on from the .htaccess file? Maybe this is my problem as you say...........

CFMaBiSmAd
11-05-2007, 08:51 PM
If the problem is not being caused by something that output buffering will fix, turning it on won't solve the problem. Just randomly changing settings is not good troubleshooting technique.

Find out why the sessions are not resuming on the second page. We are still waiting on information from your web server log about errors and/or turn on full php error reporting.

Edit: Sessions might not exist on a second page for a number of reasons (assuming that the server is setup so that sessions work at all) -

1) The session was not actually started on the first page, due to errors that would appear in your web server log file. Under this condition you can set variables $_SESSION['your_var'] and access them on that page, but they are just named array variables since the session did not start.

2) The server did not send the session id either in a cookie or on the end of the url.

3) The browser did not accept the session cookie.

4) The browser did not return the session cookie to the second page because of a domain/path setting and mismatch.

5) The session was not resumed on the second page due to errors that would appear in your web server log file.

Even through you might not be intentionally outputting content prior to the session_start(), your files could be (either due to how they are saved or even if your include file contains some white space at the beginning or end of it or your mail file contains some white space before the first <?php tag.)

crmpicco
11-05-2007, 09:57 PM
Hi there, I checked the apache error log on the server, this appears to be all the lines in the log (it says it'll keep 300?).



[Mon Nov 5 15:19:05 2007] [error] [client 86.31.117.233] File does not exist: /home/ayrshire/public_html/404.shtml
[Mon Nov 5 15:19:05 2007] [error] [client 86.31.117.233] File does not exist: /home/ayrshire/public_html/js/general.js
[Mon Nov 5 15:18:37 2007] [alert] [client 86.31.117.233] /home/ayrshire/public_html/.htaccess: Invalid command 'php_value', perhaps mis-spelled or defined by a module not included in the server configuration
[Mon Nov 5 15:18:37 2007] [alert] [client 86.31.117.233] /home/ayrshire/public_html/.htaccess: Invalid command 'php_value', perhaps mis-spelled or defined by a module not included in the server configuration
[Mon Nov 5 15:18:35 2007] [alert] [client 86.31.117.233] /home/ayrshire/public_html/.htaccess: Invalid command 'php_value', perhaps mis-spelled or defined by a module not included in the server configuration
[Mon Nov 5 15:18:35 2007] [alert] [client 86.31.117.233] /home/ayrshire/public_html/.htaccess: Invalid command 'php_value', perhaps mis-spelled or defined by a module not included in the server configuration
[Mon Nov 5 14:05:59 2007] [error] [client 86.31.117.233] File does not exist: /home/ayrshire/public_html/404.shtml
[Mon Nov 5 14:05:59 2007] [error] [client 86.31.117.233] File does not exist: /home/ayrshire/public_html/js/general.js
[Mon Nov 5 13:41:35 2007] [error] [client 66.249.72.69] File does not exist: /home/ayrshire/public_html/404.shtml
[Mon Nov 5 13:41:35 2007] [error] [client 66.249.72.69] File does not exist: /home/ayrshire/public_html/robots.txt


I have now turned on PHP Error Reporting with
error_reporting(E_ALL);
- which I put in the header (included in every script).

Is there a way I can determine if PHP sessions are supported on the server?

When you say "2) The server did not send the session id either in a cookie or on the end of the url." - i am not actually passing the $_SESSION anywhere? My understand was as soon as it was set then it was available by simply calling and not passing it through the URL or a Cookie etc...

Turning on the error reporting doesn't appear to help a great deal with regards to the session, it is output messages such as:


Notice: Undefined index: lang in /home/ayrshire/public_html/mini/inc/header.php on line 15
Notice: Undefined index: category in /home/ayrshire/public_html/mini/de/galleries.php on line 34
Notice: Undefined index: year in /home/ayrshire/public_html/mini/de/galleries.php on line 35

Doesn't appear to be anything pertaining to the session in there :-(

Picco

crmpicco
11-05-2007, 11:06 PM
Notice: Undefined index: lang in /home/ayrshire/public_html/mini/inc/header.php on line 15


That was the problem, using a GET variable that no longer existed :-)

Cheers for you help!!!

Can't see the wood for the trees



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum