Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author bailz777
- *
- *
- */
- class db_interface {
- public $ServerName = 'localhost:3306'; //hostname:port
- public $UserName = '******'; //mysql user
- public $Password = '******'; //mysql password
- public $DataBase = '******'; //database name
- public $Domain = 'test.com'; //Full domain name (no host)
- public $con = '';
- function __construct() {
- //on construction, we must ensure that the domain is registered in our system
- //first check if it was defined locally to avoid extra DataBase Work
- var_dump(defined('DOMAIN_REGISTERED'));
- if(!defined('DOMAIN_REGISTERED')) {
- $this->db_connect();
- $result = $this->validate_domain();
- if($result) {
- echo "<p>Domain Validated!!</p>";
- }
- $this->db_disconnect();
- }
- else {
- echo "<p>Domain Validated!!</p>";
- }
- }
- /**
- *
- */
- function __destruct() {
- }
- /**
- *
- * @param unknown_type $domain
- * @return boolean
- */
- private function validate_domain() {
- $constants = get_defined_constants();
- // return $this->con;
- // print_r($constants);
- var_dump(defined('DOMAIN_REGISTERED'));
- if(defined('DOMAIN_REGISTERED')) {//Check DOMAIN_REGISTERED to avoid unnecessary db work
- return TRUE;
- }
- elseif (!defined('DOMAIN_REGISTERED')) {//Check the domain is in the db
- echo '<p>Domain was not locally registered, checking DataBase</p>';
- $query = "SELECT `name` FROM `$this->DataBase`.`registered_domains` WHERE `name` = '$this->Domain'";
- $result = mysql_query($query,$this->con);
- //var_dump($result);
- if(!$result) {
- die('No result found : ' . mysql_error());
- }
- elseif (mysql_num_rows($result)==0) { //if no rows returned, then domain is not in DataBase
- $domain_exists = FALSE;
- }
- elseif (mysql_num_rows($result)>0) { //if rows returned, then domain is in DataBase
- $domain_exists = TRUE;
- //If a domain does not exist, a mysql will be passed, use @ to suppress the error
- //The domain will be written to the db and on the next run of this function, the
- //constant will be defined
- }
- if($domain_exists) {//If it exists Then assign CONSTANT DOMAIN_REGISTERED to TRUE
- echo '<p>Domain Found in DataBase</p>';
- echo '<p>Registering domain locally</p>';
- define("DOMAIN_REGISTERED", TRUE);
- if(DOMAIN_REGISTERED) {
- echo '<p>Successfully registered domain locally</p>';
- }
- //var_dump(defined('DOMAIN_REGISTERED'));
- //echo DOMAIN_REGISTERED;
- return TRUE;
- }
- elseif(!$domain_exists) {//If it does not exist then add it to the registered_domains table, and assign CONSTANT __DOMAIN_TRUE__ to TRUE
- echo '<p>Domain not found in DataBase</p>';
- echo '<p>Now Registering Domain</p>';
- $query = "INSERT INTO `$this->DataBase`.`registered_domains` (`name`) VALUES ('$this->Domain')";
- $result = mysql_query($query);
- if(!$result) {
- die('Domain not added : ' . mysql_error());
- }
- else{
- define("DOMAIN_REGISTERED", TRUE);
- return TRUE;
- }
- }
- }
- }
- //Connect to mysql and define the active database
- private function db_connect() {
- $this->con = $con = mysql_connect($this->ServerName,$this->UserName,$this->Password);
- if (!$con) {
- die('Could not connect: ' . mysql_error());
- }
- else {
- echo 'Successfully connected to MySQL<br />';
- //define active database
- $this->db = mysql_select_db($this->DataBase);
- if(!$this->db) {
- die('Could not connect to Database : ' . mysql_error());
- }
- else {
- echo 'Successfully connected to DataBase<br />';
- }
- }
- }
- //disconnect from mysql
- private function db_disconnect() {
- $close = mysql_close($this->con);
- if($close) {
- echo 'Successfully disconnected from MySQL<br />';
- }
- }
- public function add_record($fname,$lname,$email) {
- $ip = $_SERVER['REMOTE_ADDR'];
- $authorized_date = time();
- }
- }
- ?>
- <?php
- class pizza
- {
- const SIZE_SMALL = 1;
- const SIZE_MEDIUM = 2;
- const SIZE_LARGE = 3;
- public function getCookingTime($size)
- {
- if ($size == self::SIZE_SMALL) {
- $time = 10;
- } else if ($size == self::SIZE_MEDIUM || $size == self::SIZE_LARGE) {
- $time = 15;
- }
- return $size;
- }
- }
- $pizza = new pizza();
- $time = $pizza->getCookingTime(3);
- // or more usefull:
- $time = $pizza->getCookingTime(pizza::SIZE_SMALL);
- ?>
- class mydb
- {
- private $host;
- private $database;
- ..
- public function __construct($host, $database ..)
- {
- $this->host = $host;
- $this->database = $database;
- }
- }
- $mydb = new mydb('localhost', 'mydatabase', ..);
- class mydb
- {
- private $host;
- private $database;
- ..
- public function __construct($host, $database ..)
- {
- $this->host = $host;
- $this->database = $database;
- }
- public function Register( $domainname )
- {
- //connect to the database and do register stuff
- }
- }
- $mydb = new mydb('localhost', 'mydatabase', ..);
- $mydb->Register('MyDomain');
Add Comment
Please, Sign In to add comment