php package Dcore author Alexey Shpilka Стартуем SESSION session_start

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* @package Dcore
* @author Alexey Shpilka
*/
/* Стартуем SESSION */
session_start();
/* Путь корневого каталога */
define("ROOT", $_SERVER["DOCUMENT_ROOT"].'/');
/* Домен */
define("DOMAIN", 'test1.ru');
/* Выводим ip HTTP_X_FORWARDED_FOR для Nginx , REMOTE_ADDR для apache */
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = preg_replace('|[^0-9\.]|', '', $_SERVER['HTTP_X_FORWARDED_FOR']);
} else if (isset($_SERVER['REMOTE_ADDR'])) { $ip = preg_replace('|[^0-9\.]|', '', $_SERVER['REMOTE_ADDR']); }
/* Выводим браузер */
if (isset($_SERVER['HTTP_USER_AGENT'])) { $ua = htmlspecialchars($_SERVER['HTTP_USER_AGENT']); }
/* Подключение к базе данных */
include_once ROOT."core/db.php";
/* Классы для работы PDO */
class PDO_ extends PDO {
function __construct($dsn, $username, $password) {
parent :: __construct($dsn, $username, $password);
$this -> setAttribute(PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);
$this -> setAttribute(PDO :: ATTR_DEFAULT_FETCH_MODE, PDO :: FETCH_ASSOC);
}
function prepare($sql) {
$stmt = parent :: prepare($sql, array(
PDO :: ATTR_STATEMENT_CLASS => array('PDOStatement_')));
return $stmt;
}
function query($sql, $params = array()) {
$stmt = $this -> prepare($sql);
$stmt -> execute($params);
return $stmt;
}
function querySingle($sql, $params = array()) {
$stmt = $this -> query($sql, $params);
$stmt -> execute($params);
return $stmt -> fetchColumn(0);
}
function queryFetch($sql, $params = array()) {
$stmt = $this -> query($sql, $params);
$stmt -> execute($params);
return $stmt -> fetch();
}
}
class PDOStatement_ extends PDOStatement {
function execute($params = array()) {
if (func_num_args() == 1) {
$params = func_get_arg(0);
} else {
$params = func_get_args();
}
if (!is_array($params)) {
$params = array($params);
}
parent :: execute($params);
return $this;
}
function fetchSingle() {
return $this -> fetchColumn(0);
}
function fetchAssoc() {
$this -> setFetchMode(PDO :: FETCH_NUM);
$data = array();
while ($row = $this -> fetch()) {
$data[$row[0]] = $row[1];
}
return $data;
}
}
class DB {
static $dbh;
public function __construct() {
try {
self :: $dbh = new PDO_('mysql:host=' . DBHOST . ';port=' . DBPORT . ';dbname=' . DBNAME, DBUSER, DBPASS);
self :: $dbh -> exec('SET CHARACTER SET utf8');
self :: $dbh -> exec('SET NAMES utf8');
}
catch (PDOException $e) {
die('К сожалению, не доступен сервер mySQL.');} }
final public function __destruct() {
self :: $dbh = null;
}
} $database = new DB();
/* Автоматическая загрузка классов */
function Autoload($class_name) {
@include_once (ROOT.'core/classes/'.strtolower($class_name).'.class.php');
} spl_autoload_register('Autoload');
/* Авторизация */
if (isset($_COOKIE['sid'])) {
/* Обработка sid */
$sid = system::check($_COOKIE['sid']);
/* Проверяем существует ли такой пользователь */
$data = DB :: $dbh -> queryFetch("SELECT * FROM `user` WHERE `sid`=? LIMIT 1;", array($sid));
/* Проверяем данные */
if ($data['sid'] == $sid) {
/* Обновляем данные */
DB :: $dbh -> query("UPDATE `user` SET `ip`=? WHERE `id`=? LIMIT 1;", array($ip, $data['id']));
} else {
setcookie("sid", "", 0, "/");
header ("Location: /");
exit();
}
}
?>