[PHP] No Global Code Fixing
Well I've been fixing up all my code (and other peoples which is worst) getting ready to do an upgrade to 4.3. and turning off globals and warnings on. I very often move parameters that were once POSTed as a GETs. For instance... some one does a search but is not logged in, they can see the results but don's see the "Edit" results button. So they log in, and I send them back to the search they just did. The first search is done by a POST and when I redirect them after the login it's done by a GET. So I use to simply not specify if it was a GET or POST and looked to see if the var existed or not to see how to load that page. Now I've been adding alot of: if (isset($_POST['keyword'])){ $keyword = $_POST['keyword']; }elseif (isset($_GET['keyword'])){ $keyword = $_GET['keyword']; } else { unset($keyword); } I suppose I could also do something like this (which is not much different) if(isset($_POST['keyword']) || isset($_GET['keyword'])){ $keyword = isset($_POST['keyword'])?$_POST['keyword']:$_GET['keyword']; }else{ unset($keyword); } I guess I could get rid of the unset, but I like it there just in case something earlier filled that puppy. So I end up with alot of these right now at the top of each page. Especially if the URI is something like http:www.mysite.com/index.php?this=that&id=1&lang=en&so=on&so=on&so=on&so=on&so=on&so=on Know what I mean? So just wondering if anyine had something really elegant to replace it. Cheers, Mike
Re: [PHP] No Global Code Fixing
Why don't you just use $_REQUEST['keyword'] ? On Sat, 4 Jan 2003, Michael J. Pawlowsky wrote: > Well I've been fixing up all my code (and other peoples which is worst) getting >ready to do an upgrade to 4.3. and turning off globals and warnings on. > > I very often move parameters that were once POSTed as a GETs. > For instance... some one does a search but is not logged in, they can see the >results but don's see the "Edit" results button. > So they log in, and I send them back to the search they just did. > > The first search is done by a POST and when I redirect them after the login it's >done by a GET. > > So I use to simply not specify if it was a GET or POST and looked to see if the var >existed or not to see how to load that page. > > Now I've been adding alot of: > > > if (isset($_POST['keyword'])){ > $keyword = $_POST['keyword']; > }elseif (isset($_GET['keyword'])){ > $keyword = $_GET['keyword']; > } else { > unset($keyword); > } > > > I suppose I could also do something like this (which is not much different) > > if(isset($_POST['keyword']) || isset($_GET['keyword'])){ > $keyword = isset($_POST['keyword'])?$_POST['keyword']:$_GET['keyword']; > }else{ >unset($keyword); > } > > I guess I could get rid of the unset, but I like it there just in case something >earlier filled that puppy. > > So I end up with alot of these right now at the top of each page. > Especially if the URI is something like >http:www.mysite.com/index.php?this=that&id=1&lang=en&so=on&so=on&so=on&so=on&so=on&so=on > > Know what I mean? > > So just wondering if anyine had something really elegant to replace it. > > > Cheers, > Mike > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] No Global Code Fixing
I just found a better answer, but still open to suggestions with the URI : http://rc.mikeathome.net/test/index.php?one=1&two[]=2&two[]=3&three=3&key=This%20is%20the%20key I tried this: $value){ ${$key}=$value; } echo $one; echo ""; print_r($two); echo ""; echo $three . "\n"; echo $key; } ?> Result: - 1 Array ( [0] => 2 [1] => 3 ) 3 This is the key And that actually worked... I can live without the unset()'s. Just need to add the same for POST and do an include with it. Anyone have a better idea? *** REPLY SEPARATOR *** On 04/01/2003 at 1:22 PM Michael J. Pawlowsky wrote: > >So just wondering if anyone had something really elegant to replace it. > > >Cheers, >Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] No Global Code Fixing
Hey that's great... I didn't know about $_REQUEST I suppose the order of overlapping is set in php.ini I think I saw that somewhere. Thanks As for tunring it back on... in .htaccess I like the idea of having cleaner code *** REPLY SEPARATOR *** On 04/01/2003 at 11:37 AM Rasmus Lerdorf wrote: >Why don't you just use $_REQUEST['keyword'] ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] No Global Code Fixing
NOTE: this basicly mimics the way register_globals works. I use this code to fix register_globals aswell as the magic_quotes_gpc: $global = @array($_SESSION, $_SERVER, $_COOKIE, $_POST, $_GET, $_FILES, $_ENV); $global_old = @array($HTTP_SESSION_VARS, $HTTP_SERVER_VARS, $HTTP_COOKIE_VARS, $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_FILES_VARS, $HTTP_ENV_VARS); if(!$global[1]){ $global = $global_old; } if(!@ini_get('magic_quotes_gpc')){ foreach($global as $array){ if(is_array($array)){ foreach($array as $key=>$val){ $$key = addslashes($val); } } } }else{ foreach($global as $array){ if(is_array($array)){ foreach($array as $key=>$val){ $$key = $val; } } } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] No Global Code Fixing
If you do that you might as well just turn on register globals, you should look at the $_REQUEST variable, it combines $_POST, $_GET and $_COOKIE into one array so you can just reference $_REQUEST['variable'] for example $_REQUEST['one']. Jason On Sat, 2003-01-04 at 11:50, Michael J. Pawlowsky wrote: > > I just found a better answer, but still open to suggestions > > with the URI : >http://rc.mikeathome.net/test/index.php?one=1&two[]=2&two[]=3&three=3&key=This%20is%20the%20key > > I tried this: > > > > if (isset($_GET)){ > foreach($_GET as $key => $value){ > ${$key}=$value; > } > echo $one; > echo ""; > print_r($two); > echo ""; > echo $three . "\n"; > echo $key; > } > > ?> > > Result: > - > 1 > Array ( [0] => 2 [1] => 3 ) > 3 > This is the key > > > > And that actually worked... I can live without the unset()'s. > Just need to add the same for POST and do an include with it. > > Anyone have a better idea? > > > > *** REPLY SEPARATOR *** > > On 04/01/2003 at 1:22 PM Michael J. Pawlowsky wrote: > > > > >So just wondering if anyone had something really elegant to replace it. > > > > > >Cheers, > >Mike > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php