如何在访问页面时重定向到另一个页面,但如果包含页面则不重定向 (PHP)
How to redirect to another page if page is visited, but not if page is included (PHP)
我是一个PHP-新手,想自学,但现在遇到了一个问题。
我有一个 index.php,它做这样的事情:
<!DOCTYPE html>
<?php include("header.php"); ?>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<?php printHeader(); ?>
</body>
</html>
和一个 header.php,看起来像这样:
<?php function printHeader(){ ?>
<p>foo</p>
<?php } ?>
}
现在,我的问题是,当我访问 index.php 时,它工作正常,但是如果我访问 header.php,我想重定向到 index.php 而不是看到空白页面.如果我使用 header(..):
<?php header("Location: index.php") ?>
<?php function printHeader(){ ?>
<p>foo</p>
<?php } ?>
}
我当然得到了一个错误(“页面没有正确重定向”),因为 index.php 重定向到自身,这开始了一个循环。
有没有PHP中的主函数,只有在直接调用页面时才会调用,而只有包含时才调用?或者还有其他解决办法吗?
提前致谢! :3
如果您希望仅在直接访问脚本时才发生重定向,您可以使用 $_SERVER["REQUEST_URI"]
来确定。
所以对于页眉,请从
<?php header("Location: index.php") ?>
<?php function printHeader(){ ?>
<p>foo<?/p>
<?php } ?>
}
至
<?php
if ($_SERVER["REQUEST_URI"]=="header.php" || $_SERVER["REQUEST_URI"]=="/header.php") {
header("Location: index.php") ;
}
?><?php function printHeader(){ ?>
<p>foo<?/p>
<?php } ?>
}
试试这个:
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<?php require_once dirname( __FILE__ ) . '/header.php'; ?>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<?php printHeader(); ?>
</body>
</html>
header.php
<?php
/* -------------------- */
if ( $_SERVER['REQUEST_URI'] == '/header.php' ) {
/* -------------------- */
header('Location: ./');
}
/* -------------------- */
function printHeader () {
/* -------------------- */
echo '<p>foo</p>';
}
?>
我是一个PHP-新手,想自学,但现在遇到了一个问题。
我有一个 index.php,它做这样的事情:
<!DOCTYPE html>
<?php include("header.php"); ?>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<?php printHeader(); ?>
</body>
</html>
和一个 header.php,看起来像这样:
<?php function printHeader(){ ?>
<p>foo</p>
<?php } ?>
}
现在,我的问题是,当我访问 index.php 时,它工作正常,但是如果我访问 header.php,我想重定向到 index.php 而不是看到空白页面.如果我使用 header(..):
<?php header("Location: index.php") ?>
<?php function printHeader(){ ?>
<p>foo</p>
<?php } ?>
}
我当然得到了一个错误(“页面没有正确重定向”),因为 index.php 重定向到自身,这开始了一个循环。
有没有PHP中的主函数,只有在直接调用页面时才会调用,而只有包含时才调用?或者还有其他解决办法吗?
提前致谢! :3
如果您希望仅在直接访问脚本时才发生重定向,您可以使用 $_SERVER["REQUEST_URI"]
来确定。
所以对于页眉,请从
<?php header("Location: index.php") ?>
<?php function printHeader(){ ?>
<p>foo<?/p>
<?php } ?>
}
至
<?php
if ($_SERVER["REQUEST_URI"]=="header.php" || $_SERVER["REQUEST_URI"]=="/header.php") {
header("Location: index.php") ;
}
?><?php function printHeader(){ ?>
<p>foo<?/p>
<?php } ?>
}
试试这个:
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<?php require_once dirname( __FILE__ ) . '/header.php'; ?>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<?php printHeader(); ?>
</body>
</html>
header.php
<?php
/* -------------------- */
if ( $_SERVER['REQUEST_URI'] == '/header.php' ) {
/* -------------------- */
header('Location: ./');
}
/* -------------------- */
function printHeader () {
/* -------------------- */
echo '<p>foo</p>';
}
?>