Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post

    $_SESSION value gets cleared moving to another page

    I am setting a PHP session with the following code:

    PHP 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:
    PHP Code:
    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

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Have you forgotten to add session_start(); in contact.php ?
    Last edited by abduraooft; 11-03-2007 at 04:06 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    crmpicco (11-03-2007)

  • #3
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    do i need to set the session again? is it not already set if i set it for that users session?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    crmpicco (11-03-2007)

  • #5
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    superb guys!!!!!

    That was all that was needed:

    PHP Code:
     session_start(); 
    Many thanks!!

    Picco

  • #6
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    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

  • #7
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    PHP Code:
    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

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    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.
    Last edited by CFMaBiSmAd; 11-04-2007 at 10:36 PM. Reason: Forgot to give my guess
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #9
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Pretty sure you need to use session_start() before you try to check to see if the session exists or not
    PHP Code:
    session_start();
    if(!isset(
    $_SESSION["lang"])) {
            
    $_SESSION["lang"] = 'de';

        }
        
    // redirect
        
    header"Location: de/index.php?lang=DE" ) ; 
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #10
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    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:
    Code:
    ; 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

  • #11
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    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.
    Last edited by CFMaBiSmAd; 11-04-2007 at 11:31 PM. Reason: add more
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #12
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    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:
    Code:
    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 Code:
    <?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:
    PHP Code:
        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.

  • #13
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    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...........

  • #14
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    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.)
    Last edited by CFMaBiSmAd; 11-05-2007 at 09:25 PM. Reason: add info
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #15
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    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?).

    Code:
    [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
    PHP Code:
    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:
    Code:
    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


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •