Variable scoping in PHP4 v. PHP5

windows

Guest
I'm a novice PHP user. I had just figured out how to do basic sessions under PHP4 when a harddrive crash forced me to re-install everything. I upgraded to PHP5, and now the scripts I had just "perfected" don't behave. In passing variables from script to script via the address bar, e.g.:

<a href='http://www.phpbuilder.com/board/archive/index.php/page2.php?myvar=whatever' >

I found that $myvar would be empty under PHP5, whereas its value was 'whatever' under PHP4. However, I was able to correct this by using
$_REQUEST["myvar"] within page2.php to retrieve the value.

Now that that's fixed, I find that my session variables are getting lost, too. Here is the code that runs at the top of each script:

session_start();
if (!session_is_registered('aAllImages')) { session_register( 'aAllImages') ;}
if (!session_is_registered('img_cat')) { session_register( 'img_cat') ;}
if (!session_is_registered('type')) { session_register( 'type') ;}

.... and so on.

Even if one of these variables is assigned a value in script1, it comes up empty in script 2. Why is this, and is there an easy fix so I don't have to re-write all my scripts? And, what happens when I upload this code to my hosting service which is running PHP4.4? How good is the backward compatibility?

Thank you for any help you can offer!this whole issue is due to the fact that register_globals is now set to off by default in php5. this is a new security feature. instead of using the $_REQUEST[] (a catch all) array, you can use either the $_POST or $_GET array to retrieve passed variables. you will also need to use the $_SESSION[] array to retrieve values stored in sessions. you might also want to read up on the use of session_register() as it is no longer required.

there is more info on the differences between php4 and 5 here (<!-- m --><a class="postlink" href="http://www.php.net/manual/en/migration5.php">http://www.php.net/manual/en/migration5.php</a><!-- m -->)The reason your script worked before under PHP4 was because you had register_globals (<!-- m --><a class="postlink" href="http://us3.php.net/manual/en/ini.core.php#ini.register-globals">http://us3.php.net/manual/en/ini.core.p ... er-globals</a><!-- m -->) configuration setting on (in the php.ini file). When that's on the $myvar variable would be created automatically for you by PHP. With it off it wouldn't.

It's best to keep register_globals (<!-- m --><a class="postlink" href="http://us3.php.net/manual/en/ini.core.php#ini.register-globals">http://us3.php.net/manual/en/ini.core.p ... er-globals</a><!-- m -->) off for security reasons and program accordingly. That means using $_GET['myvar'] (or $_REQUEST['myvar']) instead.

Also, session_register() and related functions require register_globals (<!-- m --><a class="postlink" href="http://us3.php.net/manual/en/ini.core.php#ini.register-globals">http://us3.php.net/manual/en/ini.core.p ... er-globals</a><!-- m -->) be on too, and that's why your session script(s) aren't working. From PHP version 4.1.0 and higher, it's recommend to use the $_SESSION (<!-- m --><a class="postlink" href="http://us3.php.net/reserved.variables#reserved.variables.session">http://us3.php.net/reserved.variables#r ... es.session</a><!-- m -->) superglobal array variable instead.

Read up on some session tutorials.

hth.

Edit: Posted too late.

.Thank you for your replies. I've got things working again and understand the global variable issue much better now.
 
Back
Top