简体   繁体   中英

How do i redirect a php login file back to index.html presented with a message 'Welcome User'

My work codes but the message is not showing up after clicking login. It does redirect for 5 second but does not show the appropriate message 'Welcome User'

$error = "";
$success = "";

//  username: user password: pass
if (isset($_POST["submit"])) {
  $username = $_POST['username'];
  $password = $_POST['pass'];
  if ($username == "user") {
    if ($password == "pass") {
      $error = "";
      $success = "Welcome user!";
      header('Location: index.php');
      sleep(5);
    }
    else {
      $error = "Wrong Password!";
      $success = "";
    }
  }
  else {
      $error = "Wrong Username!";
      $success = "";
  }
}

You could use session variables to store the message and display it one time, then delete it.

session_start();
$_SESSION['message'] = 'Success! Operation completed';
header("Location: index.php");
exit();

Then in index.php , display the message

session_start();
if(isset($_SESSION['message'])){
    print_r($_SESSION['message']);#display message
    unset($_SESSION['message']); #remove it from session array, so it doesn't get displayed twice
}

如果您不想通过URL传递消息,则可以使用Session变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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