简体   繁体   English

成功登录后的php重定向

[英]php redirect on successful login

I have a .php that registers a user by calling another php function but i dont know what the best way to redirect a user if the registration is successful.我有一个 .php,它通过调用另一个 php 函数来注册用户,但我不知道如果注册成功,重定向用户的最佳方法是什么。 Is there an option other than header incase i want to echo something before i redirect?除了标题之外还有其他选项吗我想在重定向之前回显一些内容?

<?
// 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();
?> 

You can use 4 methods, as you said using a header redirect:您可以使用 4 种方法,正如您所说的使用标头重定向:

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

Another is to use a session and to set some message you want to display and then display that message after you have redirected.另一种方法是使用会话并设置一些要显示的消息,然后在重定向后显示该消息。

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

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

You can also use javascript:您还可以使用 javascript:

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

in php:在 php 中:

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

The last method is to use a HTML redirect:最后一种方法是使用 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>

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

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