如何使用页面后面定义的变量?PHP
以下是我设置文件的方式: INDEX.PHP:如何使用页面后面定义的变量?PHP,php,Php,以下是我设置文件的方式: INDEX.PHP: include($sys->root_path.'sections/start.php'); // Includes everything from <html> to where we are now if (isset($_GET['page'])) { $page = $_GET['page'].'.php'; } else { $page = 'index.php';
include($sys->root_path.'sections/start.php'); // Includes everything from <html> to where we are now
if (isset($_GET['page'])) {
$page = $_GET['page'].'.php';
} else {
$page = 'index.php';
}
if (isset($_GET['cat'])) {
$cat = $_GET['cat'];
} else {
$cat = '';
}
include($sys->root_path.'/content/'.$cat.'/'.$page);
include($sys->root_path.'sections/end.php'); // Includes everything from here to </html>
我遇到的问题是,我想为每个页面指定一个标题、元描述等,并在调用此特定页面的任何特定数据之前,将其输出到start.php
中的页面
我希望做的事情是这样的:
if (isset($_GET['page'])) {
$page = $_GET['page'].'.php';
} else {
$page = 'index.php';
}
if (isset($_GET['cat'])) {
$cat = $_GET['cat'];
} else {
$cat = '';
}
include($sys->root_path.'sections/start.php');
/CONTENT/RED/CAR.PHP:
<?php $page_title = 'Title of the page'; ?>
<p>Everything below is just the page's content...</p>
下面的所有内容都只是页面的内容
当所有这些数据都在这个特定页面的内容之前被抓取时,我如何在站点的
中使用这个页面特定的数据?我的建议是使用MVC方法,或者使用传递参数的函数,或者使用带有setter函数的OOP 您可以执行以下操作:
switch($_GET['page']) {
case 'car':
// Here you could have another conditional for category.
$page_title = 'Title of the page';
break;
// Other cases.
}
include($sys->root_path.'sections/start.php');
<title><?php echo $page_title; ?></title>
在start.php中,您可以有如下内容:
switch($_GET['page']) {
case 'car':
// Here you could have another conditional for category.
$page_title = 'Title of the page';
break;
// Other cases.
}
include($sys->root_path.'sections/start.php');
<title><?php echo $page_title; ?></title>
我必须建议不要以那种方式包含内容。这是不安全的。有人可以浏览您的服务器文件或包含您不希望包含的内容。除非总是通过正则表达式或其他方式过滤该变量,否则永远不应该以这种方式(通过get变量)包含文件
正确的方法是使用数据库和
apacheurl\u重写。我的答案只是解决你的问题
第一步 将
start.php
包含在if语句
下面,这样当您包含start.php时,您就已经知道需要哪个页面了,如下所示:
if (isset($_GET['page'])) {
$page = $_GET['page'].'.php';
} else {
$page = 'index.php';
}
if (isset($_GET['cat'])) {
$cat = $_GET['cat'];
} else {
$cat = '';
}
include($sys->root_path.'sections/start.php');
步骤2
现在,在start.php
中使用一个开关:
<?php
switch ($cat) {
case "blue":
$title = "The car is $page";
break;
case "green":
$title = "The car is $page";
break;
case "red":
$title = "The car is $page";
break;
default:
$title = "Welcome to ...";
}
?>
<!DOCTYPE html>
<head>
<title><?php echo $title ?></title>
</head>
etc...
等
很遗憾,您不能这样做。您需要从这两个文件中的一个文件中删除
标记,因为如果您回送两次,浏览器将获得两次。。。。或者使用输出缓冲,虽然它可以工作,但几乎肯定不是适合这项工作的工具。谢谢你的建议,但我想将所有特定于页面的数据保留在该页面上。我不想去更新多个页面。而且,这个网站可能有几百个页面。对所有这些都使用switch语句,或者像那样单独指定每个页面标题是不实际的。但是谢谢。