2013-03-16 73 views
-1
正確重定向

我有這樣的一段代碼,保存在文件中被稱爲「functions.php的」PHP主機沒有局域網

$host = $_SERVER['HTTP_HOST']; 
$folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\'); 

function redirect_to_home(){ /*function to redirect the user ot his profile page, or  admin page if he is an administrator*/ 
if(admin_class::isadmin()){  
header("Location:http://$host$folder//adminindex.php"); 
exit(); 
} 
else{ 
header("Location:http://$host$folder/profile.php"); 
exit(); 
}  
} 

function redirect_to_welcome(){ 
header("Location:http://$host$folder/index.php"); 
exit; 
} 

function redirect_to_loan(){ 
header("Location:http://$host$folder/loan.php"); 
exit; 
} 

當通過網站本身進行瀏覽,這些不正確工作,我可以只是通過網站內的鏈接導航,我已經完成了整個事情(當我正在開發時,我只是使用了標題(「位置:http:// localhost /YMMLS//pagename.php」))。

我在這裏需要一些啓發,我通過局域網啓動本網站,連接的人可以通過xxx.xxx.xxx.x/YMMLS訪問它。但是,當這些功能被調用時,網站無法正確重定向。

由於提前,

+0

你試過用'local IP'代替'localhost'嗎? – 2013-03-16 04:12:48

+0

附加註釋NetBeans 7.2.1在包含$ host和$ folder變量的代碼行中顯示一條警告,它將它們標記爲聲明中的「未使用」,並且在使用時似乎「未初始化」,儘管它們位於SAME文件中。打破我的想法 – 2013-03-16 04:13:23

+0

@JamieAnacleto其確定netbeans顯示給你,因爲$ _SERVER ['一些關鍵']值沒有明確地初始化在您的代碼 – kirugan 2013-03-16 04:14:59

回答

0

NetBeans被警告你,因爲變量的聲明功能。 它們不在函數的範圍內。當然,你可以調用這個函數 - 從函數外部,那些變量存在 - 但這是不同的。 :)

這裏 - 試試這個;

class redirect { 

    public $host = ''; 
    public $folder = ''; 

    public function __construct() { 
     $this->host = $_SERVER['HTTP_HOST']; 
     $this->folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\'); 
    } 

    public function home() { /*function to redirect the user ot his profile page, or admin page if he is an administrator*/ 
     if(admin_class::isadmin()){ 
      header("Location: http://$host$folder/adminindex.php"); 
     } else { 
      header("Location: http://$host$folder/profile.php"); 
     } 
     exit(); 
    } 

    public function welcome() { 
     header("Location: http://$host$folder/index.php"); 
     exit; 
    } 

    public function loan() { 
     header("Location: http://$host$folder/loan.php"); 
     exit; 
    } 
} 

與其說只是redirect_to_home();這會通過redirect::home();被調用。

希望有所幫助。

0

試試這個

host = $_SERVER['HTTP_HOST']; 
$folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\'); 

function redirect_to_home(){ 

global $host, $folder;  

if(admin_class::isadmin()){  
    header("Location:http://$host$folder//adminindex.php"); 
    exit(); 
} 
else{ 
header("Location:http://$host$folder/profile.php"); 
exit(); 
}  
} 

,如果這個工程,然後改變這個在所有其它功能 GOOD LUCK !!

文檔http://php.net/manual/en/language.variables.scope.php

+0

這似乎已經解決了這個問題,通過QQ,MSN也試圖您的解決方案:) 函數頭($頁){$ 主機= $ _ SERVER [「HTTP_HOST」]; $ dirname($ _ SERVER ['PHP_SELF']),'/ \\'); $ folder = rtrim(dirname($ _ SERVER ['PHP_SELF']),'/ \\'); header(「Location:http://$host$folder/$page.php」); } – 2013-03-16 04:35:56

+0

@JamieAnacleto你的解決方案也是正確的,但在這種情況下,你必須在每個函數中聲明$ host和$ folder – alwaysLearn 2013-03-16 04:41:26