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
LVL 9
OBCTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThGCommented:
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..
0
mixmastrCommented:
class yeah {

 function test() {
 echo "test";
 }

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

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

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

New to exepert exchange.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

OBCTAuthor Commented:
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
0
ThGCommented:
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
0
OBCTAuthor Commented:
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
0
ThGCommented:
how your code look like? i'll try to write a few proof of concept lines
0
OBCTAuthor Commented:
<?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.");
                  }
            }
      }

}

?>
0
petoskey-001Commented:
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.
0
ThGCommented:
No I disagree.

OBCT, I think your file is being used someway like "include yourfile.php" from the third parties scripts, and your object's instance is created in the earlier steps of execution.
So I think it would go like this:

ob_start();
function check_sig() {
  if (!strstr(ob_get_contents(), "http://your_host/credits.htm")) {
     ob_clean();
     print "Error: you must give credits to my beautiful work!";
  }
}
register_shutdown_function("check_sig");

class DB {
...
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petoskey-001Commented:
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.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.