2012-01-16 134 views
2

我有一個.php註冊用戶通過調用另一個php函數,但我不知道什麼是最好的方式來重定向用戶,如果註冊成功。除了header incase之外,還有其他選項嗎?我想在我重定向之前回顯一些內容。成功登錄php後重定向

<? 
// processRegister.php 
////////////////////// 

// First include the class definition 
include('UserClass.php'); 

// Next, create an instance of the class 
$newUser = new User; 

// Call the registerUser() method, passing in the required variables 
$newUser->registerUser($userName, $userPassword); 

// If it was an error, the class will kill the script, if not, it will reach this point 
$newUser->displayUserInfo(); 
?> 

回答

1

您可以使用4種方法,如你所說的用頭重定向:

<?php header('Location: success.php'); exit; ?> 

另一種是使用一個會話,並設置要顯示,然後後您顯示該消息的一些消息已重定向。

if($login){ 
    $_SESSION['login_message'] = "Your account has been logged in ".$newUser->username; 
} 

//after redirect 
if(isset($_SESSION['login_message']){ 
    echo $_SESSION['login_message']; 
} 

您也可以使用JavaScript:

<script type="text/javascript"> 
<!-- 
window.location = "http://www.google.com/" 
//--> 
</script> 

在PHP中:

<?php echo '<script>window.location = "http://www.google.com/"</script>'; ?> 

的最後一個方法是使用HTML重定向:

<html> 
<head> 
<meta http-equiv="Refresh" content="5;url=http://www.w3schools.com" /> 
</head> 

<body> 
<p>Your message here, probably in php</p> 
</body> 
</html> 
+0

那麼我回聲出元? – user1082764 2012-01-16 00:37:19

+0

是的 - 如果你迴應,你可以重定向。我錯過了另一種使用Javascript的方式。我補充說。 – 2012-01-16 00:38:22

+1

恕我直言,只有第一應該是**曾經**使用。爲什麼應該等待用戶加載頁面,然後再等到JS完成執行:) – veritas 2012-01-16 00:42:19