2012-01-16 137 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