Forum Moderators: coopster

Message Too Old, No Replies

Alternative to global variables in php functions?

General topic

         

fish_eye

1:03 am on Aug 2, 2005 (gmt 0)

10+ Year Member



I'm fairly new to php and a new site of mine was set up with register_globals off.

Some code I just created needs them on.

The hosting company advised that this type of global is being phased out for security reasons (but swtiched them on anyway).

What is(are) the alternative(s)? I noticed in this post http://www.webmasterworld.com/forum88/5763.htm [webmasterworld.com] that post and get were suggested.

My particular situation is pretty simple:

I have 3 includes in one 'php program' (which is largely html with a couple of <?php's in it). I want to declare the variable in the main php program, use it there (no probs) and then use it in the 3rd include. Is there a technique you can suggest? I specifically want to use includes to avoid duplicating the html in each and every page.

I've considered having a function and passing this the variables but I can't see that this will be any different unless I physically put the function in each and every page - which defeats the purpose.

In my case here to I use a (sub-)form on the pages and post to it? I'm not sure how to do this (exactly).

Any suggestions / alternatives to the above (or perhaps sticky me references to documentation please)?

mogenshoj

8:12 am on Aug 2, 2005 (gmt 0)

10+ Year Member



If you use this on page1:

<?
$var1 = "bah";

include("page2.php");
?>

Then var1 will still have the value "bah" in the included content from page2.

It shouldn't be a problem passing variables through include.

If you want to use functions, you can do it like this:

// set the function with one variable passed.
function my_function ($var1) {
// something here

}

// call the function and pass one variable.
my_function($var1);

chrisjoha

10:58 am on Aug 2, 2005 (gmt 0)

10+ Year Member



With register_globals off you're still able to use global variables aren't you? I thought register_global only controlled request variables? You can still use global $var; can't you?

fish_eye

11:03 am on Aug 2, 2005 (gmt 0)

10+ Year Member



I was

include 'http://www.example.com/include.php';

rather than

include ($_SERVER['DOCUMENT_ROOT'].'/include.php');

The includes happily included but stuffed up the scope it seems!

You learn something new everyday - nothing to do with globals - but I'd still like to explore the alternatives if anyone can offer some discussion documents (I don't quite understand what I've been reading in the standard online manual).

mcibor

11:21 am on Aug 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. There are some methods you can pass the variables:

1. Super globals as $_POST, $_GET, $_COOKIE, $_SERVER, $_SESSION, $_FILES are available everywhere on the server. (By everywhere I mean in functions, objects and includes).

2. All variables are available in includes.

3. To pass variable into the function you can use:
a) passing by a value:
function my($value) { $value++; echo $value." is "; return $value;}
echo my(0);//echo 1 is 1
$a = 1;
$b = my($a);
//echo 2 is
//a = 1, b = 2 - value of a unchanged by function. Function didn't change the value, because it operates on the copy.
b) passing by reference:
function my(&$value) { $value++; echo $value." is "; return $value;}
echo my(0);//error - cannot pass values, only variables
$a = 1;
$b = my($a);
//echo 2 is
//a = 2, b = 2 - value of a changed by function.
c) passing by global (not reccommended)
function my() {global $value; $value++; echo $value." is "; return $value;}
echo my();//echo 1 is 1 - was not defined, therefore 0
$a = 1;
$b = my($a);
//echo 2 is
//a = 2, b = 2 - value of a changed by function.

4. NO variables are passed to another server, or to the html includes:
include("http://www.external.server.com");//no variables passed
<img src="file.php">No variables passed to file.php

To pass variables to such you need to use get:
<img src="file.php?id=1&a=<?php echo $a;?>

in file.php -> $id = (int)$_GET["id"]; $a = $_GET["a"];
Hope this helps
Michal Cibor