2013-03-24 129 views
1

我有這個PHP腳本,它將用戶重定向到基於用戶名和密碼的特定頁面。如何修改客戶端區域的登錄php腳本

一旦您登錄並重定向到您的頁面,然後離開(例如轉到主頁),然後再次單擊客戶端登錄返回到您的頁面,彈出一條消息說您已經登錄點擊此處查看您的頁面。我如何才能將它重定向回登錄用戶的頁面?

如果你有麻煩的瞭解,請訪問我的網站看看吧(用戶:泰勒通:泰勒的登錄信息)splitlinemedia.com

的login.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>'; } 
} 
?> 

protect.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"); 
?> 
+0

您可以重定向一個用戶,使用'header(Location:http://example.com)',將example.com替換爲所需的url – 2013-03-24 00:52:22

回答

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]); 
} 
?> 
0

protect.php只需更換呼應行說,他們已經記錄在一個頭重定向線,如:header("Location: ".$_SESSION['logged_in'][0]);