2014-09-12 66 views
0

我想检查用户是否没有登录,然后他不能访问病人仪表板页面,并重定向到index.php那工作正常,但当我添加一个警报的javascript在病人仪表板页面,我重定向用户index.php如果没有登录,那么它将首先显示警报框,然后它会去索引页那些不工作...请帮助..重定向到索引页,但不工作的JavaScript警报

patient- dashboard.php

if(!isset($_SESSION['username'])) { 
     echo "<script type='text/javascript'>"; 
     echo "alert('Login First');"; 
     echo "</script>"; 

     header('Location: index.php'); 

    } 

回答

0

您必须先发送标头。该脚本阻止标题工作。重定向到信息页面而不是使用脚本,并使用元刷新将它们带回索引。

信息页面只是一个普通的HTML页面,表示“您必须登录”。你可以像这样回到索引页面: <meta http-equiv="refresh" content="5;URL=http://www.exmaple.com/index/.html"> “5”是五秒钟。你可能只需要在URL中输入index.html;我没有测试过。

0
if(!isset($_SESSION['username'])) { 
     echo '<script type="text/javascript">;alert("Login First");window.location = "index.php";</script>'; 

    } 
0

试试这个,你不需要在不同的回声中有不同的警报部分,而且标题重定向现在在警报之前。

**编辑**此刷新方式工作作为重定向并给出警报

<?php 

$var1 = 1; 
$var2 = 2; 

if($var1 < $var2) { 
    header("Refresh:1; url=index.php"); 
    echo "<script type='text/javascript'>alert('Login First')</script>"; 

}else{ 
    echo "<script type='text/javascript'>alert('Logged In')</script>"; 
} 
?> 
+0

警报不工作我这样做已经... – shani 2014-09-12 14:03:38

+0

@shani我刚刚编辑它,并使用随机if语句(1 <2)我得到了警报,然后重定向 – Parody 2014-09-12 14:10:42

0

尝试

// patient-dashboard.php 
if (!isset($_SESSION['username'])) { 
    $_SESSION['login_failed'] = 1; 
    header('Location: ./index.php'); 
    exit; // eliminate errors and prevent the user from going any further 
} 


// index.php 
if (isset($_SESSION['login_failed'])) { 
    unset($_SESSION['login_failed']; 
    die("Login First"); 
} 

或者

// patient-dashboard.php 
if (!isset($_SESSION['username'])) { 
    die("<script type='text/javascript'>alert('Login First'); location.href = './index.php';</script>"); 
}