crawl bot
<?php /********************************************** DbConn.class.php * Author:antlou * Version:1.0 * Date:05/12/2015* Verson PHP 5 *********************************************/session_start();class db_connect { private $conn; private $server; private $user; private $pass; private $db; private $store_session; public function __construct ($server, $user, $pass, $db=false, $store_session=false){ $this->server = $server; $this->user = $user; $this->pass = $pass; $this->db = $db; $this->store_session = $store_session; } private function showError($text){ #USE CLASS ERROR IF YOU WANT PERSONALIZE THE MESSAGE echo "<b class='error'> ERRO : </b>"; echo $error; } public function connect(){ if(!$this->db){ $this->conn = mysqli_connect ($this->server, $this->user, $this->pass); } else { $this->conn = mysqli_connect ($this->server, $this->user, $this->pass, $this->db); } if(!$this->conn){ self::showError ($this->server .' using '. $this->user .' as user, verify the data and try again.'); } else { if($this->store_session){ self::store(); } else { return $this->conn; } } } private function store(){ $_SESSION["connection"] = $this->conn; } public function disconnect(){ if(isset($_SESSION["connection"])){ $_SESSION["connection"] = false; } mysqli_close($this->conn); } ?>
<?php /********************************************** DbConn.php * Author:antlou * Version:1.0 * Date:05/12/2015* Verson PHP 5 *********************************************/session_start();require_once ("dbconnect.class.php"); /*I create a simple connect without specify the db to connect*/$simple = new db_connect("host", "user", "pass"); #we receive the connection variable$simple_con = $simple->connect(); echo 'Simple connect sucess!'; #we make our sql statements and procedures as normal #blabla blabla$simple->disconnect(); //create connection specifying the data base$database = new db_connect("host", "user", "pass", "my_db");$database_con = $database->connect();echo 'Connect to Db my_db.'; #our sql statements #blabla blabla$database->disconnect(); /*if we want to save the connection in a session variable we use:*/$s = new db_connect("host", "user", "pass", "my_db", true); /*we don't receive the connection variable, the session is stored in:*/ //$_SESSION["connection"]$s->connect();echo 'Connect to DB my_db with session';print_r($_SESSION); //our sql statements #blabla blabla /*the session variable value is now false and the connection is closed */$s->disconnect(); ?>