繁体   English   中英

重定向到另一个页面后如何显示警报

[英]how to display an alert after redirect to another page

登录表单(index.php)已创建。 提交表单时,用户名和密码将传递到另一个页面(login-controller.php)。 验证后,如果用户名或密码不正确,则在login-controller.php。中显示警报框后,用户已重定向至登录表单(index.php),但我想在重定向至登录后显示诸如“无效的登录凭据”之类的警报框表格(index.php)。 我该如何解决。 下面给出了login-controller.php的代码。

 <?php session_start(); if (isset($_SESSION['username'])) { header('Location: ../service/index.php'); } include '../include/conn.php'; //database connection if (isset($_POST["submit"])) { if (empty($_POST["username"] && $_POST["password"])) { header('Location: index.php'); } else { $username = mysqli_real_escape_string($conn, $_POST["username"]); $password = mysqli_real_escape_string($conn, $_POST["password"]); $password = md5($password); $sqli = "SELECT * FROM user WHERE name = '$username' AND password = '$password'"; $result = $conn->query($sqli); $count = mysqli_num_rows($result); if ($count == 1) { $_SESSION['username'] = $username; header('Location: ../service/index.php'); } else { include '../include/bootstrap.php'; //bootstrap js and css echo '<div class="alert alert-danger"><strong>Oops!</strong> Invalid Login Credentials.</div>'; echo "<script>setTimeout(\\"location.href = 'index.php';\\",3000);</script>"; } } } ?> 

使用会话变量,您可以显示警报:

$_SESSION['alerts'][] = "Test Alert";
$_SESSION['alerts'][] = "Another Test Alert";

然后在您的配置或在您拥有的每个页面上执行的任何其他.php文件中(如果您有模板系统,请在模板中执行此操作):

if(isset($_SESSION['alerts'])){
    foreach($_SESSION['alerts'] as $alertmessage){
        echo '<div class="alert">' . $alertmessage . '</div>';
    }
}

暂无
暂无

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

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