2012-11-28 19 views
0

這裏是我的形式使用的login.php檢查用戶是否註冊等PHP變化後的目標時,用戶登錄成功

<form action="login.php" method="post" target="SHOW"> 
     <ul id="login"> 
      <li>  
       Username:<br /> 
       <input type="text" name="username"> 
      </li> 
      <li> 
       Password:<br /> 
       <input type="password" name="password"> 
      </li> 
      <li> 
       Submit: 
       <br /> 
       <input type="submit" name="log in"> 
      </li> 
      <li> 
      <a href="../../register.php">Register</a></li> 
     </ul> 
     </form> 
<iframe id="iframe" name="SHOW" scrolling="no"></iframe> 

我的困境與我使用的iframe標籤莖顯示錯誤消息。如果用戶已註冊,我想將他重定向回index.php,就像您在PHP ---> header('Location:index.php')結尾處看到的一樣。

問題是iframe不允許重定向。所以,我想將目標從---> target =「SHOW」改爲target =「_ top」或類似的東西。因此,用戶在成功登錄後重定向,而不使用任何JavaScript。

我在嘗試完成此操作時失敗 - > $ href-> removeAttribute('target'); $ href-> setAttribute(「target」,「_top」);

這裏是login.php。

<?php 

include 'core/init.php'; 

if(empty($_POST) === false){ 
$username = $_POST['username']; 
$password = $_POST['password']; 

if(empty($username) || empty($password) === true){ 
    $errors[] = 'You need to enter a username and password';   
    } else if (user_exists($username) === false){ 
     $errors[] = 'We can\'t find that username. Have you registered?'; 
     } else if (user_active($username) === false){ 
      $errors[] = 'You haven\'t activated your account!'; 
      } else { 
       $login = login($username, $password); 
       if($login === false){ 
        $errors[] = 'That username/password combination is incorrect'; 
        } else { 
         $_SESSION['user_id'] = $login; 
         $href->removeAttribute('target'); 
         $href->setAttribute("target", "_top"); 
         header('Location: index.php'); 
         exit(); 
         } 
       } 

       print_r($errors); 
} 

?>

+1

你試過header('Location:index.php');在頁面頂部只是爲了檢查它是否正常工作 – knightrider

+0

header('Location:index.php');工作正常,但是,它張貼在iframe中,根本沒有用。在網頁內部創建一個迷你窗口。 –

回答

0

既然你POST-ING的IFRAME,由反應採取的任何行動只會是有效的iframe本身的背景下,這意味着做一個普通的HTTP重定向只會改變iframe的內容。

如果你想保留iframe(我建議反對,並保持錯誤處理和記錄在顯示錶單的同一個控制器/文件中),你可以在iframe響應中使用javascript進行重定向:

<script type="text/javascript"> 
    parent.location.href = 'http://www.bbc.co.uk/'; 
</script> 

不漂亮,但它的作品。

+0

感謝您的回覆。非常感謝。什麼是不使用iframe的解決方案?因爲,我不希望將用戶重定向到login.php以獲取錯誤。我可以發佈到別的地方嗎? –