Link to home
Start Free TrialLog in
Avatar of OBCT
OBCT

asked on

Security function in a class

How can I add a function into my php class so that every page that requires the class, must have my website link and things like that otherwise an error prints out on the page???

Cheers

-OBCT
Avatar of Giovanni G
Giovanni G
Flag of Italy image

Can you think of a conceptual algorithm to solve your problem? I don't think so..
If i got you correctly, something like ob_start() on inclusion, and later (maybe using register_shutdown_function()) check with ob_get_contents(), if your link is somewhere inside there..should be what you want.
Also try striptags() to remove comments, so they don't add your link inside <!-- and -->, and other output buffer control functions to modify it..
Avatar of mixmastr
mixmastr

class yeah {

 function test() {
 echo "test";
 }

 function bla() {
 echo "bla";
 }
}

$my = new yeah;
$my->bla();

Think this should work.
Sorry, posted at the wrong post.

New to exepert exchange.
Avatar of OBCT

ASKER

ThG,

I am fairly new to php and using its functions...could you put it in a more simple manner, or send me in the direction of a tutorial that could help me work it out myself?

Cheers

-OBCT
OBCT: explain me again, more technically and more strictly what you want to do. Then, read carefully the man pages for each function i mentioned, and if you can't understand the man page go back to the basic syntax chapter.

http://www.php.net/ob_start
http://www.php.net/register_shutdown_function
Avatar of OBCT

ASKER

At the bottom of all my php pages, it has who wrote the script, the company name and copyright information. I want to be able to put a function in my class, that checks that the information is on all the pages, so if that text gets removed by a user, an error will print on the page saying that the page must contain the copyright information.
I had a look at those functions, then I checked the basic syntax but I don't know how I would use those functions in my scripts.

Cheers

-OBCT
how your code look like? i'll try to write a few proof of concept lines
Avatar of OBCT

ASKER

<?php

class DB
{
      
    var $dbhost = "localhost";      
      var $dblogin = "";
      var $dbpass = "";
      var $dbname = "";
      var $dblink;      
      var $queryid;      
      var $error = array();      
      var $record = array();            
      var $totalrecords;      
      var $last_insert_id;                        

      function get_dbhost()
    {
        return $this->dbhost;
    }

      function get_dblogin()
    {
        return $this->dblogin;
    }

      function get_dbpass()
    {
        return $this->dbpass;
    }

      function get_dbname()
    {
        return $this->dbname;
    }

      function set_dbhost($value)
      {
          return $this->dbhost = $value;
      }

      function set_dblogin($value)
    {
        return $this->dblogin = $value;
    }

      function set_dbpass($value)
    {
        return $this->dbpass = $value;
    }

      function set_dbname($value)
    {
        return $this->dbname = $value;
    }


      function DB($dblogin, $dbpass, $dbname)
    {
        $this->set_dblogin($dblogin);
            $this->set_dbpass($dbpass);
            $this->set_dbname($dbname);
    }

      function connect()
    {
        $this->dblink = @mysql_pconnect($this->dbhost, $this->dblogin, $this->dbpass);
            if(!$this->dblink)
            {
                  $this->return_error("Unable to connect to the database.");
            }
            $t = @mysql_select_db($this->dbname, $this->dblink);
            if(!$t)
            {
                  $this->return_error("Unable to change databases.");
            }
            return $this->dblink;

    }


      function disconnect()
    {
        if($this->dblink)
            {      
                  $test = @mysql_close($this->dblink);
                  if(!$test)
                  {
                        $this->return_error("Unable to close the connection.");
                  }
            }
            else
            {
                $this->return_error("No connection open.");
            }
            unset($this->dblink);
    }


      function return_error($message)
      {
            return $this->error[] = $message." ".mysql_error().".";
      }


      function showErrors()
    {
        if($this->hasErrors())
            {
                  reset($this->error);
                  $errcount = count($this->error);
                  echo "<p>Error(s) found: <b>'$errcount'</b></p>\n";

                  while(list($key, $val) = each($this->error))
                  {
                        echo "<li>$val</li><br>\n";
                  }
                  $this->resetErrors();
            }
    }


      function hasErrors()
    {
        if(count($this->error) > 0)
            {
                  return true;
            }
            else
            {
                  return false;
            }
    }


      function resetErrors()
    {
        if($this->hasErrors())
            {
                  unset($this->error);
                  $this->error = array();
            }
    }


      function query($sql)
    {
            if(empty($this->dblink))
            {      
                  $this->connect();
            }
        $this->queryid = @mysql_query($sql, $this->dblink);
            if(!$this->queryid)
            {
                  $this->return_error("Unable to perform the query <b>'$sql'</b>.");
            }
            return $this->queryid;
    }


      function fetchRow()
    {
            if(isset($this->queryid))
            {
              return $this->record = @mysql_fetch_array($this->queryid);
            }
            else
            {
                  $this->return_error("No query specified.");
            }
    }


      function fetchLastInsertId()
      {
            $this->last_insert_id = @mysql_insert_id($this->dblink);
            if(!$this->last_insert_id)
            {
                  $this->return_error("Unable to get the last inserted id from MySQL.");
            }
            return $this->last_insert_id;
      }


      function resultCount()
    {
        $this->totalrecords = @mysql_num_rows($this->queryid);
            if(!$this->totalrecords)
            {
                  $this->return_error("Unable to count the number of rows returned");
            }
            return $this->totalrecords;
    }


      function resultExist()
    {
            if(isset($this->queryid) && ($this->resultCount() > 0))
            {
                  return true;
            }
            return false;
    }


      function clear($result = 0)
    {
            if($result != 0)
            {
                  $t = @mysql_free_result($result);
                  if(!$t)
                  {
                        $this->return_error("Unable to free the results from memory");
                  }
            }
            else
            {
                  if(isset($this->queryid))
                  {
                        $t = @mysql_free_result($this->queryid);
                        if(!$t)
                        {
                              $this->return_error("Unable to free the results from memory (internal).");
                        }
                  }
                  else
                  {
                      $this->return_error("No SELECT query performed, so nothing to clear.");
                  }
            }
      }

}

?>
Modify your class as follows....

class DB {
......... lots of functions cut ..............

   function DB($dblogin, $dbpass, $dbname)
    {
          $this->set_dblogin($dblogin);
          $this->set_dbpass($dbpass);
          $this->set_dbname($dbname);

          // requires you to change the function def as follows  
          //      function DB($dblogin, $dbpass, $dbname, $hostname)
          if ($_SERVER["SERVER_NAME"] <> $hostname) {
             die('Invalid Domain Name');
          }          
         // or the slightly less portable, harder to detect outside the class
          if ($_SERVER["SERVER_NAME"] <> "www.example.com") {
             die('Invalid Domain Name');
          }          

          // check "other things like that"
          if (not something else that must be ) {
             die('Something else is missing');
          }
    }
.......... lots more cut ................
}

That should do it.
ASKER CERTIFIED SOLUTION
Avatar of Giovanni G
Giovanni G
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah I see - I was thinking you didn't want the script taken from your site.  To check for the existence of a link in the output, you will need to scan the output buffer as ThG pointed out.  His code should work fine.