使用php中包含文件中包含的文件(由另一个文件包含)中定义的变量

时间:2011-06-27 15:01:09

标签: php

假设我有三个具有以下代码的文件

文件1:firstfile.php

 
    $dbhost = "localhost";
    $dbname = "mydbname";
    $dbuser = "myuserid";
    $dbpass = "mypass";
    $dblink = mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_select_db($dbname,$dblink);

    $errorlogs = "c:/wamp/www/smstrunk/tmp/weblogs.log";
    $displaycount = 2;
    $querylimit = "LIMIT $displaycount";
    $maximportfilesize = 2097152;

文件2:secondfile.php

include("firstfile.php");

//Returns the GET/POST parameter value
    function getPassedParam($param){

        if(isset($_POST[$param])) $value = $_POST[$param];
        elseif(isset($_GET[$param])) $value = $_GET[$param];
        else $value = "";

        return $value;
    }
//##########################################

// DB Functions
    function selectSQL($qry, $dblink){

        global $errorlogs;

        $result = mysql_query($qry, $dblink);
        if (!$result) {
            log2File("Invalid query: " . mysql_error());
            return false;
        }
        log2File($qry);

        return $result;
    }

    function updateSQL($qry, $dblink){

        $result = mysql_query($qry, $dblink);
        if (!$result) {
            log2File("Invalid query: " . mysql_error());
            return false;
        }
        log2File($qry);

        return mysql_affected_rows($dblink);
    }

    function insertSQL($qry, $dblink){
            echo $displaycount;
        $result = mysql_query($qry, $dblink);
        if (!$result) {
            log2File("Invalid query: " . mysql_error());
            return false;
        }
        log2File($qry);

        return mysql_insert_id();

    }
//##########################################

//Mailing Functions
    function auto_reg_mail($to, $message){

        $subject = 'Registration Confirmation';
        $headers = 'From: noreply@smstrunk.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);
    }
//##########################################


//Logging Function
    function log2File($message, $file=NULL){

        global $errorlogs;
        if($file == NULL)$file = $errorlogs;

        $msg = date("Y-m-d H:m:s") . " [" . $_SERVER['PHP_SELF'] . "] $message \n";
        error_log($msg, 3, $file);
    }
//##########################################


// Application Specific Functions
//  1. Get customer id from msisdn
    function getCustID($msisdn, $lowner, $dblink, $alias = ""){

        //Check if msisdn already exists for this user
        $sql = sprintf("SELECT custid, mstatus FROM msisdn WHERE number = '%s' AND owner = '$lowner'", mysql_real_escape_string($msisdn));
        $rsmsisdn = selectSQL($sql, $dblink);

        if($rwmsisdn = mysql_fetch_assoc($rsmsisdn)){
            $custID = $rwmsisdn['custid'];
            $mstatus = $rwmsisdn['mstatus'];
            $sql = sprintf("UPDATE msisdn SET mstatus = 0, alias = '%s' WHERE custID = '$custID'", mysql_real_escape_string($alias));
            updateSQL($sql, $dblink);
        }else{
        // if not insert number
            $sql = sprintf("INSERT INTO msisdn (number, owner, alias) VALUES ('%s', '$lowner', '%s')", $msisdn, mysql_real_escape_string($alias));
            $custID = insertSQL($sql, $dblink);
        }
        return $custID;
    }

?>

文件3:third.php

include ("secondfile.php");

echo $dblink;

为什么$ dblink在我已经在firstfile.php中定义了它时给了我未定义的变量? php编译器/解释器是否允许有限级别的包含

1 个答案:

答案 0 :(得分:1)

  

[我]不是通过文件系统路径调用包含

include()获取一个文件,然后在其中执行PHP源代码。

如果您使用http://路径,则每个包含将在其自己的解析器实例中单独解释。这样,在一个包含中定义的变量将不会成为另一个包含的变量。

您需要使用文件系统路径(或使您的http包含 emit 源代码而不是执行它)。