PHP如何在标题重定向到另一个页面之前回显警报
I'm trying to make a functionality where, after the user logs in, an alert will popup saying "welcome USER_NAME", after which it will redirect to the user profile page.
I tried the sleep & flush methods, and even ob_start & ob_end_flush methods..but nothing seems to work, it just redirects to user profile after logging in without echoing the alert. Is there any way of fixing this problem?
Here is the part of the code:
<?php
if (isset($_SESSION['user'])) {
``$_SESSION['user'] = $uname;
``echo "<script> alert('Welcome $uname')</script>";
``header("location: dashboard.php");
}
?>
我正在尝试创建一个功能,在用户登录后,会弹出一条警告“欢迎USER_NAME” “,之后它将重定向到用户个人资料页面。 p>
我尝试了睡眠&amp; 冲洗方法,甚至是ob_start&amp; ob_end_flush方法..但似乎什么都没有用,它只是在登录后重定向到用户配置文件而不回显警报。 有没有办法解决这个问题? p>
以下是代码的一部分: p>
&lt;?php
if(isset) ($ _SESSION ['user'])){
`` $ _SESSION ['user'] = $ uname;
``echo“&lt; script&gt; alert('Welcome $ uname')&lt; / script&gt;” ;
``header(“location:dashboard.php”);
}
?&gt;
code> pre>
div>
PHP's redirection with header("Location:xxx.php");
functionality does not allow any browser output.
You can use Javascript redirection (taking in consideration of program logic).
echo '<script>
alert(' + '"Welcome ' + $uname .'");
window.location.href="welcome.php";
</script>';
For simple javascript alerts, you can simply use window.location.href
but if you need to show some advanced popups like bootstrap modals or sweet alerts you can use setTimeout
which evaluates an expression after a specified number of milliseconds.
echo "<script> alert('Welcome $uname');
window.setTimeout(function(){
window.location.href = 'http://fullpath/dashboard.php';
}, 3000);
</script>"
You can use javascript alert and then redirect it after specific timeout
echo '<script type="text/javascript">';
echo 'alert("Welcome $uname");';
echo 'window.location.href = "index.php";';
echo '</script>';
try upper instead of below code
``echo "<script> alert('Welcome $uname')</script>";
``header("location: dashboard.php");