[英]Request to another page - Realise with header() or include?
在我的项目中,我使用的是干净的URL,所有请求都将转发到index.php。 在index.php中,我有一个路由aprt,它检查switch / case-construct中的URL参数并包括相应的站点。
但是,为此包括正确的工具吗? 还是应该为此使用header()?
我建议您使用一些简单的路由类,这些类将根据url +自动加载器加载控制器,以包括控制器类。
转发页面链接使用此
<?php
header("location:index.php");
?>
标头用于处理原始的HTTP标头。 IE标头(“位置: http : //www.google.com ”)
include用于包含和评估特定文件。 换句话说,此时包含的文件中的变量和函数可供应用程序使用。
因此,我相信,您想要一个包含您意图的文件。 如果我正确理解您的意思。
使用include
可根据URL有条件地运行PHP代码。 header
用于响应的内容,例如将用户发送到另一个URL。
在最基本的层面上,你可以使用路由实现像这样在你的index.php:
if ($_GET['q']) {
// Break the request into it's path parts
$path_parts = explode('/', $_GET['q']);
$section = $path_parts[0];
}
else {
$section = 'home';
}
switch ($section) {
case 'home':
// For requests to the home page
require('home.html');
case 'about':
// For URLs like http://www.example.com/about
require('about.html');
break;
case 'login':
if (check_login()) {
header('Location: http://www.example.com/');
}
else {
require('login.html');
}
break;
case 'wiki':
// For URLs like http://www.example.com/wiki/Document
$page = empty($path_parts[1]) ? '' : $path_parts[1];
require('wiki.php');
break;
default:
// Unexpected page requests should get a 404
header("HTTP/1.0 404 Not Found");
include('error.html');
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.