-1

「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;
}

ウェブサイト自体をブラウジングするとき、これらは正しく機能せず、ウェブサイト内のリンクを介してしか移動できませんでしたlocalhost. .php") 私が開発していたとき)。

ここでいくつかの啓蒙が必要です。私はこの Web サイトを LAN 経由で立ち上げており、接続されている人は xxx.xxx.xxx.x/YMMLS 経由でアクセスできます。しかしもちろん、これらの関数のいずれかが呼び出されると、Web サイトは適切にリダイレクトされません。

前もって感謝します、

4

2 に答える 2

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();ます。

お役に立てば幸いです。

于 2013-03-16T04:26:20.847 に答える
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

于 2013-03-16T04:26:23.900 に答える