包括相同的PHP文件两次或更多次

时间:2013-11-16 22:04:05

标签: php

我遇到了一个问题:我试图在同一页面中包含三次PHP文件,但它似乎根本不起作用。第一次运行后,测试服务器上的HTML表格完全空白。

我正在使用

<?php include("test.php"); ?>

3次。

有什么建议吗?

<a href="http://www.facebook.com/share.php?u=<?php include("url.php") ?>&title=<?php  include("title.php") ?>" class="tooltip" onclick="open(this.href, '_blank', 'width=700,height=350'); return false;"><span>Dela sidan på Facebook.</span><img src="images/f.png" width="32" height="32"></a>

<a href="http://twitter.com/home?status=<?php include("title.php") ?>+<?php include("url.php") ?>" class="tooltip" onclick="open(this.href, '_blank', 'width=555,height=555'); return false;"><span>Dela sidan på Twitter.</span><img src="images/t.png" width="32" height="32"></a>

<a href="https://plus.google.com/share?url=<?php include("url.php") ?>" class="tooltip" onclick="open(this.href, '_blank', 'width=615,height=615'); return false;"><span>Dela sidan på Google+.</span><img src="images/g.png" width="32" height="32"></a>

test.php包含:

function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

echo curPageURL();

2 个答案:

答案 0 :(得分:2)

“但它似乎根本不起作用”是在问题中写的最无意义的事情。解释问题是什么,或错误

您最有可能获得的是“无法重新定义函数curPageURL”或类似错误 - 一旦您创建了一个函数,就无法一遍又一遍地再次定义它。

没有理由像这样设置您的页面 - 将您的功能定义放在一个文件中,从主页中多次调用它

答案 1 :(得分:0)

多次包含同一个文件并不好。

让我们稍微重构一下代码......

使用以下命令创建一个新文件'data.php':

<?php
function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

function getUrl() {
    return 'YOUR URL HERE';
}

function getTitle() {
    return 'YOUR TITLE HERE';
}
?>

现在将您的来源更新为:

<a href="http://www.facebook.com/share.php?u=<?php echo getUrl() ?>&title=<?php  echo getTitle() ?>" class="tooltip" onclick="open(this.href, '_blank', 'width=700,height=350'); return false;"><span>Dela sidan på Facebook.</span><img src="images/f.png" width="32" height="32"></a>

<a href="http://twitter.com/home?status=<?php echo getTitle() ?>+<?php echo getUrl() ?>" class="tooltip" onclick="open(this.href, '_blank', 'width=555,height=555'); return false;"><span>Dela sidan på Twitter.</span><img src="images/t.png" width="32" height="32"></a>

<a href="https://plus.google.com/share?url=<?php echo getUrl() ?>" class="tooltip" onclick="open(this.href, '_blank', 'width=615,height=615'); return false;"><span>Dela sidan på Google+.</span><img src="images/g.png" width="32" height="32"></a>

并且打电话包括(“test.php”) 3次,请致电 curPageURL()

我希望我有所帮助!

相关问题