簡體   English   中英

在腳本之間包含來自另一個文件的php代碼

[英]Including php code from another file inbetween your script

我想要做的是相當簡單,但似乎不起作用。

在這個主頁面中,我試圖從另一個頁面插入一段代碼。

<?php

include_once $_SERVER['DOCUMENT_ROOT'].'\attach\common.php';

$clear = False;

echo $code;

if($clear){
    echo 'Clear was cleared';
}

?>

這是常見的.php

<?php

$code = "if(isset($_GET['n'],$_GET['c'])) {";
$code .="   echo 'Name and Country were set <br />';";
$code .="   $clear = True;";
$code .="} else {";
$code .="   echo 'An error occured <br />';";
$code .="}";

?>

我怎么能在PHP中這樣做?

更新

<?php
include_once $_SERVER['DOCUMENT_ROOT'].'\attach\common.php';
$clear = False;
d();
if($clear){
    echo 'Clear was cleared';
}
?>

<?php

function d() {
    if(isset($_GET['n'],$_GET['c'])) {
        echo 'Name and Country were set';
        $clear = True;
    } else {
        echo 'An error occured <br />';
    }
}
?>

請小心使用eval()因為這是達到你想要的非常危險的方式。

為什么你不能把代碼放在common.php而不是把它作為變量中的字符串?

即common.php:

<?php
if(isset($_GET['n'],$_GET['c'])) {
   echo 'Name and Country were set <br />';
   $clear = true;
} else {
   echo 'An error occured <br />';
}
?>

-

如果你真的需要這樣做:

使用eval()來解析$code 那是因為你的代碼是一個字符串變量而不是實際的代碼。

<?php

include_once $_SERVER['DOCUMENT_ROOT'].'\attach\common.php';

$clear = False;

eval($code);

if($clear){
    echo 'Clear was cleared';
}

?>

您不會將代碼放在字符串中以將其包含在另一個文件中,只需按原樣放置代碼即可。

common.php應該是

<?php

if(isset($_GET['n']) && isset($_GET['c'])) {
echo 'Name and Country were set <br />';
$clear = True;
} else {
   echo 'An error occured <br />';
}

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM