Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Настройки сайта
- -----------------------------------------------------------------------------*/
- define( 'TEST_MODE', true );
- define( 'SITE_ADDRESS', 'soc.tt' );
- define( 'SITE_NAME', 'Social network' );
- define( 'SITE_EMAIL', 'mail@soc.tt' );
- define( 'EXPIRE_COOKIE', 3600*24*32 );
- /* Установки
- -----------------------------------------------------------------------------*/
- session_start();
- ini_set( "display_errors", TEST_MODE );
- date_default_timezone_set( "Asia/Yekaterinburg" );
- /* Автозагрузка классов
- -----------------------------------------------------------------------------*/
- function __autoload($name) {
- include_once('classes/' . $name . '.php');
- }
- /* База Данных
- -----------------------------------------------------------------------------*/
- class DataBase {
- const host = 'localhost';
- const dbName = 'soc';
- const user = 'root';
- const password = '';
- public $db;
- private $attr = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
- private static $instance;
- private function __construct(){
- $this->db = new PDO('mysql:host='.self::host.';dbname='.self::dbName, self::user, self::password, $this->attr);
- }
- private function __clone(){}
- private function __wakeup(){}
- private function __destruct(){
- $this->db = null;
- }
- public static function getInstance() {
- if ( is_null(self::$instance) ) {
- self::$instance = new self();
- }
- return self::$instance;
- }
- }
- /* Сессия и куки
- -----------------------------------------------------------------------------*/
- class Session {
- public $data;
- public $auth = false;
- public function __construct(){
- if(!empty($_SESSION['key0']) || !empty($_SESSION['key1']) || !empty($_SESSION['save']))
- if(!empty($_SESSION['key0']) && !empty($_SESSION['key1']) && !empty($_SESSION['save'])){
- $this->data=DataBase::getInstance()->db->query('SELECT * FROM `users` WHERE `id`=\''.$_SESSION['key0'].'\' AND `key`=\''.$_SESSION['key1'].'\'');
- $this->data=$this->data->fetch();
- if(!empty($this->data[0])){
- $_SESSION['save']==1?$this->createCookie():$this->deleteCookie();
- $this->auth = true;
- }
- else {
- $this->deleteSession();
- $this->deleteCookie();
- }
- }
- else {
- $this->deleteSession();
- $this->deleteCookie();
- }
- else
- if(!empty($_COOKIE['key0']) || !empty($_COOKIE['key1']))
- if(!empty($_COOKIE['key0']) && !empty($_COOKIE['key1'])){
- $this->data=DataBase::getInstance()->db->query('SELECT * FROM `users` WHERE `id`=\''.$_COOKIE['key0'].'\' AND `key`=\''.$_COOKIE['key1'].'\'');
- $this->data=$this->data->fetch();
- if(!empty($this->data[0])){
- $this->createSession(1);
- $this->createCookie();
- $this->auth = true;
- }
- else
- $this->delete();
- }
- else
- $this->delete();
- else
- $this->delete();
- }
- public function login($login, $password, $save){
- $this->data=DataBase::getInstance()->db->query('SELECT * FROM users WHERE (nick=\''.$login.'\' OR email=\''.$login.'\') AND password=\''.$password.'\'');
- $this->data=$this->data->fetch();
- if(!empty($this->data[0])){
- if($save){
- $this->createCookie();
- $this->createSession(1);
- }
- else
- $this->delete();
- $this->auth = true;
- }
- else
- $this->delete();
- }
- private function createSession($save=0) {
- $_SESSION['key0'] = $this->data['id'];
- $_SESSION['key1'] = $this->data['key'];
- $_SESSION['save'] = $save;
- }
- private function deleteSession() {
- $_SESSION['key0'] = '';
- $_SESSION['key1'] = '';
- $_SESSION['save'] = '';
- }
- private function createCookie() {
- $key0=$this->data['id'];
- $key1=$this->data['key'];
- $_COOKIE['key0']=$key0;
- $_COOKIE['key1']=$key1;
- setcookie("key0",$key0,time()+EXPIRE_COOKIE,'/');
- setcookie("key1",$key1,time()+EXPIRE_COOKIE,'/');
- }
- private function deleteCookie() {
- $key0='';
- $key1='';
- $_COOKIE['key0']=$key0;
- $_COOKIE['key1']=$key1;
- setcookie("key0",$key0,time()+EXPIRE_COOKIE,'/');
- setcookie("key1",$key1,time()+EXPIRE_COOKIE,'/');
- }
- public function delete() {
- $this->deleteSession();
- $this->deleteCookie();
- }
- }
- $u = new Session();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement