简体   繁体   English

如何在站点范围内包含PHP文件使用.HTACCESS或其他方法

[英]How To Include a PHP File Site-wide Using .HTACCESS or other methods

I want to include my PHP file googleanayltics.php on every page on my webserver that is a .php or .html document 我想在我的网络服务器上的每个页面上包含我的PHP文件googleanayltics.php ,该文件是.php或.html文档

I would like to know: 我想知道:

A) How do I add this right BEFORE the </head> tag A)如何在</head>标记之前添加此权限
B) How do I add right right AFTER the <body> tag B)如何在<body>标签后右键添加

I'd like to know both methods for flexibility 我想知道这两种方法的灵活性

I think .HTACCESS could accomplish this easily, and if you know how, or if you know of any easier method then please do share. 我认为.HTACCESS可以很容易地实现这一点,如果你知道如何,或者你知道任何更简单的方法,那么请分享。

PS I do not want to manually go in and enter a code on every file, that is why I am asking this question (for time saving) PS我不想手动进入并在每个文件上输入代码,这就是我问这个问题的原因(节省时间)

EDIT: Only now I see what you actually wanted (was hidden due to formatting). 编辑:只是现在我看到你真正想要的东西(由于格式化而被隐藏)。 Although possible, that would be a very clumsy thing to do, and really not worth the effort. 尽管可能,这将是一个非常笨拙的事情,并且真的不值得努力。 See my other answer for a way to do that. 请参阅我的其他答案,了解如何做到这一点。


There are two php.ini settings you might be interested in: 您可能感兴趣的有两个php.ini设置:

auto_prepend_file and auto_append_file auto_prepend_fileauto_append_file

They can be changed via .htaccess (see the other answer). 它们可以通过.htaccess进行更改(参见其他答案)。

NOTES : 注意

  • These will be included before or after the whole PHP script; 这些将包含在整个PHP脚本之前或之后; but not in specific sections of the output. 但不是在输出的特定部分。

  • They will only affect files handled that go through PHP, which means HTML files are not included, unless your server is set up to pass HTML files through PHP. 它们只会影响通过PHP处理的文件,这意味着不包括HTML文件,除非您的服务器设置为通过PHP传递HTML文件。 This can be done by adding the following line to your .htaccess: 这可以通过在.htaccess中添加以下行来完成:

     AddType application/x-httpd-php .html .htm 
  • Auto prepending a file that generates output is a dangerous thing to do, because it will affect (break) scripts that set headers or use sessions. 自动添加生成输出的文件是一件危险的事情,因为它会影响(中断)设置标头或使用会话的脚本。

You can append or prepend files by setting a PHP ini value in .htaccess 您可以通过在.htaccess中设置PHP ini值来追加或添加文件

php_value auto_prepend_file "full_path_to_the_include_directory/prepend.php" 
php_value auto_append_file "full_path_to_the_file_containing_your_analytics_code" 

(It'd be better to set them manually in php.ini) (最好在php.ini中手动设置它们)

http://php.net/manual/en/ini.core.php http://php.net/manual/en/ini.core.php

在htaccess你可以放

php_value include_path "your/include/path/here/googleanayltics.php"

Here's what you could do accomplish what you actually want. 以下是您可以完成的实际目标。

  • Create two files, called prepend.php and append.php . 创建两个名为prepend.phpappend.php的文件。
  • Use the method previously described to set them as auto_prepend_file and auto_append_file . 使用前面描述的方法将它们设置为auto_prepend_fileauto_append_file

prepend.php prepend.php

<?php

function callback($buffer) {
    $fileContents = file_get_contents('somefile.html');

    // insert $fileContents right BEFORE </head> tag
    return str_replace('</head>', $fileContents . '</head>', $buffer);

    // insert $fileContents right AFTER <body> tag
    // return str_replace('<body>', . '<body>' . $fileContents , $buffer);

}

ob_start("callback");

?>

append.php append.php

<?php

ob_end_flush();

?>

This relies on output buffering and is relatively expensive. 这依赖于输出缓冲并且相对昂贵。 You might also run into problems if your scripts also use output buffering. 如果您的脚本也使用输出缓冲,您可能也会遇到问题。

So my best advice is to design your scripts in a way that the a layout is used for every page and you can easily change what's included in your pages by just changing the layout. 因此,我最好的建议是以每个页面使用布局的方式设计脚本,只需更改布局即可轻松更改页面中包含的内容。

if its in the same folder 如果它在同一个文件夹中

<?php include_once("googleanayltics.php") ?>

If not use this 如果不使用这个

<?php include("/customers/f/5/f/example.com/httpd.www/folder/googleanayltics.php"); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM