1

我有这个 php 脚本,它根据用户名和密码将用户重定向到特定页面。

一旦您登录并重定向到您的页面,然后离开(例如转到主页)然后再次单击客户端登录以返回您的页面,会弹出一条消息说您已经登录单击此处查看你的页面。如何让它重定向回登录用户的页面?

如果您理解有困难,请访问我的网站查看(用户:tyler pass:tyler 用于登录信息)splitlinemedia.com

登录.php

<?php
    if(!defined("SESSION")){
    session_start();
    define("SESSION", true);
 }
    if(isset($_GET["log_out"])){
    unset($_SESSION["logged_in"]);
    header('refresh: 3; url=login.php');
    echo "You're logged out, and will be redirected in about 3 seconds";
    exit;
 }

 $login = true;
 require "protect.php";

 $logins[0]["user"] = "tyler";
 $logins[0]["pass"] = "tyler";
 $logins[0]["redirect"] = "test.php";

 $logins[1]["user"] = "x";
 $logins[1]["pass"] = "y";
 $logins[1]["redirect"] = "z.php";

  // No need to edit below, except the errors

 if(isset($_POST['submit'])){ //is the form submitted?
   if(empty($_POST['user']) || empty($_POST['pass'])){
     echo "You have to fill out the user name and password!";
     exit;
   } //check for empty user name or password
   $is_logged = false; 
   foreach($logins as $login){
      $user = $_POST;
      if(($user["user"] == $login["user"]) && ($user["pass"] == $login["pass"])) {
         $is_logged = true;
         $_SESSION["logged_in"] = array($login["redirect"], true); 
         header("Location: ".$login["redirect"]);
     exit;
     }
 }
 if(!$is_logged){ echo '<script type="text/javascript">alert("Inncorect username or     password");window.history.go(-1);</script>'; } 
 }
 ?>

保护.php

<?php
if(!defined("SESSION")){
   session_start();
   define("SESSION", true);
}
   if((!isset($_SESSION["logged_in"])) || !$_SESSION["logged_in"][1]){
     if(!isset($login)){
        header("Location: login.php"); //check to see if logged in, otherwise go to the login
    exit;
   }
  } else if (isset($login) || isset($index)){
  echo "Your already logged in!! <a href='login.php?log_out'>Click here</a>, to logout.   Or, go back to your <a href='{$_SESSION['logged_in'][0]}'>page</a>.";
  exit;
 }
?>

然后在我的 test.php 页面的顶部

<?php
include("protect.php");
?>
4

2 回答 2

0

protect.php 中,只需将表示他们已经登录的显行替换为标头重定向行,例如:header("Location: ".$_SESSION['logged_in'][0]);

于 2013-03-24T00:54:50.307 回答
0

这可能会帮助你,

<?php
if(!defined("SESSION")){
session_start();
define("SESSION", true);
}
 if((!isset($_SESSION["logged_in"])) || !$_SESSION["logged_in"][1]){
 if(!isset($login)){
 header("Location: login.php"); //check to see if logged in, otherwise go to the login
exit;
}
} else if (isset($login) || isset($index)){
header("Location: " . $_SESSION['logged_in'][0]);
}
?>
于 2013-03-24T01:00:02.760 回答