Page 1 of 1

[RESOLVED] $_SESSION?

Posted: Sat Jun 21, 2008 2:00 pm
by bwan37
Question:
How come I cannot change $_Session variable using the following..
$country = "china";


PC Setting: (localhost)
1. apache_2.2.6-win32-x86
2. php-5.2.4-win32
3. mysql-essential-4.1.22
4. phpMyAdmin-2.11.6


Code:

Code: Select all

===page1.php===
<?php
  session_start();
  $country = "usa";
  session_register("country");
?>
 
===page2.php===
<?php
  session_start();
  echo $_SESSION['country']; //output 'usa'
  [color=#FF0000]$country = "china";[/color]
  echo $_SESSION['country']; //output 'usa'
?>
 
 

Re: $_SESSION?

Posted: Sat Jun 21, 2008 2:12 pm
by Zoxive

Code: Select all

///===page1.php===
<?php
  session_start();
  $_SESSION['country'] = "usa";
?>
 
//===page2.php===
<?php
  session_start();
  echo $_SESSION['country']; //output 'usa'
  $_SESSION['country'] = "china";
  echo $_SESSION['country']; //output 'china'
?>
 
 

Re: $_SESSION?

Posted: Sun Jun 22, 2008 2:03 am
by bwan37
I do understand what you are saying...
but shouldn't
---> $country = 'china';
work as well?
is it because of my apache setting is missing something?
or that because I am using localhost?

Thank you very much :D :D

Re: $_SESSION?

Posted: Sun Jun 22, 2008 11:24 am
by jayshields
It's because $_SESSION['country'] and $country are 2 seperate variables. You managed to set a session variable using session_register in the first script, which is not the recommended method. You should set session variables by assigning values to the appropriate key in the $_SESSION super global array.

Re: $_SESSION?

Posted: Mon Jun 23, 2008 2:04 am
by bwan37
icic..
you mean if I set register_global=off
I cannot use $country to access $_SESSION['country'] right?
Got it!.. :)