2016-07-07 63 views
2

直接將用戶導向另一個頁面如果IF語句正確,將用戶導向另一個頁面的最佳方式是什麼?我希望頁面將用戶引導到另一個頁面使用PHP,當IF語句運行時,我厭倦了這一點,但它不工作?直接用戶使用php

if (mysqli_num_rows ($result) > 0) 
    { 

    header('Location: exist.php'); 
    die(); 

    } 

以下是該頁面的完整源代碼。

<?php 

    // starts a session and checks if the user is logged in 
    error_reporting(E_ALL & ~E_NOTICE); 
    session_start(); 

    if (isset($_SESSION['id'])) { 
     $userId = $_SESSION['id']; 
     $username = $_SESSION['username']; 


    } else { 
     header('Location: index.php'); 
     die(); 

    } 

?> 



<!DOCTYPE html> 

<html lang="en"> 



    <head> 

    </head> 



    <body> 

     <p><span>Room No: </span><?php $room = $_SESSION['g']; 

       echo $room; // echo's room ?> 
     </p> 



     <p><span>Computer No: </span><?php 

       $select3 = $_POST['bike']; 
       echo $select3; 
       ?> 
     </p> 



     <p><span>Date: </span><?php $date = $_POST['datepicker']; 
       echo $date; // echo's date 
       ?> 
     </p> 



     <p><span>Start Session: </span> 
       <?php 
       if(isset($_POST['select1'])) { 
       $select1 = $_POST['select1']; 
       echo $select1; 
       echo ""; 
       } 
       else{ 
       echo "not set"; 
       } 
       ?> 
     </p> 

     <p><span>End Session: </span> 
       <?php 
       if(isset($_POST['select2'])) { 
       $select2 = $_POST['select2']; 
       echo $select2; 
       echo ""; 
       } 
       else{ 
       echo "not set"; 
       } 
       ?> 
     </p> 


     </div> 




     <div id="success"> 

<?php 

      $servername = "localhost"; 
      $name = "root"; 
      $password = "root"; 
      $dbname = "my computer"; 

      // Create connection 
      $conn = mysqli_connect($servername, $name, $password, $dbname); 
      // Check connection 
      if (!$conn) { 
       die("Connection failed: " . mysqli_connect_error()); 
      } 



$query = "SELECT * FROM `booked` WHERE 
     `date` = '{$date}' AND 
     `computer_id` = '{$select3}' AND 
     `start_time` = '{$select1}' AND 
     `end_time` = '{$select2}' AND 
     `room` = '{$room}' 
     "; 

      $result = mysqli_query($conn, $query); 



      if (mysqli_num_rows ($result) > 0) 
      { 

      header('Location: exist.php'); 
      die(); 

      } 




      else 
      { 
      $sql = "INSERT INTO booked (date, computer_id, name, start_time, end_time, room) 
       VALUES ('$date', '$select3', '$username', '$select1', '$select2', '$room')"; 



      if (mysqli_query($conn, $sql)) { 
       echo "New record created successfully"; 
       } else { 
       echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
       } 

      mysqli_close($conn); 

      } 
      ?> 

     </div> 



     <form action="user.php"> 
      <input type="submit" value="book another" class="bookanother" /> 
     </form> 



     </div> 


    </body> 




</html> 
+0

它不工作的原因是因爲'頭()'函數必須在之前的任何輸出發送到瀏覽器調用。 http://php.net/manual/en/function.header.php – mulquin

+0

嘗試移動標題作爲第一行,看看它的工作,否則按照我的答案,並嘗試一個JavaScript後備。如果您已經發送標題,通常會發生標題位置問題。 – Iceman

+0

[如何修復]可能的PHP頭文件已發送錯誤(http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Qirel

回答

2

如果頭已經發送,例如你有之前那麼頭將無法正常工作echo東西,因爲數據流開始後的頭不能被設置,(因爲PHP將已經設置默認標題爲你)。所以,在這種情況下,如果是這樣,我會使用javascript進行重定向。

PHP Docs

記住,標題()之前的任何實際的輸出是 發送,或者通過正常的HTML標記,在一個文件中的空行,或從PHP必須被調用。 使用include或require, 函數或其他文件訪問函數讀取代碼,並在調用header()之前輸出空格或空行 是非常常見的錯誤。使用單個PHP/HTML文件時存在相同的問題 。

WORK-AROUND:這是一個我已經寫了很久的函數,並且包含在控制器中。

 /** 
    * Safely redirect by first trying header method but if headers were 
    * already sent then use a <script> javascript method to redirect 
    * 
    * @param string 
    * @return null 
    */ 
    public function safeRedirect($new_url) { 
     if (!headers_sent()) { 
      header("Location: $new_url"); 
     } else { 
      echo "<script>window.location.href = '$new_url';</script>"; 
     } 
     exit(); 
    } 

添加功能,只需撥打:

safeRedirect('index.php');