如果用户已经登录,如何重定向

发布于 2024-10-09 05:30:16 字数 959 浏览 0 评论 0原文

我有一个登录脚本可以执行以下操作:

$_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

瞄了个咪的 2024-10-16 05:30:17
<?php
  session_start();

  if((isset($_SESSION["username"]))) 
  {
   header("Location: home.php");
  }
?>
<?php
  session_start();

  if((isset($_SESSION["username"]))) 
  {
   header("Location: home.php");
  }
?>
兔姬 2024-10-16 05:30:16
header(' URL= index.php');

另外,

header ( 'Location: index.php' );

您可能希望在调用 header() 之后放置一个 die() 语句,以便完全停止脚本的执行。

而且您可能应该将对 redirect() 的调用移至任何其他输出之上,因为 HTTP 标头必须是响应中的第一个内容。这也可能是导致您的问题的原因。

header(' URL= index.php');

should be

header ( 'Location: index.php' );

Also you might want to put a die() statement after the call to header() 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.

深海夜未眠 2024-10-16 05:30:16

redirect() 函数更改为:

header('Location: index.php');

并将对重定向的调用移至所有 html 输出之上:

<?php session_start();
if(isset($_SESSION['username'])) {
    redirect();
} ?>

header() 文档

记住必须调用 header()
在发送任何实际输出之前,
可以是普通的 HTML 标签,空白
文件中的行,或来自 PHP 的行。

这就是最终的样子,采纳 @Jan 的建议添加对 die() 的调用:

<?php
function redirect($DoDie = true) {
    header('Location: index.php');
    if ($DoDie)
        die();
}
php session_start();
if(isset($_SESSION['username'])) {
    redirect();
}
?>
<!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>
?> 

Change the redirect() function to:

header('Location: index.php');

And move the call to redirect above all the html output:

<?php session_start();
if(isset($_SESSION['username'])) {
    redirect();
} ?>

From the header() docs:

Remember that header() must be called
before any actual output is sent,
either by normal HTML tags, blank
lines in a file, or from PHP.

This is what it should look like in the end, taking @Jan's advice to add a call to die():

<?php
function redirect($DoDie = true) {
    header('Location: index.php');
    if ($DoDie)
        die();
}
php session_start();
if(isset($_SESSION['username'])) {
    redirect();
}
?>
<!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>
?> 
深居我梦 2024-10-16 05:30:16
function redirect() {
    header('location:index.php');
}
function redirect() {
    header('location:index.php');
}
め七分饶幸 2024-10-16 05:30:16

它是 header('位置:index.php');

It's header('Location: index.php');

月野兔 2024-10-16 05:30:16

如果您想立即重定向,那么

function redirect(){
    header("Location: home.php");
}

如果您想延迟一段时间重定向,请

function redirect(){
    header("Refresh: 0;url=default.php");
}

增加“Refresh:0”中的 0 以引入更大的延迟。

您可以在向用户显示一些通知/消息后使用它来重定向。

注意如果您在重定向时遇到问题,请将“exit()”放在函数末尾

if you want to redirect immediately, then

function redirect(){
    header("Location: home.php");
}

if you want to redirect with some delay, then

function redirect(){
    header("Refresh: 0;url=default.php");
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文