php主机无法在LAN上正确重定向

时间:2013-03-16 04:10:52

标签: php redirect localhost

我有这段代码,保存在名为' functions.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访问它。但当然,当调用任何这些函数时,网站无法正确重定向。

提前致谢,

2 个答案:

答案 0 :(得分: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();,而不是简单地调用{{1}}。

希望有所帮助。

答案 1 :(得分: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();
}     
}

如果这样可行,则在所有其他功能中更改此设置 祝你好运!

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