开发者

Using variables defined in an included file (included by another file) in the including file in php

开发者 https://www.devze.com 2023-03-16 03:24 出处:网络
Let\'s say I have three files that have the following code File 1: firstfile.php $dbhost = \"localhost\";

Let's say I have three files that have the following code

File 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;

File 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 开发者_Go百科== 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;
    }

?>

File 3: third.php

include ("secondfile.php");

echo $dblink;

Why is $dblink giving me undefined variable when I have already defined it in firstfile.php? Does the php compiler/interpreter allow limited levels of include


[I] am not calling includes through a file system path

include() fetches a file, and then executes the PHP source code in it.

If you are using a http:// path, every include will get interpreted separately, in its own parser instance. That way, variables defined in one include will not make it to the other.

You need to use filesystem paths (or make your http includes emit source code instead of executing it).

0

精彩评论

暂无评论...
验证码 换一张
取 消