开发者

Using file_get_contents() on data files - PHP code not wanted

With my data files I use with sites I usually include some PHP code in them to prevent them being directly accessed, such as below..

<?php

if (defined("VALID")) {

?>

    html code h开发者_JS百科ere

<?php

} else {

    die('Restricted Access.');

}

?>

Now this works fine when I do a simple include..... however I am using one of these files to do some replacements in & hence need to make use of file_get_contents(); however when using this, not only do I get the HTML code, I obviously also get the PHP code returned with it..... this ends up going in the source, which I do NOT want.

Is there any way around this? Perhaps stripping the PHP code? Any better ways/suggestions?


If you want to make replacements on an output of a script try using output buffering.

Instead of file_get_contents('your-php-script.php') do this:

ob_start();
include('your-php-script.php');
$contents = ob_get_clean();

// do your replacements on a $contents


echo preg_replace("~<\?php(.*?)\?>~", "", $contents);

This should work to erase the PHP code in the file.


Why dont you use a hashed string in a session cookie to check it? I think its the best solution. So add to the cookie a hashed value, then check for that value on the file you need to check if its valid and voila!

Hope it helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜