Re: predefined variables

From: Date: Fri, 09 Jun 2000 02:20:29 +0000
Subject: Re: predefined variables
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-1359@lists.php.net to get a copy of this message
At 10:12 PM 6/8/00 -0700, Evan Raskob wrote:
Hi all, I am trying to write a function for use in a mysql generic database class that traps bad queries and saves tham to the database. It saves the bad query, and automatically generated error id#, and suppoedly the script that generated the error and the http request that was sent to the server. It is part of a class that I include from another file - the only problem is, I don't want to have to explicitly pass the name of the calling script, or the apache http variables. But, when I look at what gets saved, I see null strings for $PHP_SELF, $HTTP_SELF, and $REMOTE_ADDR but the rest of the error has been saved. Is there some kind of global declaration I can use, or am I just confused about the scope of those variables when the function is called from an included file? Anyone?
        function db_die($code) {
          $this->connect();
                $query = "insert into errors (script, code, error_date) values
('$PHP_SELF', 'request($HTTP_REQUEST) from IP($REMOTE_ADDR): $code', ".mktime().")";
                mysql_query($query, $this->link_id) or die("Could not die properly,
$query!");
                $error_num = mysql_insert_id($this->link_id);
                $this->close();
                die ("Error: $error_num.  Please email
<mailto:\"evan@talksofa.com\">evan@talksofa.com with the error number");
        }
Hi Evan, Functions operate in local name space. This means that they do not have access to any variables that are not created inside the function or that are passed to the function as arguments. To get around this, you need to let your function know that you want to use the Global versions of $PHP_SELF, $HTTP_SELF, etc.. using the global keyword. Adding this line to your function should do the trick. global $PHP_SELF, $HTTP_SELF, $REMOTE_ADDR; You can also use the special $GLOBALS array to access the values. Instead of using $PHP_SELF use $GLOBALS['PHP_SELF'] HTH, Zak

« previous php.general (#1359) next »