开发者

How can I echo php with php?

开发者 https://www.devze.com 2023-02-15 09:30 出处:网络
The website that I\'m developing uses a \"header\" and \"footer\" template. These templates are included on each page of the site. The header template opens the HTML and contains the complete head tag

The website that I'm developing uses a "header" and "footer" template. These templates are included on each page of the site. The header template opens the HTML and contains the complete head tag, and then opens the body tag. The footer template closes the body tag and then closes the HTML. The basic structure is as follows:

<?php
//Set the current page name so it can be highlighted in the menu bar
$page="Home";
//For page-specific HEAD includes
$headincludes=array("<some type of code>","<some other type of code>");
include("/assets/template/header.开发者_开发问答php");
?>
<!--START PAGE CONTENT-->
<!--END PAGE CONTENT-->
<?php
include("/assets/template/footer.php");
?>

The header template dumps the "headincludes" array into the head section of the page as so:

foreach ($headincludes as $toecho)
echo $toecho;

This method works fine for things such as javascript, but when I try to add a PHP statement, it ends up as unparsed PHP in the final page served to the browser. Is there some special method to echoing PHP with PHP?


Yes, eval() (read evil()) however you're best to reconsider your approach. Perhaps, if you're looking to include a considerable amount of PHP code, pass an array of files that could further be included:

// at the beginning of the file
$includes = array('db.php', 'config.php');


// in your header, or footer (whichever is applicable)
foreach($includes as $file){
    if(file_exists($file)){
        include $file; // or include_once
    }
}

Then, using the dynamic includes, you can manage and load additional code as necessary.


The way to execute PHP from a string is with eval(), however, it is rarely the right tool to use.

Can you place the PHP you need above $headincludes, and then add its output to $headincludes?

<?php
//Set the current page name so it can be highlighted in the menu bar
$page="Home";

include 'generators.php';

$links = generateLinks('some_arg');

//For page-specific HEAD includes
$headincludes=array("<some type of code>","<some other type of code>", $links);
include("/assets/template/header.php");
...


What are you adding to your array?

If you are setting you php like that :

$headincludes=array("$var1","$var2");

it will not work. You have to do like that:

$headincludes=array($var1,$var2);


Execute the PHP code in your main files and dump the result into $headincludes. There's no need to execute the code in the header, the result won't change.

Or would it? If you're actually trying to inject arbitrary code into other code, you're doing it wrong.


whats into $headincludes

note: use include_once


did you try string concatenate. like this

$headincludes=array("<your html code>".$variable."<more html>","<more>")


Try this:

echo eval('?>html or javascript<?');
echo eval('php');


If you try to output php file, sure you do that easily but your browser won't able to run.

So you need to:

  • Eval the php file and output the result

or

  • Create somehow html file that you'll output instead of php file.

include_once() or include() are your friends anyway, it'll (just like name reads) include the file and the file is "executed" in that very place. "once" version won't include the same file twice if it was already included before (useful if you do several includes, which in turn do another inclusions).

0

精彩评论

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

关注公众号