kazsil Posted December 6, 2007 Share Posted December 6, 2007 Okay so here it goes.... I have used this logic all in one page...: <?php $ArrayList = array("_GET", "_POST", "_SESSION"); foreach($ArrayList as $gblArray){ $keys = array_keys($$gblArray); foreach($keys as $key){ $$key = trim(${$gblArray}[$key]); //$value = ${$key}; //print "Key- $key ". $value ."<BR>"; } //end foreach get session and from form variables and stuff into their names }// end foreach array list ?> That works great when it is on the same page. But I need to make an XML file and FTP it, so I wanted to make an include file so it would be more orginzed and just call it as a function. So one payment.php page I am doing this: <?php include "/home/www/carcheckup.com/sendFTP.inc"; // this has MakeXML function $ArrayList = array("_GET", "_POST", "_SESSION"); session_register("ArrayList2"); $_SESSION['ArrayList2']=array("_GET", "_POST", "_SESSION"); MakeXML(); // calls the function from my include file which ?> then on sendFTP.inc file I am doing this..: <?php Function MakeXML(){ global $FName; print "in analytics<BR>"; print "First name $FName<BR>"; // just a test to see if var are coming over foreach($_SESSION['ArrayList2'] as $gblArray){ $keys = array_keys($$gblArray); foreach($keys as $key){ $$key = trim(${$gblArray}[$key]); $value = ${$key}; print "Key- $key ". $value ."<BR>"; print "HELLO<BR>"; // just a test }//end foreach get session and from form variables and stuff into their names }// end foreach array list }// end function MakeXML ?> When i execute it... i get these errors: Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/www/carcheckup.com/sendFTP.inc on line 11 Warning: Invalid argument supplied for foreach() in /home/www/carcheckup.com/sendFTP.inc on line 12 Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/www/carcheckup.com/sendFTP.inc on line 11 Warning: Invalid argument supplied for foreach() in /home/www/carcheckup.com/sendFTP.inc on line 12 Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/www/carcheckup.com/sendFTP.inc on line 11 Warning: Invalid argument supplied for foreach() in /home/www/carcheckup.com/sendFTP.inc on line 12 So it works great when it is all on one page, but for some reason it is not working when i break it up in an include.....What am i doing wrong? Thanks for your help! Jen Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted December 6, 2007 Share Posted December 6, 2007 You need to read about variable scope. You can't call a variable that was declared outside a function unless it was declared global inside the function. http://www.phpfreaks.com/tutorials/26/0.php While your technique for dealing with the _POST _GET etc. is clean, it is also very insecure. Make sure you sanitize incoming variables before using them in your code. Quote Link to comment Share on other sites More sharing options...
kazsil Posted December 6, 2007 Author Share Posted December 6, 2007 Thanks for your respnse DyslexicDog! I guess then I still dont understand... I have tried it every way that I cannot figure it out!!!! I read that article, but when I try why they say, it still doesn't work. Am I not storing the array right. I guess I just dont know. ??? How do i do that? Thanks agian. Jen Quote Link to comment Share on other sites More sharing options...
didijeeeke Posted December 6, 2007 Share Posted December 6, 2007 wel $_Session is a supperglobal so it means it exists in any programming scoop. So that will not be the problem here. Mayby use a other way to pass the var Function MakeXML($array){ global $FName; print "in analytics<BR>"; print "First name $FName<BR>"; // just a test to see if var are coming over foreach($array as $gblArray){ $keys = array_keys($$gblArray); foreach($keys as $key){ $$key = trim(${$gblArray}[$key]); $value = ${$key}; print "Key- $key ". $value ."<BR>"; print "HELLO<BR>"; // just a test }//end foreach get session and from form variables and stuff into their names }// end foreach array list }// end function MakeXML <?php include "/home/www/carcheckup.com/sendFTP.inc"; // this has MakeXML function $ArrayList = array("_GET", "_POST", "_SESSION"); session_register("ArrayList2"); $array =array("_GET", "_POST", "_SESSION"); MakeXML($array); // calls the function from my include file which ?> (for example no working code) Quote Link to comment Share on other sites More sharing options...
kazsil Posted December 6, 2007 Author Share Posted December 6, 2007 I solved my issue. Just incase anyone wanted to use this. It is an awesome way to get ALL your variables that have been Posted from a form, or stored in session variables and get them into their variable names. Here's how it works. This sets up an array of all your varables so u can use it in a function ( a sneaky way of getting it into a function without having to GLOBAL til your fingers bleed.) <?php // make sure to do validation testing before getting here $ArrayList1 = array("_GET", "_POST", "_SESSION"); foreach($ArrayList1 as $gblArray){ $keys = array_keys($$gblArray); foreach($keys as $key){ $title =$key; // this is the Var name from your form or session needed for the array $$key = trim(${$gblArray}[$key]); //sets the above variable with its value $value = ${$key}; //print "Key- $key ". $value ."<BR>"; //for testing $ArrayVars["$title"]= "$value" ; //add values to the array }//end foreach get session and from form variables and stuff into their names }// end foreach array list ?> then call your function (this is MakeXML2) and use this code: <?php Function MakeXML2(){ global $ArrayVars; //gets the Array made above foreach($ArrayVars as $Title=>$val){ // echo "title $Title key $val <BR>"; // for testing $$Title = "$val"; // this sets the var name to the title and sets the value of that var to val } //end foreach //PRINT "Last name: $LName <BR>"; // for testing }// end function MakeXML // now you can use any of the var you need ?> Hope it helps someone out... Saved me a BUNCH of time once I got it working! Good Luck! Jen Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted December 6, 2007 Share Posted December 6, 2007 Like I said it's better to sanitize your user supplied incoming variables before you start using them. The other thing that should be noted here is , you're creating copies of variables that are already superglobals... While it might be great for organization, it's not so nice on memory. Quote Link to comment Share on other sites More sharing options...
kazsil Posted December 7, 2007 Author Share Posted December 7, 2007 DyslexicDog , So are you saying its a waste to store the Session Var in the array? Is that what you mean? Thanks for your input! Jen Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted December 7, 2007 Share Posted December 7, 2007 DyslexicDog , So are you saying its a waste to store the Session Var in the array? Is that what you mean? Thanks for your input! Jen In a word... yes. You already have direct access to all the arrays you are combining into a single array. The arrays you are conjoining are superglobals, meaning you can access them from anywhere at anytime. By copying them into another array you are doubling their memory usage, BAD!!! This probably wouldn't cause you much issue on a small site, but it will scale very poorly. IMO You're better off to just use the variables directly. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.