简体   繁体   English

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

[英]How to redirect if user already logged in

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?这是为什么?

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. 此外,您可能希望在调用header()之后放置一个die()语句,以便完全停止脚本的执行。

And you should probably move the call to redirect() above any other output since HTTP headers must be the first thing in the response. 你可能应该将调用redirect()移到任何其他输出之上,因为HTTP头必须是响应中的第一件事。 It's possible that this is also the cause of your problem. 这可能也是导致问题的原因。

Change the redirect() function to: redirect()函数更改为:

header('Location: index.php');

And move the call to redirect above all the html output: 并将调用移至重定向上方的所有html输出:

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

From the header() docs : 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. 请记住,在发送任何实际输出之前,必须通过普通HTML标记,文件中的空行或PHP来调用header()。

This is what it should look like in the end, taking @Jan's advice to add a call to die() : 这是最终应该看起来的样子,采用@ 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>
?> 
function redirect() {
    header('location:index.php');
}

它的header('Location: index.php');

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. 在“刷新:0”中增加0以引入更大的延迟。

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 请注意,如果您在重定向时遇到一些问题,请在函数末尾添加“exit()”

<?php
  session_start();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM