Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old May 31, 2002, 03:16   #1
garymailer
SitePoint Enthusiast
 
Join Date: Dec 2001
Location: London
Posts: 30
register globals off and legacy code

OK

So now php 4.1 and above has the register globals defaulted to off.

However as i am supporting legacy code then i need to switch them on just for that code?

I'm going to leave the default settings as register gobals = off in the php.ini file

i believe it is soething to do with $_server(register_globals)

can anyone shed some light on this ?
garymailer is offline   Reply With Quote
Old May 31, 2002, 03:27   #2
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
You've got a couple of choices.

One is to use a .htaccess file in the application directory containing;

Code:
<IfModule mod_php4.c> 
    php_flag register_globals on 
</IfModule>
The other alternative is some code which will act like register globals is on. This comes from here

PHP Code:

// set _GET and _POST _SESSION for old PHP variables:

if (!isset($_GET)) {
    
$_GET  = &$HTTP_GET_VARS;
}
if (!isset(
$_POST)) {
    
$_POST = &$HTTP_POST_VARS;
}
if (!isset(
$_SESSION)) {
    
$_SESSION = &$HTTP_SESSION_VARS;
}
if (!isset(
$_SERVER)) {
    
$_SERVER = &$HTTP_SERVER_VARS;
}
if ( !isset(
$_ENV)) {
    
$_ENV = &$HTTP_ENV_VARS;
}
if ( !isset(
$_COOKIE)) {
    
$_COOKIE = &$HTTP_COOKIE_VARS;
}
if ( !isset(
$_FILES)) {
    
$_FILES = &$HTTP_POST_FILES;
}
if ( !isset(
$_REQUEST)) {
    
$_REQUEST = &$_GET&$_POST&$_COOKIE&$_FILES;
}
PHP Code:

// Now pretend the register globals is on

if (isset($_GET)) {
   while (list (
$key, $value) = each ($_GET)) {
      $
$key = $value;
   }
}
if (isset(
$_POST)) {
   while (list (
$key, $value) = each ($_POST)) {
     $
$key = $value;
   }
}
if (isset(
$_SESSION)) {
   while (list (
$key, $value) = each ($_SESSION)) {
     $
$key = $value;
   }
}
if (isset(
$_SERVER)) {
   while (list (
$key, $value) = each ($_SERVER)) {
     $
$key = $value;
   }
}
if (isset(
$_ENV)) {
   while (list (
$key, $value) = each ($_ENV)) {
     $
$key = $value;
   }
}
if (isset(
$_COOKIE)) {
   while (list (
$key, $value) = each ($_COOKIE)) {
     $
$key = $value;
   }
}
if (isset(
$_FILES)) {
   while (list (
$key, $value) = each ($_FILES)) {
     $
$key = $value;
   }
}
if (isset(
$_REQUEST)) {
   while (list (
$key, $value) = each ($_REQUEST)) {
     $
$key = $value;
   }
}
You could simply include that in the required code.
HarryF is offline   Reply With Quote
Old May 31, 2002, 03:43   #3
garymailer
SitePoint Enthusiast
 
Join Date: Dec 2001
Location: London
Posts: 30
register globals off and legacy code

Thanks,

however that means that i have to go through the code and change all variables to have the $_GET / $_POST that is going to be a real pain :-).

I was hpoing there was a php option function that would allow me to switch on register globals.

Oh well never mind

thanks again
garymailer is offline   Reply With Quote
Old May 31, 2002, 04:29   #4
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
No no - it doesn't.

If you include that code above, you can keep register_globals off, and you won't need to change any code.

For example, if you have a link like;

http://www.yourdomain.com/index.php?view=news

And your code was like;

PHP Code:

if ( $view == "news" ) {

  
// Display news here
}
That works with globals on but not if globals are off.

That would correspond to the $_GET variable, i.e. $_GET['view'] = "news".

Now that code above "recreates" this variable to make it look like globals are on;

PHP Code:

if (isset($_GET)) {

   while (list (
$key, $value) = each ($_GET)) {
      $
$key = $value;
   }
}
That will assign the contents of $_GET['view'] to a variable called $view !

The first section of code above is for people using versions of PHP below 4.1, which don't regonised the new variables like $_GET - that's just for backward compatibility.

Why not try it on your code with register globals off - it should work.
HarryF is offline   Reply With Quote
Old May 31, 2002, 04:37   #5
garymailer
SitePoint Enthusiast
 
Join Date: Dec 2001
Location: London
Posts: 30
legacy code

Harry

Yeah i get it now

Thanks for your help :-D
garymailer is offline   Reply With Quote
Old May 31, 2002, 05:19   #6
garymailer
SitePoint Enthusiast
 
Join Date: Dec 2001
Location: London
Posts: 30
one more question

Ok then how do you pass the $_GET values to function

i have the following code

function show_top_menu($_GET['this_section_id'],$_GET['side_section_id'],$site_url)

but it throws this error


Parse error: parse error, unexpected '[', expecting ')' in g:\web_server\htdocs\mp\scripts\mcintosh.php on line 119

and i'm calling it from the following code

<?
show_side_menu($_GET['this_section_id'],$_GET['sub_section_id'],$site_url);
?>

any ideas?
garymailer is offline   Reply With Quote
Old May 31, 2002, 05:59   #7
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Errr - are you sure you've got the right function call?

The function you've put there is "show_top_menu" but you're calling "show_side_menu".

Anyway - what's on line 119?
HarryF is offline   Reply With Quote
Old May 31, 2002, 08:03   #8
Travis
Super Ninja Monkey
 
Travis's Avatar
 
Join Date: Dec 2001
Location: Sioux City, Iowa
Posts: 693
instead of that big chuck of code Harry gave for extracting the variables you could use extract($_GET) and so on for all the superglobals.
__________________
Travis Watkins - Hyperactive Coder
My Blog: Realist Anew
Projects: Alacarte - Gnome Menu Editor
Travis is offline   Reply With Quote
Old May 31, 2002, 08:12   #9
redemption
SitePoint Wizard
silver trophy
 
redemption's Avatar
 
Join Date: Sep 2001
Location: Singapore
Posts: 5,312
yeah exactly harry... i was looking through that long piece of code and thinking the exact same thing Trav said...

and yes code above line 119 would be good, together with an indication of what's line 119
redemption is offline   Reply With Quote
Old May 31, 2002, 08:29   #10
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Don't know the extract function that well. When it creates the variables from the array, does it maintain the reference to the origional data?

Doing this;

PHP Code:

while*(list*($key,*$value)*=*each*($_SESSION))*{

****$$key*=*$value;
}
The variables created point to the origional data, and with sessions in particular, there may be cases where you want to update the session variable, so the "dummy" variable needs to keep up with that. Is that something you can do with extract?
HarryF is offline   Reply With Quote
Old May 31, 2002, 08:31   #11
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Actually - now I look at it, the code I posted doesn't preserve the reference.

Guess it would need to be like;
PHP Code:

while*(list*($key,*$value)*=*each*($_SESSION))*{

***&$$key*=*$value;
}
But not 100% - believe each makes a copy anyway.
HarryF is offline   Reply With Quote
Old Jun 13, 2002, 15:05   #12
GeekSupport
SitePoint Evangelist
 
GeekSupport's Avatar
 
Join Date: May 2002
Location: Southern California
Posts: 408
Quote:
Originally posted by HarryF
You've got a couple of choices.

One is to use a .htaccess file in the application directory containing;

Code:
<IfModule mod_php4.c> 
    php_flag register_globals on 
</IfModule>
is there a way to apply this globally without having to copy the .htaccess file in every single directory?
GeekSupport is offline   Reply With Quote
Old Jun 13, 2002, 15:18   #13
DR_LaRRY_PEpPeR
Making a better wheel
silver trophy
 
DR_LaRRY_PEpPeR's Avatar
 
Join Date: Jul 2001
Location: Missouri
Posts: 3,425
just put it in the "highest up" directory. it will affect all subdirectories.
__________________
- Matt
Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
"Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR
DR_LaRRY_PEpPeR is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 09:24.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved