[RESOLVED] $_SESSION?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bwan37
Forum Newbie
Posts: 9
Joined: Thu May 22, 2008 11:42 am

[RESOLVED] $_SESSION?

Post 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'
?>
 
 
Last edited by bwan37 on Mon Jun 23, 2008 2:05 am, edited 1 time in total.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: $_SESSION?

Post 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'
?>
 
 
bwan37
Forum Newbie
Posts: 9
Joined: Thu May 22, 2008 11:42 am

Re: $_SESSION?

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: $_SESSION?

Post 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.
bwan37
Forum Newbie
Posts: 9
Joined: Thu May 22, 2008 11:42 am

Re: $_SESSION?

Post by bwan37 »

icic..
you mean if I set register_global=off
I cannot use $country to access $_SESSION['country'] right?
Got it!.. :)
Post Reply