Round-a-bout page loading

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Round-a-bout page loading

Post by Rooster242 »

i'm having some trouble and was wondering if anyone could help. here's some sudo code to describe what i'm doing. i'm trying to figure out why i only get the first $_GET parameter from myfile.php. (MakeRequest() is a simple ajax function like you find all over the web).

index.php:

Code: Select all

<script type="text/javascript">
  callback = function( response ) { document.getElementByID( "someDiv" ).innerHTML = response; };
  MakeRequest( "getfile.php?file=thefile.php?name=jim&age=35", callback );
</script>
getfile.php:

Code: Select all

$text = file_get_contents( $_GET[ 'file' ] );

  // Evaluate each php block
  while( ( $blockStart = strpos( $text, "<?php" ) ) !== false )
  {
    $blockEnd = strpos( $text, "?>", $blockStart ) + 1;
    if( $blockEnd !== false )
    {
      $evalText = substr( $text, $blockStart + 5, ( $blockEnd - $blockStart ) - 6 );
      ob_start();
      eval( $evalText );
      $newText = ob_get_contents();
      ob_end_clean();
      $text = substr_replace( $text, $newText, $blockStart, $blockEnd - $blockStart + 1 );
    }
    else
    {
      $text = "Missing closing element for php block";
      break;
    }
  }
  echo( $text );
myfile.php:

Code: Select all

echo( count( $_GET ) . "<br />" );
  echo( $_GET[ 'name' ] . "<br />" );
  echo( $_GET[ 'age' ] . "<br />" );
output (should be 2 params, missing age):

Code: Select all

1
  jim
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Change :
MakeRequest( "getfile.php?file=thefile.php?name=jim&age=35", callback );
To:
MakeRequest( "getfile.php?file=thefile.php&name=jim&age=35", callback );
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I am still amazed that I don't see those things as quickly as I should. That was a good catch nickvd.
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

nickvd wrote:Change :
MakeRequest( "getfile.php?file=thefile.php?name=jim&age=35", callback );
To:
MakeRequest( "getfile.php?file=thefile.php&name=jim&age=35", callback );
thanks for the suggestion but it didn't work, then the output is just 0.
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

i think you guys are missing what i'm trying to do. i'm trying to load myfile.php via getfile.php via an ajax call from index.php. so i pass the url with parameters for index.php as a parameter to getfile.php. did that make sense?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

then you might want to run the Javascript function escape() on it.
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

feyd wrote:then you might want to run the Javascript function escape() on it.
well, i was using encodeURI() but i tried escape() and no change, still getting just the first param. encodeURI and escape doesn't modify "getfile.php?file=thefile.php?name=jim&age=35" anyway.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Rooster242 wrote:encodeURI and escape doesn't modify "getfile.php?file=thefile.php?name=jim&age=35" anyway.
Are you still using "getfile.php?file=thefile.php?name=jim&age=35" instead of "getfile.php?file=thefile.php&name=jim&age=35" or are you trying to pass a page with query string vars as a query string var to another page?
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

Everah wrote:
Rooster242 wrote:encodeURI and escape doesn't modify "getfile.php?file=thefile.php?name=jim&age=35" anyway.
Are you still using "getfile.php?file=thefile.php?name=jim&age=35" instead of "getfile.php?file=thefile.php&name=jim&age=35" or are you trying to pass a page with query string vars as a query string var to another page?
I'm still using "getfile.php?file=thefile.php?name=jim&age=35" hence i'm trying to pass a page with query string vars as a query string var to another page. i don't understand why, when i get to myfile.php, i get the first var (name), but not the second one (age). i thought about setting up $_GET manually in getfile.php but it looks like i can't modify $_GET. [edit] That is, it looks like i can't add to $_GET [edit]
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

So, lemme get this straight... the variable that you want to pass to getfile.php is "thefile.php?name=jim&age=35" ?

and, in getfile.php, it redirects to "thefile.php?name=jim&age=35".

or, does it include() thefile.php, and thefile.php expects the get variables name=jim and age=35 to exist.

if that is the case, then you should re-read this thread and pay attention to feyd's post(s) :)

in getfile.php, it will see $_GET as having two variables, file = "thefile.php?name=jim" and age="35"

You'll need to turn thefile.php?name=jim into thefile.php&name=jim if you want getfile.php to be able to include thefile.php and still have the other variables set.
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

sorry for the confusion. i just realized i made a mistake in my orginal post... subsitute thefile.php for myfile.php in the first code block.

i totally get where your comming from nickvd and i now understand why i get the first var, but, what i'm trying to accomplish is getting the vars name and age in myfile.php. i just want getfile.php to pass them through so i can access them in myfile.php.

getfile.php's whole job is to read the contents of another php file, evaluate any php code in it (among other things), and echo the result. so what do i do if i need to pass vars to the file i'm evaluating (myfile.php)?

if i change "getfile.php?file=thefile.php?name=jim&age=35" to "getfile.php?file=thefile.php&name=jim&age=35" then sure enough, getfile.php has 3 vars, but myfile.php has 0 vars.

i know this probably seems like a silly thing to do given my sudo code above, like why don't i just include() myfile.php or just do MakeRequest( "myfile.php?name=jim&age=35", callback ); but getfile.php does more thngs to myfile.php like replacing string ids with literals from a string resource file... for example [#StrSomeString#] in myfile.php is replaced with "Some string" etc... hence the file_get_contents() call which seems to handle query string vars just fine.
--------------------------------
just as i was about to post this i thought of a solution:
if i replace the & with some other token like { then replace that token with & in getfile.php, it works!
so the code would look like:
index.php:

Code: Select all

MakeRequest( "getfile.php?file=myfile.php?name=jim{age=35", callback );
getfile.php:

Code: Select all

$file = $_GET[ file ];
$file = str_replace( '{', '&', $file );
$text = file_get_contents( $file );
// [do whatever with text]
echo( $text );
maybe not the prettiest solution but hell, it works! i'll have to be sure to pick a good token. what do you guys think, is there a better way to do this?

on another note, i just realized that file_get_contents() will evaluate all the php code in the target file so i don't need the code to look for <?php ?> blocks. strange, the php docs don't say anything about file_get_contents() doing any evaluation.

whew, sorry for the long arse post. :)
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

Rooster242 wrote: on another note, i just realized that file_get_contents() will evaluate all the php code in the target file so i don't need the code to look for <?php ?> blocks. strange, the php docs don't say anything about file_get_contents() doing any evaluation.
i take that back. if i pass a fully qualified url to file_get_contents() (http://...) then the web server evaluates the php code in the target file before it's returned by file_get_contents().
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

as feyd said, urlencode the query string passed to getfile.php i.e.

Code: Select all

$url = "getfile.php?file=".urlencode('myfile.php?name=jim&age=35');
then, in getfile.php, you can

Code: Select all

file_get_contents(urldecode($_GET['file']))
Post Reply