0

I am using ajax on a website that calls a php script that then parses another page and returns the part of the page that I want to display. I have used this technique in the past and it works well, except this time the part of the page that is being returned has some php code in it and it is being returned raw with out the php code being processed.

Here is the php script that is doing the parseing and returning the results.

require_once('simple_html_dom.php');
$dom = new simple_html_dom;

if(isset($_GET['url']))  {
    $url = $_GET['url'];
    $html = file_get_html($url);
    /*get contents*/
    $contents = $html->getElementById('content');
    echo $contents;
}

As I mentioned what is being returned in $contents is being returned raw with out the php code inside the $contents string being processed, how can I get it to process the php code before returning the $contents string?

Thank you in advance for your help.

6
  • the local file is no parsed though apache\php. this seems like an odd way to get dynamic content from one of your own files Commented Dec 30, 2012 at 5:29
  • Try loading the php file using the jQuery .load() function. That should process the file and load it correctly Commented Dec 30, 2012 at 5:29
  • if I go to the page index.php directly then yes it processes correctly, it I go to getContents.php?url=index.php which is the file that has the script above then no it does not process it is returned raw. Commented Dec 30, 2012 at 5:42
  • I will try jQuery .load, I am currently using .ajax, but I don't think that will help, since the script above is returning the string raw and it needs to be processed on the server side. Commented Dec 30, 2012 at 5:46
  • What is the contents of $url? is it a third party side? Commented Dec 30, 2012 at 6:01

1 Answer 1

1

Parsing the HTML generated by another PHP script is rather hackish. If you are developing a new site, at a minimum, you should divide your PHP script into functions and then, for AJAX requests, only call the necessary functions.

Even if you are working on an existing, complex site, keep in mind that jQuery's .load() allows you to insert a specific fragment of the server's response into the document, discarding the rest:

$('#content').load('script.php #content');

That said, what you are asking about is possible:

  • PHP's output control functions allow you to capture a script's output as a string, which you can parse using str_get_html(). To make this work, you would have to include the existing PHP script. Note that any script you include will have access to global functions and variables, superglobals, and classes.

    Warning: Make sure you check the requested filename against a whitelist of specific files to prevent file inclusion attacks. Merely blacklisting URLs and disabling allow_url_fopen are not sufficient to protect your site.

    ob_start();
    require $filename; // might have to be outside of a function
    $html = str_get_html(ob_get_clean());
    
  • Alternatively, you could have the AJAX script make its own HTTP request using either file_get_contents() or cURL (specifying an absolute URL, not a filename). Unless you configure your web server otherwise, this internal request would show up in your web server's access log as coming from 127.0.0.1 or your web server's IP address.

Sign up to request clarification or add additional context in comments.

1 Comment

you are right the .load function is a much easier way to do what I was trying to do. I didn't know you could use it to load a part of a page that way. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.