Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

what wrong with $_SESSION

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

what wrong with $_SESSION

Postby DigitalRice » Sat Sep 20, 2003 11:30 pm

hi, i'm developing a website using version 4.2 at my pc and when upload into my server using php version 4.3, the $_SESSION doesn't work as expected.

here is my code

page1:

Code: Select all
<?php
session_start();

$myVar['username'] = "xyz";
$myVar['logged'] = true;

$_SESSION['myVar'] = $myVar;

?>

<html>
stuff
f
asf
sa
f
</html>


page2:

Code: Select all
<?php
   session_start();
   $myVar = null;
   $username = "";
    $logged = false;
    if (isset($_SESSION['myVar'])) {
       $myVar = $_SESSION['myVar'];
       $username = $myVar['username'];
       $logged  = $myVar['logged'];
    }
   
    echo ($username);
?>


but it doesn't display the user name when i upload in my server using php version 4.3, but in my machine at home using version 4.2, it does.

what is wrong?

Thanks
DigitalRice
New php-forum User
New php-forum User
 
Posts: 29
Joined: Fri Jun 27, 2003 10:05 pm

Postby Joel » Sun Sep 21, 2003 7:28 pm

Maybe the sessions are transferred differently on the servers PHP.INI - Maybe they have them disabled. Do a phpinfo() and compare the results under Sessions with a phpinfo() on your home computer.
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Postby DigitalRice » Sun Sep 21, 2003 10:22 pm

Thank Joel, I did compare my phpinfo() with the one on the server ,and they are the same. after awhile of debugging, a strange thing happen (at least to me)

if the second page code like this by commenting three variable at the beginning:
Code: Select all
<?php
     session_start();
    /*
     $myVar = null;
      $username = "";
       $logged = false;
   */

    if (isset($_SESSION['myVar'])) {
        $myVar = $_SESSION['myVar'];
        $username = $myVar['username'];
        $logged = $myVar['username'];
     }
      echo ($username);
}

then it print the username.

what i find it strange is that i thought that all the variable on top of the if statement will be global scope, but it actually not, the $username inside the if statement is global from there on, how come?
Last edited by DigitalRice on Sun Sep 21, 2003 10:29 pm, edited 1 time in total.
DigitalRice
New php-forum User
New php-forum User
 
Posts: 29
Joined: Fri Jun 27, 2003 10:05 pm

Comments

Postby ruturajv » Sun Sep 21, 2003 10:28 pm

how can the variable be made if they are commented?
You wrote yourself you've commented those!!! ...?????
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India

Postby DigitalRice » Sun Sep 21, 2003 10:54 pm

tp ruturajv

sorry for not being clear... I was refering to my original post, the first post of this thread that my page2 have all those variables if you scroll upward looking at my first post.
but when i commented those three variable before the if statement, everything works fine. The reason that i'm confusing that if the variable was set to some value outside of the if statement scope, whatever inside the if statement if the variables are reassign won't affect, that is what i'm confused about. someone explain please
DigitalRice
New php-forum User
New php-forum User
 
Posts: 29
Joined: Fri Jun 27, 2003 10:05 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.