php 使用 php-content 打印文件内容

发布于 2024-11-27 17:39:07 字数 1575 浏览 0 评论 0原文

在使用搜索命令“php fread php code”和 php fopen php code”检查 fread 和 fopen 但没有成功后,我现在开始自己问这个问题。(超过 300 页的问题有点难以挖掘) 我有一个页面,

我从外部文件获取内容,其中包含通过 url(例如?links=home)发送请求的链接,该链接是从另一个文件读取的。一个数组和找到正确的文件就可以了,但棘手的部分是: 其中一个文件包含一些 php 代码字符串,它们不会完成其工作,但只会在视图源代码中徘徊。是的,您可以在源代码中看到命令,但它不会执行我要求的任何操作。没有一丝回声。

这里有一些代码可以更好地解释事情。

获取 url 命令的代码:

<?php
function load_pages() {
    if ($_GET['link'] != NULL) {
        $link = $_GET['link'];
        $links = array("hem" => "hem.php", "about" => "about.php", "blogg" => "blogg.php", "kontakta" => "kontakta.php");
        foreach ($links as $key => $value) {
            if ($key == $link) {
                $file = "links/" . $value;
                $fh = fopen($file, "r") or exit("Unable to open the file.");
                $fileContent = fread($fh, filesize($file));
                fclose($fh);
                echo $fileContent;
            }
        }
    } else {
        $file = "links/hem.php";
        $fh = fopen($file, "r") or exit("Unable to open the file.");
        $fileContent = fread($fh, filesize($file));
        fclose($fh);
        echo $fileContent;
    }
}
?>

获取我要加载的页面的命令的文件:

<?php
include ("../include/functions.php");
connect();
?>

<h1>Blogg</h1>
<?php
if ($_GET['id'] == NULL) {
    blogg_content();
} else {
    blogg_link();
}
?>

<div id="blogg_menu">
    <?php blogg_menu(); ?>
</div>

出来的是: Blogg 这根本不起作用,那么我可以改变什么来让它给我博客内容之类的东西呢? (该页面是瑞典语,只是为了避免任何有关“Blogg”的拼写错误。)

After checking for both fread and fopen with the search-command "php fread php code" and php fopen php code" without success I'm now turning to asking the question myself. (Over 300 pages with questions were a bit to steep to dig around in.)

I have a page where I get the content from external files. I got the index.php with the links which sends requests through the url (?links=home, for example) that is read from another file that looks through an array and finds the right file. All that works! But here is the tricky part:
On of the files includes a few strings of php-codes that won't do it's job but just hangs around in the view-source. Yes, you can see the commands in the source:code, but it won't anything I request. Not a single echo.

Here is some code that might explain things even better.

The code that gets the url-command:

<?php
function load_pages() {
    if ($_GET['link'] != NULL) {
        $link = $_GET['link'];
        $links = array("hem" => "hem.php", "about" => "about.php", "blogg" => "blogg.php", "kontakta" => "kontakta.php");
        foreach ($links as $key => $value) {
            if ($key == $link) {
                $file = "links/" . $value;
                $fh = fopen($file, "r") or exit("Unable to open the file.");
                $fileContent = fread($fh, filesize($file));
                fclose($fh);
                echo $fileContent;
            }
        }
    } else {
        $file = "links/hem.php";
        $fh = fopen($file, "r") or exit("Unable to open the file.");
        $fileContent = fread($fh, filesize($file));
        fclose($fh);
        echo $fileContent;
    }
}
?>

The file that gets the command for the page I want to load:

<?php
include ("../include/functions.php");
connect();
?>

<h1>Blogg</h1>
<?php
if ($_GET['id'] == NULL) {
    blogg_content();
} else {
    blogg_link();
}
?>

<div id="blogg_menu">
    <?php blogg_menu(); ?>
</div>

What comes out is: Blogg
That just doesn't do the trick, so what might I change to make it give me the blog-content and such? (The page is on Swedish, just to disclaim any typos about "Blogg".)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好听的两个字的网名 2024-12-04 17:39:07

如果这些文件包含您想要执行的 PHP 代码,您需要 includerequire 它们,而不是回显原始数据。

请参阅PHP 文档了解如何包含代码文件。

编辑
从你的描述中很难看出,但如果有的话,你必须做类似的事情:

...
if ($key == $link) {
    $file = "links/" . $value;
    include_once $file;
}
...

If those files contain PHP code that you want to be executed you need to include or require them rather than echoing the raw data.

Please see PHP's documentation on how to include code files.

EDIT
Kind of hard to tell from your description but if at all, you would have to do something like:

...
if ($key == $link) {
    $file = "links/" . $value;
    include_once $file;
}
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文