Forum Moderators: coopster

Message Too Old, No Replies

Problems passing variables in php5 compared to php4

         

kenfused

3:09 pm on Apr 28, 2008 (gmt 0)

10+ Year Member



Hello,Was hoping someone could help me.

On my old PHP 4 server, when I have a filename "joe.php" with the contents:

<? function joe($poo) {
echo $poo;} ?>

<? joe($poo);?>

If I go to [mysitephp4.com...]

I see a page with "5" printed on it.

ON my New PHP5 server, the same [myphp5site.com...]
yields nothing.

Is there a new way to pass variables in PHP5?

How do I fix my script?
THANKS

penders

3:18 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For your script to work,
register_globals
must be set to On (probably in php.ini). However, this is a bit of a security hole and is better Off (in fact it should be Off).

Parameters passed in the URL can be accessed from the $_GET[] array, so you can modify your code to read:

<?php joe($_GET["poo"]); ?>

EDIT: register_globals [uk2.php.net] is available in PHP4 and 5 - it is a configuration setting. But has been removed from PHP6 apparently.

PHP_Chimp

9:26 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Although may well not be related but you are using short opening tags i.e. <?. If you are removing all of the globals then you may also want to change your short tags for <?php, as short tags are another thing in php.ini so may or may not be enabled on a server.

henry0

10:23 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and won't be on #6

kenfused

4:04 am on Apr 29, 2008 (gmt 0)

10+ Year Member



Thanks.
I'm trying to get the syntax to work.

For a function:

<? function joe($A,$B) {
echo $A;
echo $B;} ?>

<? joe($_GET["A"],$_GET["B"]);?>

when i go to mysite.com/joe.php?A=5&B=2

I can't seem to get the page to output:

"5 2"

kenfused

4:21 am on Apr 29, 2008 (gmt 0)

10+ Year Member



Hmm.. seems to work now.

Can I define a variable for a page by doing:

$Jim=$_GET["Jim"];

Then proceed to use $Jim thru the page?
(assuming I go to mysite.com/test.php?Jim=5

penders

7:10 am on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$Jim=$_GET["Jim"];

Yes, that's OK. (Is there any kind of potential security issue with using the same variable name?)

It might be an idea to check that $_GET["Jim"] is actually set first (if 'Jim' has actually been passed in the URL) as you could get a warning (E_NOTICE) depending on how error_reporting() is set, and simply just good practice.

$Jim = isset($_GET["Jim"]) ? $_GET["Jim"] : null;

(Or any default value you require in place of null)