[SOLVED]include/require bombs the app

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

[SOLVED]include/require bombs the app

Post by vaughtg »

In my application, I've got a number of include statements and they all seem to be working, save one. Or, rather, include statements that include a particular class file.

I have the following test file (as I wanted to see if it was something to do with the original file):

Code: Select all

<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
  <head>
  </head>
  <body>
<?php
   include '../classes/Input.php';
   
   $select = new Input();
   $valCol = "cityid";
   $labelCol = "cityname";
   $tableName = "cities";
   $andArray = array();
   $orArray = array();
   $sortOrder = "sort_order";
   $name = "city";
   $select->getSelect($valCol, $labelCol, $tableName, $andArray, $orArray, $sortOrder, $name);
   echo $select;
   
   if(isset($_SESSION['errorMsg'])){
      echo "Error: ".$_SESSION['errorMsg'];
      unset($_SESSION['errorMsg']);
   }
?>
	</body>
</html>
I hit a breakpoint at my include statement in this file:

Code: Select all

<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
  <head>
  </head>
  <body>
<?php
   include '../classes/Input.php'; // <<<<<<<<<<<<  BREAKPOINT SET HERE
   
   $select = new Input();
   $valCol = "cityid";
   $labelCol = "cityname";
   $tableName = "cities";
   $andArray = array();
   $orArray = array();
   $sortOrder = "sort_order";
   $name = "city";
   $select->getSelect($valCol, $labelCol, $tableName, $andArray, $orArray, $sortOrder, $name);
   echo $select;
   
   if(isset($_SESSION['errorMsg'])){
      echo "Error: ".$_SESSION['errorMsg'];
      unset($_SESSION['errorMsg']);
   }
?>
	</body>
</html>
And it gets me to here:

Code: Select all

<?php
   include_once 'DBConnection.php';// <<<<<<<<<<<<<<<<BREAKPOINT SET HERE
And there's a breakpoint at the same point in DBConnection.php that never gets hit.

I'm really new to PHP and how all this works in PHP, so I'm lost as to what it is about the include_once that causes this thing to die an ugly (and undocumented) death. There's nothing coming back to the console that helps me out. Just that the script terminated. The behavior I get from the browser is that it pops up a couple of dialogs offering me the options to Find, Save or Cancel the page.
Last edited by vaughtg on Tue Feb 22, 2011 1:31 pm, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: include/require bombs the app

Post by social_experiment »

The Manual wrote:The include() statement includes and evaluates the specified file. The documentation below also applies to require(). The two constructs are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. Be warned that parse error in included file doesn't cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does.
This is from the manual, it might be irrelevant but it explains a bit about the function.

It's probably some problem on the DBConnection page that is stopping the script from continuing. What does that page do and could you paste it's contents.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: include/require bombs the app

Post by vaughtg »

I can't see what it would be - as this was working wonderfully two days ago. I moved it to a test file and it all seems to work fine. DBConnection isn't reserved word, is it? If so, why did this work previously?

Code: Select all

<?php
   include 'db_constants.php';
   
   class DBConnection{
      private static $connection;
      private static $instantiated = FALSE;

      // Constructor
      public function __construct(){
         if(!DBConnection::$instantiated){
            $dsn = "oci:dbname=".ORA_HOST;
            $conn = new PDO($dsn, ORA_USER, ORA_PWD);
            if(!$conn){
                $e = oci_error();
                trigger_error(htmlentities($e['message'], ENT_QUOTES), 
                      E_USER_ERROR);
            }else{
               DBConnection::$connection = $conn;
               DBConnection::$instantiated = TRUE;
            }
         }
      }
      
      // Destructor
      public function __destruct(){
      }
      
      //Public functions
      public function getConnection(){
         if(DBConnection::$instantiated){
            return DBConnection::$connection;
         }else{
            $this->setErrorMsg("-Error getConnection:No connection to the database.");
         }
      }
      
      public function getDataVals($dataArray, $tableName, $andArray, $orArray, 
            $orderBy){
         $query = "SELECT ";
         for($i=0; $i<count($dataArray); $i++){
            if($i>0)$query.=", ";
            $query.= $dataArray[$i];
         }
         $query.=" FROM ".$tableName." ";
         if(0<count($andArray) || 0<count($orArray)){
            $query.=" WHERE ";
         }
         $andKeys = array_keys($andArray);
         for($j=0; $j<count($andArray); $j++){
            if($j>0)$query.=" AND ";
            $query.="$andKeys[j] = ?";
         }
         $orKeys = array_keys($orArray);
         for($k=0; $k<count($andArray); $k++){
            if($k>0)$query.=" OR ";
            $query.="$orKeys[$k] = ?";
         }
         if(isset($orderBy) && "" != $orderBy)$query.=" ORDER BY $orderBy";
         $rsArray = $this->getResultSet($query, $andArray, $orArray);
         return $rsArray;
      }

      public function getRowCount($query){
         $rs = $this->getCount($query);
         return $rs;
      }
      
      public function closeConnection(){
         DBConnection::$connection = null;
         DBConnection::$instantiated = FALSE;
      }
      
      public function insertRecord($query){
         $success = false;
         $conn = $this->getConnection();
         $statement = oci_parse($conn, $query);
         if(oci_execute($statement, OCI_COMMIT_ON_SUCCESS)){
            $success = true;
         }else{
            $this->setErrorMsg("- Database error:insertRecord<br />\nCode: ".
            		$err["code"]."<br>\nMessage: ".$err["message"]."<br />\nPosition: ".
                  $err["offset"]."<br />\nStatement: ".$err["sqltext"]."<br>");
            oci_rollback($conn);
         }
         return $success;
      }
      
      public function getNextId($sequence){
         $query = "select $sequence.nextval from dual";
         $dataVals = $this->getDataVals($query);
         return $dataVals[0][0];
      }

      //Private functions
   
      private function getResultSet($query, $andArray, $orArray){
         $conn = DBConnection::$connection;
         $stmt = $conn->prepare($query);
         $retArray = array();
         $bindParams = array();
         $andKeys = array_keys($andArray);
         for($j=0; $j<count($andArray); $j++){
            $key = $andKeys[$j];
            $value = $andArray[$j];
            $bindParams[] = $key;
            $bindParams[] = $value;
         }
         $orKeys = array_keys($orArray);
         for($k=0; $k<count($orArray); $k++){
            $key = $orKeys[$j];
            $value = $orArray[$j];
            $bindParams[] = $key;
            $bindParams[] = $val;
         }
         if($stmt->execute($bindParams)){
            while ($row = $stmt->fetch()) {
               foreach ($row as $value){
                  $retArray[] = $value;
               }
           }
         }else{
            $errInfo = $conn->errorInfo();
            $this->setErrorMsg("- Database error:getResultSet<br />\nSQLSTATE ".
            		"Error Code: ".$errInfo[0]."<br />\nError Code: ".$errInfo[1].
            		"<br />\nMessage: ".$errInfo[2]."<br />\n");
         }
         return $retArray;
      }

      private function getCount($query){
         if(!get_magic_quotes_gpc())$query = addslashes($query);
         $statement = oci_parse($this->getConnection(), $query);
         $rsid = oci_execute($statement);
         $tmp = oci_fetch_all($statement, $output);
         $count = oci_num_rows($statement);
         return $count;
      }

      private function setErrorMsg($err){
         $origErr = $_SESSION['errorMsg'];
         $_SESSION['errorMsg'] = $origErr.$err;
      }
   }
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: include/require bombs the app

Post by social_experiment »

vaughtg wrote: DBConnection isn't reserved word, is it?
No.
vaughtg wrote:I can't see what it would be - as this was working wonderfully two days ago.
What changes, if any, did you make? (To the script).
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: include/require bombs the app

Post by Jonah Bron »

Start by enabling error reporting. That will tell you exactly what the problem is.

http://nucleussystems.com/blog/enable-error-reporting
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: include/require bombs the app

Post by vaughtg »

@ social_experiment: I made some "sweeping, wholesale" changes to the code as I realized that I'd done NOTHING to prevent SQL injection - so I implemented prepared statements and had to completely restructure where and how my queries were generated. Actually, that's about it, but it touched every "class" file I have, at least once. Could be I have bigger issues, but right now it looks like I've got some kind of issue with the DBConnection.php file - as all other classes which reference it choke and die as soon as they hit the following line:

Code: Select all

   require_once 'DBConnection.php';
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: include/require bombs the app

Post by vaughtg »

Jonah Bron wrote:Start by enabling error reporting. That will tell you exactly what the problem is.

http://nucleussystems.com/blog/enable-error-reporting
Ah, many thanks here! As the first paragraph of the article states - I don't have it enabled because I didn't know how!

Turns out the error reporting was on, but it was not displaying on my page. I turned that on and was able to go to the issues. Seems that, despite what I was seeing, the interpretor was seeing 'DBDBConnection.php' - which didn't exist. Also, having restructured my query generation, I needed to accommodate for that in handling my result sets.
Post Reply