Hi there, I want to grab a page from my site that's PHP, however, I don't want it to display the PHP...
How can I do this?

Dani AI

Generated

Short summary and what's going on: the behavior you saw came down to two different ways of getting a file. Reading a file from disk returns the raw source; executing or requesting the file through the webserver returns the PHP-processed output. 's output-buffering idea is the right way to capture executed output within the same process, simplified it by using include(), clarified the file-read semantics, and pointed out that fetching the full HTTP URL gives the server-parsed HTML.

Which approach to use and why:

  • Use include/require (or the buffering technique) when you control the code and want the PHP executed in-process — it is fastest and keeps variables/context available. Be careful with relative paths and side effects from included scripts.
  • Use an HTTP fetch (file_get_contents with a URL or cURL) when you need the page exactly as the webserver would serve it (routing, rewrite rules, or headers matter). That goes through the webserver, so it’s slower, requires allow_url_fopen enabled for URL fopen wrappers, or you can use cURL instead, and can cause recursion if you request the same script from itself.

Security and best practices:

  • Never echo raw server-side source to users. Don’t let user input control filenames to include — use a whitelist or map IDs to filenames.
  • Prefer separating logic and presentation: have functions or small endpoints that return data (arrays/JSON) and consume those, rather than including templates with side effects.
  • If you ever see PHP code delivered to a browser (not just read from disk), check server config — PHP not being handled by the handler is a serious misconfiguration.

Quick troubleshooting/checklist:

  • Do you want source or rendered HTML? (source → read file; rendered → execute/include or HTTP GET)
  • Is allow_url_fopen off? Use cURL.
  • Watch for recursion/performance when calling your own URLs.
  • If you need a server-rendered fetch, a minimal cURL GET is a reliable option, for example:
$ch = curl_init('http://localhost/project/data.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);
echo $html;

This preserves what the webserver outputs while avoiding fopen-URL-wrapper issues.

Recommended Answers

All 13 Replies

Below is example to get Google's content.

<?php
$homepage = file_get_contents('http://www.google.com/');
echo $homepage;
?>

Is this what u want?

Below is example to get Google's content.

<?php
$homepage = file_get_contents('http://www.google.com/');
echo $homepage;
?>

Is this what u want?

Hmm.. Not really, it's a file located in my site.. Something like

echo file_get_contents("./data.php");

But it displays the PHP code in data.php too =\

echo file_get_contents("./data.php");

code will not display any php tag. It shows HTML output of file data.php.
You want to display php tag also?

echo file_get_contents("./data.php");

code will not display any php tag. It shows HTML output of file data.php.
You want to display php tag also?

Interesting, I made a page named data.php that contained user data, it outputted it as text. However, when I try to use echo file_get_contents("./data.php"); It also includes the PHP, not just the text it outputs.

how could it display php tags? its a scripting language and when you echo it it automatically executed.
Post your exact output if issue is still not solved.

how could it display php tags? its a scripting language and when you echo it it automatically executed.
Post your exact output if issue is still not solved.

Exact Example:

index.php
<?php echo file_get_contents("./lol.php"); ?>

lol.php
<?php echo "lol"; ?>

index.php Outputs <?php echo "lol"; ?> instead of lol.

how could it display php tags? its a scripting language and when you echo it it automatically executed.
Post your exact output if issue is still not solved.

-vib
That is not true. When you use something like file_get_content() it does not parse/execute what it reads in. If you included the file then yes it would execute any php in that file.

-Gigs
This appears to me like you're trying to use it for some kind of template system? Where your output has a series of echo statements and you've already set some variables that should be replaced.

ob_start();
include ('lol.php');
$content = ob_get_contents();
ob_end_clean();

echo $content;

That code will include lol.php which will execute any php statements within it and then capture the content to a variable which you can then use at a later time. Is this more what you're trying to accomplish?

-vib
That is not true. When you use something like file_get_content() it does not parse/execute what it reads in. If you included the file then yes it would execute any php in that file.

-Gigs
This appears to me like you're trying to use it for some kind of template system? Where your output has a series of echo statements and you've already set some variables that should be replaced.

ob_start();
include ('lol.php');
$content = ob_get_contents();
ob_end_clean();

echo $content;

That code will include lol.php which will execute any php statements within it and then capture the content to a variable which you can then use at a later time. Is this more what you're trying to accomplish?

The function will not internally parse it.
But lets say i have one php page :
And i use file_get_contents('') then html source will be returned by file_get_contents function.

Simply we can have 'View Source' type of functionality.

Thanks guys! mschroeder's solution seemed to fix it :)

The function will not internally parse it.
But lets say i have one php page :
And i use file_get_contents('') then html source will be returned by file_get_contents function.

Simply we can have 'View Source' type of functionality.

From the PHP.net documentation:
"file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance."

If the file is stored locally, file_get_contents() basically does the following:

$fp = fopen($file, "r");
$contents = fread($fp, filesize($file));
fclose($fp);

It will not execute any PHP contained within the file. For any PHP code within the file to execute you will have to use output buffering and include the file, as mschroeder suggested, or grab the file with cURL (which is similar to grabbing the source from a browser). By far, including the file is faster since it doesn't require any socket connections like cURL does.

From the PHP.net documentation:
"file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance."

If the file is stored locally, file_get_contents() basically does the following:

$fp = fopen($file, "r");
$contents = fread($fp, filesize($file));
fclose($fp);

It will not execute any PHP contained within the file. For any PHP code within the file to execute you will have to use output buffering and include the file, as mschroeder suggested, or grab the file with cURL (which is similar to grabbing the source from a browser). By far, including the file is faster since it doesn't require any socket connections like cURL does.

Thanks for explanation :)

I found an EASIER way to do it! :D include("lol.php") XD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.