2014-09-03 56 views
-3

我正在處理數據庫,我需要在登錄過程後將用戶重定向到另一個頁面。php redirect_to()函數undefined

我正在使用redirect_to()函數。在代碼執行後,它給我的錯誤

Fatal error: Call to undefined function redirect_to()

這裏是我試過

if($result){ 
     $num_rows = mysqli_num_rows($result); 

     if($num_rows == 1){ 

     $found_user=mysqli_fetch_array($result); 
     redirect_to("main.php"); 
     } 

     else{ 
     redirect_to("index.php"); 
     } 
} 
+2

這意味着'redirect_to'不存在。 – 2014-09-03 11:57:48

+0

redirect_to不是PHP方法,如果您沒有在任何地方定義該函數,那麼它不會存在...你在哪裏得到這個功能從?! – 2014-09-03 11:58:31

+2

這個名稱沒有內置函數。如果你自己寫了一個,它超出範圍 – Steve 2014-09-03 11:58:46

回答

1

你必須調用它之前定義的redirect_to功能。試試這個代碼

<?php 
     if($result){ 
      $num_rows = mysqli_num_rows($result); 
      if($num_rows == 1){ 
       $found_user=mysqli_fetch_array($result); 
       redirect_to("main.php"); 
      } 
      else{ 
       redirect_to("index.php"); 
      } 
     } 
     function redirect_to($location){ 
      header('Location:'.$location); 
     } 
?> 
1

嘗試使用頭:

<?php 
/* Redirection vers une page différente du même dossier */ 
$host = $_SERVER['HTTP_HOST']; 
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); 
$extra = 'mypage.php'; 
header("Location: http://$host$uri/$extra"); 
exit; 
?> 
0

redirect_to()不是一個函數。你想用header("Location: main.php");

由於這是在加入標頭信息,它需要的事情之前被傳遞到頁面(我相信),所以不要試圖header()調用之前打印任何東西了頁面加載。

如果你想這樣做,我建議使用JavaScript。

0

redirect_to不存在,或者如果存在,它不會被正確包含/要求。

加入此;

if(function_exists('redirect_to') == FALSE) { //Future proofing... 
    function redirect_to($where) { 
     header('Location: '. $where); 
     exit; 
    } 
} 

如果你已經有輸出,我勸你考慮使用output buffering.

0

您可以通過下面的頭容易重定向(「地點:....)語法:

header("Location: main.php"); 
exit;