Closed Thread Icon

Topic awaiting preservation: Scoping included pages (PHP) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12300" title="Pages that link to Topic awaiting preservation: Scoping included pages (PHP)" rel="nofollow" >Topic awaiting preservation: Scoping included pages (PHP)\

 
Author Thread
bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-05-2002 23:19

SO I've checked the online doc's and haven't found an answer to this so I'm hoping someone here can help.

I'm writing a blog app in PHP and I'm needing to pass some information in the query string that will point to included directories. This is inheirently an insecure way to do this I know. So I wrote a function that would validate the variable stuff and then include the correct page if everything checks out. The issue is that if you use an include within a function then the incuded PHP is doesnot have access to variables outside the scope of the function.

This makes it difficult to use. Anyone know a simple way to call an include inside a function but make it retain the "Global" scope?



.:[ Never resist a perfect moment ]:.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 07-06-2002 08:42

Instead of using a function, why don't you simply put the same code somewhere with the rest of main code. If you still want to use a function, use it only for validation, including should be done outside the function...

Example:

/* some code here */

function validate_query($query)
{
&nbsp;&nbsp;&nbsp;&nbsp;return preg_match(...whatever...);
}

/* some other code here */

if (validate_query($query))
{
&nbsp;&nbsp;&nbsp;&nbsp;include($query);
}

/* some other code here */


lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 07-06-2002 08:51

Good question!
you made me do a test and conclude that include() when called from inside a function, its functions are avail/visible to the rest of the program and not only to the function that called it.
But the functions declared in that include file are only avail to the function that called the include!...
?

make those files for testing:


i2.php:

code:
<?
<?
error_reporting(E_ALL);

function i_2()
{
include('i1.php');
echo "test=$test from inside i_2()\n";
}

i_2();
inc_1();
echo $test;
?>?>



and i1.php

code:
<?
$test = '<test>';
function inc_1()
{
echo "inc_1\n";
}
?>



now run i2.php and deduce...

anyway, but for your question, it is a bit unclear, did you look at $GLOBAL[] ?

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 07-06-2002 09:41

quickest fix: Declare the variables you want to have in the includes global in the function that includes the includes.

like
function foo()
{
global $bar;
do_something;
}

The PhP Manual is your friend!

so long,

Tyberius Prime

« BackwardsOnwards »

Show Forum Drop Down Menu