get_file_contents execute the PHP part - php

I am trying something like this:
$content = get_file_contents("abc.file");
echo $content;
The problem is that abc.file contains some HTML tags along with a php statement:
<?php require_once('a_php_file.php') ?>
How do I get the server to execute the php parts of abc.file before it being echo(ed).
Thanks!

Use include(): you might need to make some php configuration changes to let include work on remote files, but typically that's what you do.
Either that or eval(), I think.

Use include:
include 'abc.file';

Related

Is there a way to pass custom variables while loading html file in php by using htmlLoadFile function?

I am trying to pass specific variables to the html file which has extension of .php
for e.g I am trying to load main.php by using loadHTMLFile() function in another file. but when I try to add code like this
"<?php echo "hello world"?>"
and running the website and checking it's inspect elemnt this php code is commented, do you have any idea or way around of this?
I have tried using semicolon like this
'.<?php echo "hello world" ?>.'
also
'.echo "hello world".'
but it does not works
function loadHtml($loadfile){
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile($loadfile);
echo $doc->saveHtml() ;
}
and is called in index.php like this:
loadHtml("./views/html/shop/main.php");
about errors there are no errors just that I can't call the php variable or function or anythin php related to the main.php file because anything that has php tags around it is commented. Please help me if you know solution or way around this.
You can use file_get_contents() with eval()
Example: eval(file_get_contents('file.php'));
But you will either have to remove the PHP tags from the file you want to include or do like this: eval('?>'. file_get_contents('file.php'));
Why you didn't use just require("./views/html/shop/main.php") or include("./views/html/shop/main.php")?
Using eval() is not great idea, because it call script in special context, and is it unsafe, less effective to power and little hard to debug errors in future (stack trace log is not contain source of eval files).

PHP code not outputting properly

I'm working with PHP and HTML, but I have an issue popping up whenever I write some PHP code. An example of this is as follows:
<?php
echo "<h2>Hello?</h2>";
$var = 5;
echo "You have $var minutes to go.";
?>
What this ends up outputting on screen is:
Hello?"; $var = 5; echo "You have $var minutes to go."; ?>
But what I want to happen is this:
Hello? You have 5 minutes to go.
Is there something I'm forgetting to do? It doesn't seem to matter whether or not I add the HTML preamble, or if I put a tag like around the second echo line. Does anyone have any advice?
EDIT: Apparently I have failed to parse PHP correctly. This computer is new and I have XAMPP installed on it, but nothing else. Did I miss something I needed in order to use PHP?
That sounds like if the php code wasn't interpreted.
Make sure to have the code in a file with a filename ending with .php and that PHP is installed/enabled on your server.
The PHP isn't being parsed properly.
Make sure you are saving your files as .php
If you are running this locally through WAMP the make sure to use localhost in your URL because if your URL looks like this file:///C:/wamp/www/index.php then that is incorrect.
I think CakePHP uses .ctp files so that could also be an issue
You can setup Apache to interpret any file extension as PHP
make php and html different.so things become much easier.
try like this:
<h2>Hello?</h2>
<?php
$var = 5;
?>
You have <?php echo $var;?> minutes to go.

search-and-replace on PHP code before it is executed

Is there a way to perform search and replace on PHP code before it is interpreted by PHP engine ?
Desired timeline:
PHP code is <?php echo("hello"); ?>.
Search and replace operation is hello → good bye
PHP code is now <?php echo("good bye"); ?>.
PHP engine interprets the code (output is good bye).
It is possible to manipulate the output of the PHP engine, using ob_start or even mod_substitute as an output filter of Apache. However, is it possible to manipulate the input of the PHP engine (PHP code, request, etc)?
Edit:
I'm working with thousands of PHP files and I don't want them to be modified. I would like to replace host1 with host2 in these files, since the files were copied from host1 and they have to be executed on host2. Indeed, several tests are made on the host name.
You could use a php script that opens a .php file, makes the necessary replacement, then includes that file.
Request is unusual)
Looks stupid. But if you really need it try this..
For example you need to change file test.php before execute it.
Try following steps (in index.php for example):
1) Open test.php for reading and get its content.
2) Replace everything you want.
3) Save changed text as test2.php
4) Include test2.php.
EDIT
Better thin why you need it? Try using variables.
It will help)
You should study template engines like smarty and various forms of prefilters.
Modifying code like this isn't good idea, however to answer your question... You may load original code, preprocess it and store in new file:
$contents = file_get_contents( 'original.php');
// ...
file_put_contents( 'new.php', $contents);
include( 'new.php')
But I don't see any valid use of this...
You also may possibly use eval() but every time you do that a kitty dies (no really, it's dangerous function and there's really just a few valid uses of it).

php block start with <?php= instead of <?php

Hi All
I download a free chat script that in it's php files all of php code blocks start with <?php= instead of <?php or <? for print variables and constants that causes some problems and show error messages .
i want to know that how to solve this problem for php script work correctly
in your server's php config (php.ini file)
add
short_open_tag = On
At my understanding this <?php= is wrong..
Your currently problem is not the short open tags <?, but this code
<?=
means you are doing exactly this
<?php echo $someVar;
so all you have to do is change for the correct syntax.

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 here
<?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!

Categories