好的设计要求只编写一次函数.在PHP中我是通过使用包含文件(如Utils.php和Authenticate.php)和PHP命令include_once来实现的.但是,我无法找到PHP包含文件的任何标准或最佳实践.你对StackOverflow的建议是什么?
我在找:
在我的工作中,我可以看到使用SSI的旧网站包含大量代码并做一些有条件的工作.我只是好奇SSI还在发展吗?如果没有,那么现有的简单服务器端脚本语言是什么?
我有一个扩展名为.html的文件,其中包含:
<!--#include virtual="filename"-->
Run Code Online (Sandbox Code Playgroud)
指令,我正在使用Microsoft WebMatrix.当我从WebMatrix运行网页时,指令不会被处理,而是指令显示在HTML中.如何配置WebMatrix以将这些页面视为.asp文件?
我正在编写一个PHP脚本,它通过"require_once()"方法包含了几个外部PHP脚本.我想知道主脚本(包括其他脚本)是否有办法判断包含脚本的处理输出是否生成了任何内容.
因此,例如,用户对特定脚本的权限可能导致PHP不生成任何输出.所以,也许,主脚本会回应如下:
Nothing interesting here!
Run Code Online (Sandbox Code Playgroud)
有没有办法在主脚本中执行此操作,还是需要在包含的脚本中创建这些测试,并将结果返回到主脚本?
感谢您的时间,
spryno724
我无法让服务器端包含工作.我只想将一个html文件包含在另一个中,我似乎无法让它工作
服务器是运行Windows XP的另一台计算机(不是localhost).我有一个略高版本的xampp堆栈,它只运行Apache/2.2.11
虚拟目录(在httpd-vhosts.conf中)配置为
Run Code Online (Sandbox Code Playgroud)<Directory "c:\www\dev1"> Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Order allow,deny Allow from all </Directory>
index.html包含
A Big Block of Text<br />
A Big Block of Text<br />
<!-- #include file="test.shtml" -->
A Big Block of Text<br />
A Big Block of Text<br />
Run Code Online (Sandbox Code Playgroud)
test.shtml包含
This is another line
Run Code Online (Sandbox Code Playgroud)
在我的开发机器上的浏览器中,www.dev.com/index.html显示
A Big Block of Text<br />
A Big Block of Text<br />
A Big Block of Text<br />
A Big Block of Text<br /> …Run Code Online (Sandbox Code Playgroud) 我一直想弄清楚这一点.我想让我的应用引擎网站使用基本的html和shtml,以避免应用引擎上的jsp应用程序的缓慢预热阶段.这样我的目标网页就会立即加载.
基本上,我试图将html文件包含到我的主html文件中(index.html - 我尝试了index.shtml).这是我尝试的命令:
<!-- include virtual="header.html" -->
Run Code Online (Sandbox Code Playgroud)
但它不起作用.服务器端包括似乎没有在应用程序引擎中执行.我是否需要先在某处启用这些命令 - 或者应用引擎是否只是不允许它们?
假设您正在开发一个包含许多页面的网站模板(index.html,blog.html,contact.html ...).首先设计主页,然后转到其他页面.当您在其他页面上时,您会想到一些改进,这些改进必须影响整个网站.
所以我回到每个页面进行更改.这是非常没有效果的,尤其是当您有6页或更多页时,有时您忘记更新更改.
如果它是PHP,我会这样做
require ('header.php');
Run Code Online (Sandbox Code Playgroud)
这对我很有用.但是现在,我正在研究HTML并且不想真正转换为PHP,然后再转换为HTML.
那么你有一个特定的方法/方法吗?
我有几个独立的网站,住在不同的目录中.对于他们共同的包含,我将它生活在其他人居住的根目录中.
user@hostname:/var/www$ ls
website_1 website_2 website_3 common_files
Run Code Online (Sandbox Code Playgroud)
我想包含一个Zend包,所以我有我的包含路径
ini_set("include_path", get_include_path() . ":/var/www/common_files/Zend");
require_once("Mail.php");
Run Code Online (Sandbox Code Playgroud)
Mail.php 加载好了,但是那里的某个地方就是这条线
require_once 'Zend/Mail/Transport/Abstract.php';
Run Code Online (Sandbox Code Playgroud)
这给出了这个错误
Warning: require_once(Zend/Mail/Transport/Abstract.php): failed to open stream: No such file or directory in var/www/common_files/Zend/Mail.php on line 26
Run Code Online (Sandbox Code Playgroud)
所以php不会递归地进入包含路径的目录结构.我是否必须将Zend移动到每个网站目录中,拼出每个包含的路径,或者什么?
BTW摘要确实存在:
user@host:/var/www/common_files/Zend$ tree -d
...
`-- Mail/Transport
|-- Mail/Transport/Abstract.php
|-- Mail/Transport/Exception.php
|-- Mail/Transport/Sendmail.php
`-- Mail/Transport/Smtp.php
9 directories, 32 files
Run Code Online (Sandbox Code Playgroud) 的index.php
<?php
$title = "This is the page title";
require_once '/includes/header.php';
?>
Run Code Online (Sandbox Code Playgroud)
header.php文件
<head>
<title><?= $title ?></title>
</head>
Run Code Online (Sandbox Code Playgroud)
产量

我正在使用10个项目的无序列表作为导航栏。使用SSI,我将标题和导航栏放入每个文件中。我想要一种添加class="active"到当前活动页面的规则集中的方法(当前页面对应的规则<li>将具有不同的样式)。
在每个页面中包含文件意味着在包含的文件中,所有项目都不能具有活动类。
仅用几行代码就可以做到这一点吗?(使用jQuery / JS)
我的另一个选择是将URL的最后一部分与href每个列表项中锚点的一部分匹配。
解决方案:(由RomanGorbatko提供)
var tab = window.location.pathname.split("/");
tab = tab[tab.length - 1]; // This probably is not efficient - suggestions?
if (tab != "") $("#nav a#" + tab).addClass("active");
Run Code Online (Sandbox Code Playgroud) php ×4
html ×2
include ×2
iis ×1
iis-express ×1
include-path ×1
javascript ×1
jquery ×1
navbar ×1
require-once ×1
ssi ×1
webmatrix ×1