2014-09-24 83 views
0

我不知道我的代碼有什麼問題。我試圖做一個簡單的頁面重定向。但它不起作用。如果我訪問login.php頁面,然後點擊登錄,它將保持在login_process.php中並且什麼都不顯示,它應該重定向到books.php。如果我已經登錄,我再次訪問login.php中,它會詢問用戶名和密碼,但它不應該發生爲什麼我的頁面重定向不起作用?

//Login.php

<?php 

     include_once ('autoload.php'); 

     open_page($configs['web_name']); 
    ?> 

    <form action="login_process.php" method="post"> 
     <table border="1"> 
      <tr> 
       <td>Username</td> 
       <td>:</td> 
       <td><input type="text" name="username"></td> 
      </tr> 

      <tr> 
       <td>Password</td> 
       <td>:</td> 
       <td><input type="password" name="password"></td> 
      </tr> 
      <tr> 
       <td colspan="3"><input type="submit" name="action" value="login"></td> 
      </tr> 
     </table> 
    </form> 

    <?php 
    close_page(); 
    ?> 

//Login_process.php

<?php 

     include_once ('autoload.php'); 

     $is_logged_in = get_session('is_logged_in'); 
     if(!$is_logged_in){ 
      $username = get_form_post('username'); 
      $password = get_form_post('password'); 

      if($configs['username'] == $username && $configs['password'] == $password){ 
       set_session('is_logged_in',TRUE); 
      }else{ 
       redirect('login.php'); 
      } 
     } 
      redirect($configs['main_page']); 

而且重定向功能是:

function redirect($_location){ 
     header('Location : ' .$_location); 
    } 
+0

的print_r($ CONFIGS)Location之間的空間,並告訴我們結果。另外,我看到你正在分離編程邏輯和頁面展示的良好軌道上,但要更進一步:使用模板引擎!我建議你Smarty,因爲它是我使用的。 – 2014-09-24 14:39:13

回答

1

中刪除空格:

header('Location : ' .$_location); 
       ^space 

,這樣它的內容:

header('Location: ' .$_location); 

不應該有和login_process.php結腸:

+1

謝謝夥計們!這是工作:D – 2014-09-24 14:45:08

+0

@DanielBoyMarpaung不客氣。接受解決,*歡呼* – 2014-09-24 14:45:44

+0

@DanielBoyMarpaung歡迎來到堆棧。如何接受答案** http://meta.stackexchange.com/a/5235/** – 2014-09-24 14:58:01