Ver Mensaje Individual
  #4 (permalink)  
Antiguo 15/02/2010, 06:49
Nisrokh
 
Fecha de Ingreso: septiembre-2009
Ubicación: Neuquén
Mensajes: 142
Antigüedad: 14 años, 6 meses
Puntos: 12
Respuesta: [APORTE] Sistema de Sesiones y Autentificación de usuario con MySQL

Nota: Para poder utilizar las clases de alli arriba, deben utilizar una para MySQL que yo mismo he hecho... Si despues quiren pueden adaptarlo a la suya... Pero el codigo fue hecho con la utilizacion de la siguiente clase para MySQL... Bien "Simple".

SimpleMySQL.php
Código PHP:
Ver original
  1. <?php
  2.  
  3. /**
  4.  * @author estilodg.com <[email protected]>
  5.  * @copyright 2009
  6.  */
  7.  
  8. /**
  9.  * MySQL Management Class
  10.  *
  11.  * @author Diego P. M. Baltar <www.estilodg.com>
  12.  * @package SimpleMySQL
  13.  */
  14. class SimpleMySQL
  15. {
  16.     /**
  17.      * Link identifier
  18.      *
  19.      * @access public
  20.      * @var resource
  21.      */
  22.     public $link = null;
  23.    
  24.     /**
  25.      * Selected database
  26.      *
  27.      * @access public
  28.      * @var string
  29.      */
  30.     public $database = null;
  31.    
  32.     /**
  33.      * Query result resource
  34.      *
  35.      * @access public
  36.      * @var resource
  37.      */
  38.     public $result = null;
  39.    
  40.     /**
  41.      * Query result data
  42.      *
  43.      * @access public
  44.      * @var array
  45.      */
  46.     public $query_result = array();
  47.    
  48.     /**
  49.      * Query error
  50.      *
  51.      * @access public
  52.      * @var boolean
  53.      */
  54.     public $query_error = false;
  55.    
  56.     /**
  57.      * Selected rows
  58.      *
  59.      * @access public
  60.      * @var integer
  61.      */
  62.     public $rows = null;
  63.    
  64.     /**
  65.      * Number of affected rows
  66.      *
  67.      * @access public
  68.      * @var integer
  69.      */
  70.     public $affected_rows = null;
  71.    
  72.     /**
  73.      * Last inserted id
  74.      *
  75.      * @access public
  76.      * @var integer
  77.      */
  78.     public $last_id = null;
  79.    
  80.     /**
  81.      * MySQL connection state
  82.      *
  83.      * @access public
  84.      * @var boolean
  85.      */
  86.     public $ready = false;
  87.    
  88.     /**
  89.      * Database tables
  90.      */
  91.     public $tables = array();
  92.    
  93.     public function __construct($hostname = 'localhost', $username = 'root', $password = '')
  94.     {
  95.         return $this->connect($hostname, $username, $password);
  96.     }
  97.    
  98.     public function __destruct()
  99.     {
  100.         return true;
  101.     }
  102.    
  103.     /**
  104.      * MySQL connect
  105.      *
  106.      * @param string $hostname MySQL server address/ip(port)
  107.      * @param string $username MySQL username
  108.      * @param string $password MySQL password
  109.      * @return boolean
  110.      */
  111.     private function connect($hostname,$username,$password)
  112.     {
  113.         $this->link = @mysql_connect($hostname, $username, $password, true);
  114.         if (!$this->link) return exit(mysql_error($this->link));
  115.         else return $this->ready = true;
  116.     }
  117.    
  118.     /**
  119.      * MySQL select database
  120.      *
  121.      * @param string $database MySQL database name to use
  122.      * @return boolean
  123.      */
  124.     public function select_db($database = '')
  125.     {
  126.         if (!$this->link) return exit(mysql_error($this->link));
  127.         else if (!@mysql_select_db($database)) {
  128.             $this->ready = false;
  129.             return exit(mysql_error($this->link));
  130.         }
  131.         else $this->database = $database;
  132.         return true;
  133.     }
  134.    
  135.     /**
  136.      * MySQL query
  137.      *
  138.      * @param string $sentence MySQL query sentence
  139.      * @return integer Number of selected rows
  140.      */
  141.     public function query($sentence = '')
  142.     {
  143.         if (!$this->link) return exit(mysql_error($this->link));
  144.         $this->result = @mysql_query($sentence, $this->link);
  145.         if (!$this->result) {
  146.             $this->query_error = true;
  147.             return exit(mysql_error($this->link));
  148.         }
  149.        
  150.         $this->affected_rows = mysql_affected_rows($this->link);
  151.        
  152.         if (preg_match('/^\s*(insert|replace)(.+)/is',$sentence))
  153.             $this->last_id = mysql_insert_id($this->link);
  154.        
  155.         if (preg_match('/^\s*(select)(.+)/is', $sentence)) {
  156.             if ($this->affected_rows > 0) {
  157.                 $rows = 0;
  158.                 while ($row = mysql_fetch_object($this->result)) {
  159.                     $this->query_result[$rows] = $row;
  160.                     $rows++;
  161.                 }
  162.             }
  163.         }
  164.        
  165.         @mysql_free_result($this->result);
  166.        
  167.         if ($this->query_error) $this->query_error = false;
  168.        
  169.         $this->rows = $rows;
  170.         return true;
  171.     }
  172.    
  173.     /**
  174.      * Clean cached query result
  175.      *
  176.      * @access public
  177.      * @return void
  178.      */
  179.     public function clean()
  180.     {
  181.         $this->query_error = false;
  182.         $this->query_result = array();
  183.         $this->affected_rows = null;
  184.     }
  185.    
  186.     /**
  187.      * Espaces a string
  188.      *
  189.      * @access public
  190.      * @param string $string
  191.      * @return string
  192.      */
  193.     public function escape($string)
  194.     {
  195.         if (!$this->link) return exit(mysql_error($this->link));
  196.         $string = stripslashes($string);
  197.         return @mysql_real_escape_string($string, $this->link) ;
  198.     }
  199. }
  200.  
  201. ?>