Re: Simple Unidentified Variable
| From: | Steve Buehler | Date: | Wed, 08 May 2002 21:19:12 +0000 |
| Subject: | Re: Simple Unidentified Variable | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-96676@lists.php.net to get a copy of this message | ||
Just to make sure if I have this right for any new programming. If I have:
http://www.domain.com/script.php?div_id1=1&name=thisteam
To extract these in any php version I would use:
$div_id1=$HTTP_POST_VARS[div_id1];
$name=$HTTP_POST_VARS[name];
Is this correct? It does work fine when I tried it on my php versions 4.1.2 and 4.0.6. Hmmmm. I didn't think that my versions were that new. Will they work that way with earlier versions of PHP? Basically, I am trying to start coding so that my programs will work on any version of php.
Thanks
Steve
At 01:59 PM 5/8/2002, Philip Olson wrote:
if (!ini_get('register_globals')){ extract($HTTP_SERVER_VARS); } That code will just extract SERVER vars, so it'll create such beasts as $DOCUMENT_ROOT, $PHP_SELF, $HTTP_REFERER, etc. That's it. It sounds like you want many vars to behave this way, not just SERVER ones. Since you're talking about older scripts, extract() will do your bidding. You want GET, POST, COOKIE and perhaps a few others? Try this: // register a lot of vars if register_globals = off if (!ini_get('register_globals')) {$types_to_register = array('GET','POST','COOKIE', 'SESSION','SERVER'); foreach ($types_to_register as $type) { if (@count(${'HTTP_' . $type . '_VARS'}) > 0) { extract(${'HTTP_' . $type . '_VARS'}, EXTR_OVERWRITE); } }} Not sure how bullet proof that is, will think about it. This doesn't depend on the variables_order directive like register_globals does, but it could. In the above, COOKIE vars will overwrite GET/POST vars, etc. if a collision exists. Regards. Philip Olson On Wed, 8 May 2002, Steve Buehler wrote: I have access to my php.ini files for the servers, but some of my scripts will not be on my servers after I write/wrote them. I read one of your links and at the end, it talks about using: if (!ini_get('register_globals')){ extract($HTTP_SERVER_VARS); } If I put this in all of my scripts, will that make it portable from the old PHP versions to the New ones? Or am I going to have to go back through and change everything over manually so that they work on all PHP versions? I have a lot of scripts that are out there from pervious versions of PHP and it would be nice if that would be the only upgrade that I would have to put into them to make them work because there are so many of them out there that are customized. Also, I am going to have a lot of pissed of customers that are on other servers and their scripts are going to be broken if their server gets upgraded to the new PHP where I am grabbing foo.php?a=apple and just saying print $a; Steve At 12:38 PM 5/8/2002, Philip Olson wrote:Hi, Everyone makes it sounds that since 4.2.0 it's impossible to do: http://www.example.com/foo.php?a=apple print $a; // apple It is still very possible. The main way to make this possible is via the register_globals directive, a PHP setting that can be changed in PHP's configuation file, php.ini The default of this directive changed from on to off as of 4.2.0, this is just the default. Everyone should read php.ini as it contains information about many directives that affect our scripts. Please do not hesitate to ponder over php.ini: http://cvs.php.net/co.php/php4/php.ini-dist There are several ways to "reflect" the behavior of register_globals, methods include: 0) turning it on in php.ini (sysadmin) a) .htaccess : if the sysadmin allows use ofsite, ....htaccess, then the following will work in it:php_flag register_globals onb) use extract() or import_request_variables() c) don't code with this behavior Check the archives for more tips, I've ranted a few times in the recent past :) Such as: http://marc.theaimsgroup.com/?l=php-general&m=102036870428992 And btw, undefined variable errors are of level E_NOTICE and configured via either the error_reporting directive in php.ini or at runtime via the error_reporting() function. Regards, Philip Olson On Wed, 8 May 2002, Analysis & Solutions wrote:Hi Jeremy: On Wed, May 08, 2002 at 01:06:16PM +0800, Jeremy wrote:I am very new to PHP and when doing the tutorial on the mysql-- 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.phpNotice: Undefined variable: "value" in .....\welcome.php on line 2 even though they have been entered in the html form and they are in $HTTP_POST_VARS.Things have changed in PHP. GET and POST variables are no longer automatically globalized. Use "$_POST['value']" rather than "$value."