Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
Old 06-26-2009, 07:06 AM   #1 (permalink)
Meeow!
 
Costin Trifan's Avatar
 
Join Date: 04-13-07
Location: Romania
Posts: 3,235
iTrader: 0 / 0%
Latest Blog:
None

Costin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest order
any way of filtering $GLOBALS ?

I'm working on a big project I can't give you too much details about this right now, sorry, but I need a way of filtering the $GLOBALS variable.

The current architecture is a template based one so I need a way to extract only some of the global variables into some of my scripts.

Although it might seem easy I really have a big problem in getting it to work(maybe I have too much on my head right now and I obviously can't think clearly enough) so I need some help.

Description:

I have a php file, let's call it < main.php > which will hold some of the configuration settings and import some other scripts as well. Basically this would be the website's main file. It might look like this:
PHP Code:
<?php
    
// main.php

// Globals

    
$server '';
    
$db '';
    
$user '';
    
$password '';
    
$cs = array( 'server' => $server'db' => $db /* etc */);

    
$sql = new MySqlWrapper();
    
    require 
'lang.php';
    
$lang = array(
         
'welcome_msg' => 'Welcome to our website!'
        
,'home_page_title' => 'Welcome!'
    
);

    require 
'tpl.php';
    
$tpl = new TemplateClass()

    require 
'other_class.php';
    
$class = new OtherClass();

    
/* etc ... */
?>
Then I need to access some of these variables in other external pages, an example would be the < functions.php > file that will be included in some of the website's pages:

PHP Code:
<?php
    
// functions.php

    
$tpl->set_var('welcome_msg'$lang['welcome_msg']);
    
$tpl->set_var('home_page_title'$lang['home_page_title']);

    
// ^^ breaks the script. $tpl and $lang variables are not visible on this page
    // an alternative would be extract(GLOBALS); but that would bring on ALL globals here and I don't need them all
?>
And finally, an external page would look something like this:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>{home_page_title}</title> </head> <body> <h1>{welcome_msg}</h1> </body> </html>
Now, my problem is accessing the global variables set in main.php file in the functions.php file, so I need to filter the $GLOBALS variable and I tried a few tricks but I couldn't get what I wanted.

The point of this is that the $GLOBALS variable is holding everything so I need to have the function exclude_globals() to filter them out,

PHP Code:
/**
* Filter $GLOBALS
*
* @param string|array $exclude    The variable or the array of variables to exclude
* @return array
*/
function exclude_globals$exclude NULL )
{
    
# Will hold the $GLOBALS filtered
    
$new = array();

    
# the default list of variables to exclude from $GLOBALS
    
$default_excludes = array('_POST''_GET''_COOKIE''_FILES''_SESSION''_SERVER');

    if (
is_array($exclude))
    {
        
$excludes array_merge($default_excludes$exclude);
    }
    else {
        
array_push($default_excludes$exclude);
    }

    
// TODO:
    //....



    
return $new;

function that I would call in my functions.php file
PHP Code:
<?php
    
// functions.php

    
$globals exclude_globals();
    
extract($globals);

    
$tpl->set_var('welcome_msg'$lang['welcome_msg']);
    
$tpl->set_var('home_page_title'$lang['home_page_title']);
?>
and give me access to the global variables set in the other included files.

Can anyone help?


__________________
...to be continued
Costin Trifan is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 07:58 AM   #2 (permalink)
v7n Mentor
 
Izzmo's Avatar
 
Join Date: 11-01-03
Location: Kansas City
Posts: 1,330
iTrader: 0 / 0%
Latest Blog:
Starting p90x today

Izzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web pro
Send a message via ICQ to Izzmo Send a message via AIM to Izzmo Send a message via MSN to Izzmo Send a message via Yahoo to Izzmo
I have to say, this is pretty confusing, and I read over your post 3 times!

So, let me get this straight, you want to extract the globals because another file is overwriting the globals?

I have never personally ran into this problem, but I don't think this should be too difficult.

Let me know if I am right so I can get a good understanding of the problem at hand.
__________________
Izzmo
Coding Guru Extraordinaire
ZeroWeb Hosting & Design - Customizable hosting for every type of user!
Izzmo is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 08:06 AM   #3 (permalink)
Meeow!
 
Costin Trifan's Avatar
 
Join Date: 04-13-07
Location: Romania
Posts: 3,235
iTrader: 0 / 0%
Latest Blog:
None

Costin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest order
Quote:
Originally Posted by Izzmo View Post
I have to say, this is pretty confusing, and I read over your post 3 times!

So, let me get this straight, you want to extract the globals because another file is overwriting the globals?

I have never personally ran into this problem, but I don't think this should be too difficult.

Let me know if I am right so I can get a good understanding of the problem at hand.

No. I just want to exclude some of the variables existent in the $GLOBALS, such as _GET, _POST, _SESSION, etc... and other vars that I specified(using that filter function)
__________________
...to be continued
Costin Trifan is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 09:21 AM   #4 (permalink)
v7n Mentor
 
Izzmo's Avatar
 
Join Date: 11-01-03
Location: Kansas City
Posts: 1,330
iTrader: 0 / 0%
Latest Blog:
Starting p90x today

Izzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web proIzzmo is a highly respected web pro
Send a message via ICQ to Izzmo Send a message via AIM to Izzmo Send a message via MSN to Izzmo Send a message via Yahoo to Izzmo
Hmm, I don't know if I follow you. I know.. annoying, but this is a confusing dilemma!
__________________
Izzmo
Coding Guru Extraordinaire
ZeroWeb Hosting & Design - Customizable hosting for every type of user!
Izzmo is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 09:37 AM   #5 (permalink)
Meeow!
 
Costin Trifan's Avatar
 
Join Date: 04-13-07
Location: Romania
Posts: 3,235
iTrader: 0 / 0%
Latest Blog:
None

Costin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest order
np.

just imagine you have a file where you declare many variables. They will all be defined in the global scope, now you want to access those variables in other inner pages so you would normally use extract($GLOBALS); which will give you full access to those vars.

But my problem with that is that I don't want all those variables, only some of them, so I need a function that will filter the $GLOBALS variable and return a new filtered array:
PHP Code:
$globals $GLOBALS;

// to do: filter globals
$filtered_globals exclude_globals( array('_POST''_GET''_SESSION''cs''other_object') );

extract($filtered_globals); 
and now I'll have access to al global variables except the ones I excluded using the function.

Ex: if I'd print this out now:
PHP Code:
 var_dump($cs); // the connection string array 
it should throw an error, because the $cs array was excluded.

see what I mean now?
__________________
...to be continued
Costin Trifan is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 09:41 AM   #6 (permalink)
Meeow!
 
Costin Trifan's Avatar
 
Join Date: 04-13-07
Location: Romania
Posts: 3,235
iTrader: 0 / 0%
Latest Blog:
None

Costin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest order
One more thing: I need a way of doing this without using the unset function because it's destroying the session state
__________________
...to be continued
Costin Trifan is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 10:19 AM   #7 (permalink)
Moderator
 
Hades's Avatar
 
Join Date: 01-23-07
Location: Buenos Aires
Posts: 1,064
iTrader: 0 / 0%
Hades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest order
I would roll through the globals checking if your exclusion variables are there and creating a temp. array, and after that replace $GLOBALS with the temp.

PHP Code:
function exclude_globals($exclude NULL){
    
    
//default exclusions
    
$default_excludes = array("_POST","_GET");
    
    
//roll through $GLOBALS
    
$GLOBALS_temp=array();
    foreach(
$GLOBALS as $GLOBALS_name=>$GLOBALS_content){
        
        
//check if vars are in global
        
if (!(in_array($GLOBALS_name,$exclude) || in_array($GLOBALS_name,$default_excludes)) ){
            
            
//assign to temp array
            
$GLOBALS_temp[$GLOBALS_name]=$GLOBALS_content;
        }
        
    
//replace globals with temp array
    
$GLOBALS $GLOBALS_temp;
    }
    
}

//call the function
exclude_globals(array("_FILES"));

//dump GLOBALS
echo("<h2>glob</h2>");
echo(
"<pre>");
print_r($GLOBALS);
echo(
"</pre>"); 
That takes away those variables from $GLOBALS but I'm not sure if that's exactly what you want
__________________
Hades,
Ancient god, King of the Nether World, and Guardian of the Dead.
...and on my free time I'm also a web developer, contact me if you need one!
Hades is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 10:31 AM   #8 (permalink)
Meeow!
 
Costin Trifan's Avatar
 
Join Date: 04-13-07
Location: Romania
Posts: 3,235
iTrader: 0 / 0%
Latest Blog:
None

Costin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest order
fortunately, that's what I'm after;
unfortunately, your example doesn't work

Edit:
it still brings everything(all globals, like they're not excluded at all) on that page...



__________________
...to be continued
Costin Trifan is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 10:54 AM   #9 (permalink)
Member
 
danf's Avatar
 
Join Date: 04-14-09
Posts: 34
iTrader: 0 / 0%
Latest Blog:
None

danf is liked by many
I never ran into this problem either and I'm not sure this is helpful, but I'll try anyway..

If the functions.php file is something you do not control, then you obviously can't rely on someone else excluding globals no matter how you implement the function. In that case, maybe you could (and should?) unset your session state before calling out to those functions and then restore the session state afterwards.

If you do control functions.php and the entire application, it's really a good idea to limit the use of global variables as much as possible. What's the expression? Motherhood and apple pie? Not helpful, I know. Sorry. Maybe you could take the same approach and save your session state, unset, call the functions.php functions, and then restore state.
danf is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 10:59 AM   #10 (permalink)
Meeow!
 
Costin Trifan's Avatar
 
Join Date: 04-13-07
Location: Romania
Posts: 3,235
iTrader: 0 / 0%
Latest Blog:
None

Costin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest order
@danf:
whether you want or not you would still have global variables in your code. This is not something you can avoid(unless you use only static classes)

by simple declaring $x = new ClassName() you just got a global variable $x.

and yes, I'm in control of all app.

Edit:
resetting the session state is not a good idea, because when I won't be able to re-enable a user's session. I store values in session that I cannot just re-enable..
__________________
...to be continued
Costin Trifan is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 11:07 AM   #11 (permalink)
Moderator
 
Hades's Avatar
 
Join Date: 01-23-07
Location: Buenos Aires
Posts: 1,064
iTrader: 0 / 0%
Hades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest order
Quote:
Originally Posted by Costin Trifan View Post
fortunately, that's what I'm after;
unfortunately, your example doesn't work

Edit:
it still brings everything(all globals, like they're not excluded at all) on that page...



I tested it here with PHP 5.2 with register globals off.
It did remove all those variables from the $GLOBALS (they still appeared on the recursion array though).
__________________
Hades,
Ancient god, King of the Nether World, and Guardian of the Dead.
...and on my free time I'm also a web developer, contact me if you need one!
Hades is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 11:21 AM   #12 (permalink)
Moderator
 
Hades's Avatar
 
Join Date: 01-23-07
Location: Buenos Aires
Posts: 1,064
iTrader: 0 / 0%
Hades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest orderHades is a web professional of the highest order
what's your setup? I have quite a few apaches/php running here
__________________
Hades,
Ancient god, King of the Nether World, and Guardian of the Dead.
...and on my free time I'm also a web developer, contact me if you need one!
Hades is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-26-2009, 11:42 AM   #13 (permalink)
Meeow!
 
Costin Trifan's Avatar
 
Join Date: 04-13-07
Location: Romania
Posts: 3,235
iTrader: 0 / 0%
Latest Blog:
None

Costin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest order
@hades:

the default php 5.2.9 + Apache 2 on win XP. I'm currently working on it locally

you were right about the globals displayed in the recursion array; I saw it too, but when I said it doesn't work I wasn't referring to that; I tried to pass some variables which were holding arrays and they were still displayed.
__________________
...to be continued
Costin Trifan is offline  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
IP Block or Adsense Filtering Grant Draper Webmaster Revenue 0 09-28-2008 09:58 AM
Google Filtering Links theChronic Marketing Forum 6 02-18-2007 12:29 AM
Google is filtering doorway pages. newsguy Google Forum 3 11-19-2004 04:14 PM


Sponsor Links
Get exposure! Contextual Links V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 03:47 AM.
© Copyright 2008 V7 Inc
Powered by vBulletin
Copyright © 2000-2009 Jelsoft Enterprises Limited.


Search Engine Optimization by vBSEO 3.3.0 ©2009, Crawlability, Inc.