2014-03-06 23 views
0

我想在php中显示一个JavaScript警告框并导航回主页面。这是我的代码在php中显示提醒框并向后导航

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

试试这个。只需在页面需要重定向多少秒后添加即可。所以

变化

header('Location: mainPage.php'); 

header("refresh:5; url=mainPage.php"); 
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>'; 

    } 

请参阅该代码

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>'; 

} 

试试这个