制作一个没有框架的网站

发布于 2024-08-25 04:26:49 字数 84 浏览 5 评论 0原文

我想制作我的主页,没有框架,我是否应该在index.php上拆分我的设计,使其成为header.php/footer.php,然后将它们包含在每个页面上?

I want to make my homepage, without frames, should i just split up my design on index.php so it is header.php/footer.php, and then just include them on every page?

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

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

发布评论

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

评论(5

梦断已成空 2024-09-01 04:26:50

我建议你使用一个框架。大多数框架(如果不是全部)都有简单的模板系统,因此您不必重复代码。

I suggest you use a framework. Most frameworks (if not all) have simple template systems, so you don't have to repeat code.

贱贱哒 2024-09-01 04:26:50

在网站的每个页面中包含内容的建议解决方案的问题在于,如果您想包含其他内容(例如侧边栏),则必须更新网站的所有页面。

更好的想法是根本不使用脚本-页面连接。因此,您不必为要显示的每个页面编写一个 php 文件。相反,使用一个前端控制器文件,大多数使用网站根目录中的index.php。然后使用 Apache mod_rewrite 或其他服务器技术来灵活地设置站点的 URL。然后让index.php映射不同的URL请求来服务不同的页面,然后你可以将站点的所有页面放入数据库或其他地方。

这样,您的网站中只有一个点包含页眉和页脚的模板,因此可以轻松更改,并且您可以使用网站的根目录来服务 AJAX 请求,在该请求中您不想输出 HTML,而是输出 JSON例如。

Afaik 这是一个很好的解决方法。

The problem with the suggested solution of including stuff in every page of your site is that you have to update all the pages of your site if you want to include another thing, say a sidebar.

A better idea is not to have a script--page connection at all. So you don't write a php file per page you want to show. Instead, use one front controller file, most use index.php in the root of the website. And then use Apache mod_rewrite or other server techniques to have flexibility in the URL's of your site. Then let index.php map different URL requests to serve different pages, you can then put all the pages of your site into a database or somewhere else.

This way there's only one point in your site that includes the templates for the header and footer, so that's easily changeable, and you can use the root of the site to serve AJAX requests, in which you won't want to output HTML but JSON for instance.

Afaik this is a good way of going about it.

断舍离 2024-09-01 04:26:50

另一种想法是只有一个入口点,使用 GET 参数调用,例如 ?site=about。您的 index.php 可能如下所示:

<?php
// whitelist of allowed includes
$allowedIncludes = array('home', 'about', 'error404'); // etc.
// what to include if ?site is not set at all / set to an illegal include
$defaultInclude = 'home';
$errorInclude = 'error404';

// if site is not set, include default
$site = (empty($_GET['site'])) ? $defaultInclude : $_GET['site'];
// if site is illegal, include error page
$include = (in_array($site, $allowedIncludes)) ? $site : $errorInclude;

// actual includes
include 'header.php';
include $include.'.php';
include 'footer.php';

因此,您只需包含 header.phpfooter.php 一次,即可完全控制内容允许什么,不允许什么(包含的文件可能位于只有 php 有权访问的目录中)。当您的 index.php 处理请求时,home.phpabout.php 不必了解 header.php< /code> 和 footer.php (您可以在以后轻松地替换它们)。

如果您不喜欢http://www.example.com/?site=about,您可以查看mod_rewrite和朋友。

Another idea would be to have just one single point of entry which is called with a GET parameter, e.g ?site=about. Your index.php could look like this:

<?php
// whitelist of allowed includes
$allowedIncludes = array('home', 'about', 'error404'); // etc.
// what to include if ?site is not set at all / set to an illegal include
$defaultInclude = 'home';
$errorInclude = 'error404';

// if site is not set, include default
$site = (empty($_GET['site'])) ? $defaultInclude : $_GET['site'];
// if site is illegal, include error page
$include = (in_array($site, $allowedIncludes)) ? $site : $errorInclude;

// actual includes
include 'header.php';
include $include.'.php';
include 'footer.php';

Thus you only have to include header.php and footer.php once and have full control about what is allowed and what is not (the included files could be in a directory that only php has access to). While your index.php handles the request, home.php, about.php do not have to know about header.php and footer.php (you could easily replace them at a later point in time).

If you do not like http://www.example.com/?site=about, you can look into mod_rewrite and friends.

も让我眼熟你 2024-09-01 04:26:50

您可能想为此设置一个会话。只要访问者在您的网站上,会话变量就存在:

<?php
    session_start(); // Remember that session_start(); must be the first line of your PHP and HTML-code

    if($add_a_message){
        $_SESSION['message'] = 'Message';
    }

    if($destroy_message){
        $_SESSION['message'] = '';
    }

    // echo this message
    if(isset($_SESSION['message']) && strlen($_SESSION['message']) > 0){
        echo '<strong>' . $_SESSION['message'] . '</strong>';
    }
?>

You may want to set a session for that. A session variable exists as long as a visitor is on your website:

<?php
    session_start(); // Remember that session_start(); must be the first line of your PHP and HTML-code

    if($add_a_message){
        $_SESSION['message'] = 'Message';
    }

    if($destroy_message){
        $_SESSION['message'] = '';
    }

    // echo this message
    if(isset($_SESSION['message']) && strlen($_SESSION['message']) > 0){
        echo '<strong>' . $_SESSION['message'] . '</strong>';
    }
?>
笙痞 2024-09-01 04:26:49

是的,您可以将 index.php 拆分为 header.php/footer.php,然后将它们包含在每个页面上。
请注意,您的页面不能是静态 HTML,而是 php 脚本,以便用一个脚本显示多个页面。
我还建议不要使用像这样的常见结构,

include 'header.php';
//do some stuff
include 'footer.php';

而是另一种更有用的结构:

//do some stuff, retrieve all data.
include 'header.php';
include 'page.php'; //include page template
include 'footer.php';

Yes, you can split your index.php into header.php/footer.php and then just include them on every page.
Note that your pages can be not static HTML but php scripts, to show multiple pages with one script.
I'd suggest also to have not a commonplace structure like

include 'header.php';
//do some stuff
include 'footer.php';

but another structure, much more useful:

//do some stuff, retrieve all data.
include 'header.php';
include 'page.php'; //include page template
include 'footer.php';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文