php session won't show after redirect

Discussion in 'Programming/Scripts' started by hanswid, May 28, 2008.

  1. hanswid

    hanswid New Member

    i have a php login script, which will check the login against mysql database, and on successful check, i create a session like this :

    $verified_user = $_SESSION['verified_user'];

    and if I echo out $verified_user, it works (user name shows) on the same file. On my script, after successful login, i redirect it to another page, let's say success.php. But in success.php if i echo out $verified_user, it shows nothing. Both files has session_start() of course. Session variable supposed to be registered already.

    Funny thing is, this scripts work fine on my old server (suse 10.0). I also have checked phpinfo, it was --enable-session' for the configuration. Btw, i set up ISPConfig on my openSuSE 10.3 base on the tutorial here.

    How do i fix that session variable thing? Thx for any help.
     
  2. Ben

    Ben Active Member Moderator

    may you post the code here (best in the [ code ] tag!) to better see what's going on or not?

    next thing is, did you allow / set session use cookies? Otherwise you have to attach the PHPSESSID=session_id() parameter when forwarding, or a new session will be created. An output of phpinfo() would help here to find this out.
     
  3. hanswid

    hanswid New Member

    problem solved. i just needed to re-declare session name on the redirected page, though it was not necessary on my suse 10 server, don't why.
     
  4. collardgreen

    collardgreen New Member

    check your php.ini settings

    the reason you had to re-declare your variable was probably due to the register_globals setting in your php.ini being turned Off.
    simply change the value to :
    register_globals = On
    to enable this.

    i cant remember on which version PHP started defaulting this value to Off...
     
  5. topdog

    topdog Active Member

    turning on register globals is not encouraged it leads to security problems.
     
  6. collardgreen

    collardgreen New Member

    Exactly!

    Agreed! not advised, but, in the past i have had to deal with legacy code that required it.
     
  7. AnilKumar

    AnilKumar New Member

    Without change the register_global variables how can we get the solution for that problem,
     
  8. collardgreen

    collardgreen New Member

    re-declare the variable

    On the page he is redirecting to he would have to re-declare the variable for use on that page.
     
  9. AnilKumar

    AnilKumar New Member

    hi, friends
    i written the code for session variable when i redirect the page then session variable is not set,in the same page when i display the session then it display the session variable, after that redirected to page through header then that page is session is not set in that page.
    in the previous server PHP Version 4.3.10-22 in that it is working fine, i am copy that site to new server PHP Version 5.1.6 .
    in the new server also register_globals is on.
    And both create session and redirected page is also in the same folder, and on each page i written the code session_start(),
    and before redirected the page i written session_write_close();
    i tried both putting comment that line and remove the comment that line, please give me any suggestion, to overcome from this regards, i am spend the lot of time to this one.

    Thanks in advance
    :mad:
     
  10. collardgreen

    collardgreen New Member

    try this simple test.

    Create 2 files, test1.php and test2.php.

    in test1.php, copy this PHP code:
    Code:
    <?php
    session_start();
    
    $_SESSION['test'] = 'testing';
    ?>
    Now in test2.php, copy this PHP code:
    Code:
    <?php
    session_start();
    
    if (isset($_SESSION['test']))
    {
    	echo $_SESSION['test'];
    }
    else
    {
    	echo "session variable was not set";
    }
    
    echo '<pre>';
    print_r($_SESSION);
    echo '</pre>';
    ?>

    upload these 2 files to your server in a place where they are web accessible.
    point your browser to http://yoursite/test1.php. Now go to http://yoursite/test2.php.

    you should see the text string "testing" echoed to the screen, under that, you should see the output of the $_SESSION super global array.
    if the test was successful, you should see something like the following:

    testing

    Array
    (
    [test] => testing
    )

    if this does not work as expected you may want to turn on error logging and display in your php.ini file and check for session related errors.

    if you do see any errors, please copy/paste them here and i will try to help you further.
     
    Last edited: Sep 9, 2008
  11. AnilKumar

    AnilKumar New Member

    hello,
    i am upload 2 files to my server, i run the test1.php , no error is displayed , but i run the test2.php,
    i have the following output :

    session variable was not set
    Array
    (
    )



    give me any solution to overcome the error.
    thanks in advance.
     
  12. collardgreen

    collardgreen New Member

    Ah ha!

    now we are on to something!
    looks like sessions are not working correctly!

    look in your php.ini file and search for these 2 settings:

    session.save_path
    session.save_handler

    copy/paste what you find. i suspect that either the path on the server to where the session files are to be stored is incorrect or the handler is not set to "files".

    also, are you running PHP on windows or linux?
     
  13. AnilKumar

    AnilKumar New Member

    hello,

    session.save_path = /var/lib/php/session

    session.save_handler = files

    and i am running my server in windows NT, PHP Version 5.1.6.
     
  14. collardgreen

    collardgreen New Member

    I think i have found your problem!

    session.save_path = /var/lib/php/session

    if you are running this on Windows, "/var/lib/php/session" path does not exist.
    try creating a directory such as c:\temp\session

    now change the setting in php.ini to:
    session.save_path = c:\temp\session

    i think on windows you can use forward or backward slashes in paths. i dont think you will have to put the path in double quotes like "c:\temp\session", but if it doesn't work without quotes, try it with them.

    Note that you will have to restart your webserver after making any changes to the php.ini file for them to take effect.

    one other "gotcha" may be your "include_path" in the php.ini. i think you will have to change it to a Windows path if you use the include_path for any of your applications.
    in my php.ini file i have something like the following:

    include_path = ".:/php/includes:/data/app/includes"
    ;include_path = ".;c:\php\includes"

    for your case, you would comment the first line with a semicolon (;) and uncomment the second line, changing the path to your includes directory accordingly.
     
  15. AnilKumar

    AnilKumar New Member

    hello,
    i run my site in new hosting server, in my localhost the
    session.save_path = C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp , in that it is running perfectly,
    in the hosting server i have the problem.,
     
  16. collardgreen

    collardgreen New Member

    do you have the ability to change the php.ini on your hosting server?

    make sure the directory permissions are set correctly so that your webserver can write the the directory.

    if you are unable to modify the hosting server php.ini, you can use the PHP function: session_save_path() to set a directory that you can write to within your web directory such as:

    Code:
    <?php 
    session_save_path('/path/to/htdocs/sessions/');
    session_start();
    
    ?>
    again, make sure this directory exists and has write permissions for your webserver user.
     
  17. AnilKumar

    AnilKumar New Member

    i had written code what you mentioned, but no use it is not working,

    same error

    session variable was not set

    Array
    (

    )
     
  18. BeCase

    BeCase New Member

    Try to set php.ini option:
    Code:
    session.cookie_path = /
     

Share This Page