在PHP中自动重定向页面
我想在PHP中自动重定向页面 Logout.php:在PHP中自动重定向页面,php,html,http,Php,Html,Http,我想在PHP中自动重定向页面 Logout.php: <?php include "base.php"; $_SESSION = array(); session_destroy(); ?> <meta http-equiv="refresh" content="=0;URL=index.php" /> 其中base.php调用数据库并启动会话: <?php session_start(); $dbhost = "localhost";
<?php
include "base.php";
$_SESSION = array(); session_destroy();
?>
<meta http-equiv="refresh" content="=0;URL=index.php" />
其中base.php调用数据库并启动会话:
<?php
session_start();
$dbhost = "localhost";
$dbname = "login";
$dbuser = "root";
$dbpass = "";
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>
按logout时,我没有返回到
index.php
这应该可以,您在0
之前有一个额外的=
:
<meta http-equiv="refresh" content="0;URL=index.php" />
Linky您可以在PHP代码中添加以下内容:
header('Location:index.php');
请注意,根据all,这必须放在任何输出(甚至空白)之前。据我所知,
HTML
、JavaScript
和PHP
提供了它们自己的页面/标题重定向方式。下面是三个示例,说明如何重定向到http://google.com
#JavaScript:
注意content=“0;
中的0是秒值。它告诉浏览器在开始重定向之前应该等待多少秒
#PHP:
元刷新语法稍有错误
<meta http-equiv="refresh" content="0;URL='<?php echo $_SERVER['HTTP_HOST']; ?>/index.php'">
例如:
Location: http://www.w3.org/pub/WWW/People.html
来源:如果需要使用PHP变量include重定向网页,您可以这样做:其中$user[0]
是PHP变量。这样,下一个网页user.PHP
就可以获得变量的值
header('Location:./user.php?u_id='.$user[0]);
或
我本来打算写的,但谁是第一,永远是赢家!很高兴你提到了关于输出的事实,根据HTTP规范@gamehelp16,headers应该是一个完整的(也称为绝对的)URL-实际上,建议在使用重定向()时使用绝对的URL;函数而不是相对URL’s ding get必须将部分放在任何输出(甚至空白)之前。顺便说一句,从您的角度来看,这不是重定向的好方法(离题)停止使用元标记缓存内容好吗?或者使用其他方法应该好吗?@soft genic我更喜欢标头,因为它可以在标头请求中读取,也可以由服务器(如代理)读取,而不仅仅是由检索浏览器读取。此外,开销稍大,这并不会对(HTML可以gzip压缩,标题不能)@Pee-Haa,谢谢你的提醒。我已经修改了上面的代码。它们不再被维护。请参阅?改为了解,并使用或-将帮助你决定哪个。如果你选择PDO。
<meta http-equiv="refresh" content="0;URL='<?php echo $_SERVER['HTTP_HOST']; ?>/index.php'">
<?php
..
session_destroy();
header( 'HTTP/1.1 301 Moved Permanently');
header( 'Location: ' . $_SERVER['HTTP_HOST'] . '/index.php' );
exit(0);
Location = "Location" ":" absoluteURI
Location: http://www.w3.org/pub/WWW/People.html
header('Location:./user.php?u_id='.$user[0]);
header("Location:./user.php?u_id=$user[0]");