我想在php中显示一个javascript警告框并导航回主页面。这是我的代码
session_start();
$name= $_SESSION['name'];
$id= $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">alert(" You are not allowed to view this contnent"); </script>';
header('Location: mainPage.php');
}
//the rest of my page
使用此代码,警报弹出窗口不会仅显示重定向。我也试试这个
$name= $_SESSION['name'];
$id= $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">window.alert(" You are not allowed to view this contnent");</script>';
echo'<script type="text/javascript> window.navigate("mainPage.php"); </script>';
}
//the rest of my page
现在显示弹出窗口,但重定向不起作用。
答案 0 :(得分:0)
试试这个。只需在页面需要重定向的秒数后添加。所以
更改
header('Location: mainPage.php');
到
header( "refresh:5; url=mainPage.php" );
答案 1 :(得分:0)
$name= $_SESSION['name'];
$id= $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">window.alert(" You are not allowed to view this contnent");</script>';
echo'<script type="text/javascript> window.location.href="mainPage.php"; </script>';
}
请查看此代码
答案 2 :(得分:0)
session_start();
$name = $_SESSION['name'];
$id = $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">
alert(" You are not allowed to view this contnent");
self.location="mainPage.php";
</script>';
}
试试这个