如果用户已经登录,如何重定向
我有一个登录脚本可以执行以下操作:
$_SESSION['username']=$username;
$_SESSION['password']=$password;
如果用户登录成功。 因此,我编辑了注册页面来执行此操作:
<?php
function redirect() {
header(' URL= index.php');
}
?>
<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" id="jmtoday" class=" no_js">
<head>
<link href='icon.jpg' rel='icon' type='image/jpg'/>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-language" content="en" />
<LINK REL=StyleSheet HREF="Mainstyles.css" TYPE="text/css"></link>
<Title>Sign up | JMToday</title>
</head>
<body>
<?php
if(isset($_SESSION['username'])){
redirect();
}
?>
但是当我使用我创建的帐户登录时,它不会重定向用户。这是为什么?
I have a login script that does this:
$_SESSION['username']=$username;
$_SESSION['password']=$password;
If the user logged in succesfully.
And so I edited the signup page to do this:
<?php
function redirect() {
header(' URL= index.php');
}
?>
<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" id="jmtoday" class=" no_js">
<head>
<link href='icon.jpg' rel='icon' type='image/jpg'/>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-language" content="en" />
<LINK REL=StyleSheet HREF="Mainstyles.css" TYPE="text/css"></link>
<Title>Sign up | JMToday</title>
</head>
<body>
<?php
if(isset($_SESSION['username'])){
redirect();
}
?>
But it doesn't redirect the user when I logged in with my account that I created. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
另外,
您可能希望在调用
header()
之后放置一个die()
语句,以便完全停止脚本的执行。而且您可能应该将对
redirect()
的调用移至任何其他输出之上,因为 HTTP 标头必须是响应中的第一个内容。这也可能是导致您的问题的原因。should be
Also you might want to put a
die()
statement after the call toheader()
so that you stop the execution of your script completely.And you should probably move the call to
redirect()
above any other output since HTTP headers must be the first thing in the response. It's possible that this is also the cause of your problem.将
redirect()
函数更改为:并将对重定向的调用移至所有 html 输出之上:
从
header()
文档:这就是最终的样子,采纳 @Jan 的建议添加对
die()
的调用:Change the
redirect()
function to:And move the call to redirect above all the html output:
From the
header()
docs:This is what it should look like in the end, taking @Jan's advice to add a call to
die()
:它是
header('位置:index.php');
It's
header('Location: index.php');
如果您想立即重定向,那么
如果您想延迟一段时间重定向,请
增加“Refresh:0”中的 0 以引入更大的延迟。
您可以在向用户显示一些通知/消息后使用它来重定向。
注意如果您在重定向时遇到问题,请将“exit()”放在函数末尾
if you want to redirect immediately, then
if you want to redirect with some delay, then
increase the 0 in "Refresh:0" to introduce greater delay.
you might use this to redirect after showing some notification/message to the user.
Note if you are having some trouble in redirecting, then put "exit()" at the end of function