在同一页面重定向不起作用?

时间:2014-02-25 15:06:01

标签: php redirect

我有这段代码:

// index.php
if(!isset($_SESSION['user']) && !isset($_SESSION['id']){    
     // My Code 
     if($ris1['pass'] == $pass)){ // it controls if the pass the user inserts is true...
         $_SESSION['user'] = $ris1['user'];
         $_SESSION['id'] = $ris1['id'];
         die(header("Location: index.php")); // the same page 
     }
} else { 
     // My Code 
}

我插入:die(header("Location: index.php"));自动重定向页面,但是如果传递正确,它会保存我的会话,但是,当我重定向用户时,用户会看到你看到的同一页面的一半,如果$ _SESSION没有定义,但$ _SESSION是isset,我看到我在其他地方(在我的代码中)我必须更新页面... 我尝试使用ob_start(),但它不起作用...... 为什么我的页面没有自动重定向,而是在其他地方?

1 个答案:

答案 0 :(得分:1)

在中止当前代码之前尝试重定向:

if(!isset($_SESSION['user']) && !isset($_SESSION['id'])) {    
     // My Code 
     if($ris1['pass'] == $pass)){ // it controls if the pass the user inserts is true...
         $_SESSION['user'] = $ris1['user'];
         $_SESSION['id'] = $ris1['id'];
         header("Location: index.php"); // the same page 
         die();
     }
} else { 
     // My Code 
}