Logowanie obiektowe
Prosty przyklad autoryzacji klasy i pare innych :) zobacz a nie pożałujesz
Listing
//class.login.php
class Login
{
/*****Zmienne bazy danych*****/
private $db;
private $db_host = 'localhost';
private $db_user = 'user';
private $db_pass = 'pass';
private $db_base = 'base';
/*****Zmienne sesji*****/
private $user;
private $rank;
/************************* M e t o d y k l a s y *************************/
function __get($var){
return $this->$var;
}
function __set($var, $val){
$this->$var = $val;
}
function __call($method, $arguments){
return false;
}
/************************* M e t o d y l o g o w a n i a *************************/
public function __construct(){
@ $db = new mysqli($this->db_host,$this->db_user, $this->db_pass,$this->db_base);
$this->db = $db;
if(mysqli_errno($db)){
return false;
}else{
return true;
}
}
public function zaloguj($name, $pass){
$zapytanie = "SELECT name, rank FROM users WHERE name = '". $name ."' AND password = '". sha1($pass)."'";
$wynik = $this->db->query($zapytanie);
$wiersz = $wynik->fetch_row();
$user = $wiersz[0];
$this->user = $user;
$this->rank = $wiersz[1];
if(isset($user)){
$this->sesja_start();
return true;
}else{
return false;
}
}
/************************* M e t o d y s e s j i *************************/
public function sesja_start(){
self::sesja_stop();
session_start();
if(!isset($_SESSION['zalogowany'])){
session_regenerate_id();
$_SESSION['zalogowany'] = true;
$_SESSION['name'] = $this->user;
$_SESSION['rank'] = $this->rank;
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
}
if($_SESSION['ip'] != $_SERVER['REMOTE_ADDR']){
self::sesja_stop();
}
}
public static function sesja_trwa(){
session_start();
if(!isset($_SESSION['zalogowany'])){
$new_sesja = new login();
$new_sesja->sesja_start();
}
if($_SESSION['ip'] != $_SERVER['REMOTE_ADDR']){
self::sesja_stop();
}
}
public static function sesja_stop(){ //wylogowywanie
session_start();
$stary_zalogowany=$_SESSION['zalogowany'];// przechowanie do sprawdzenie czy logowanie nastąpiło
$stary_name=$_SESSION['name'];
$stary_rank=$_SESSION['rank'];
unset($_SESSION['zalogowany'],$_SESSION['name'],$_SESSION['rank']);
session_destroy();
if(!empty($stary_zalogowany)){
return true;
}else{
return false;
}
}
/************************* M e t o d y r e j s t r a c j i *************************/
public function zarejstruj($name, $pass, $email){
if(isset($_SESSION['name']) == true){
self::sesja_stop();
}
$id = NULL;//auto_increment samo sobie zwiększy id
$pass = sha1($pass);
$date = date('Y-m-d');
$rank = 2;
$stmt = $this->db->prepare("insert into users values(?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssi", $id, $name, $pass, $date, $email, $rank);
if($stmt->execute()){
return true;
}else{
return false;
}
}
/************************* Z a m k n i ę c i e p o ł ą c z e n i a z b a z ą d a n y c h *************************/
final function __destruct(){
$stmt->free;
$db->close;
}
}
class Login
{
/*****Zmienne bazy danych*****/
private $db;
private $db_host = 'localhost';
private $db_user = 'user';
private $db_pass = 'pass';
private $db_base = 'base';
/*****Zmienne sesji*****/
private $user;
private $rank;
/************************* M e t o d y k l a s y *************************/
function __get($var){
return $this->$var;
}
function __set($var, $val){
$this->$var = $val;
}
function __call($method, $arguments){
return false;
}
/************************* M e t o d y l o g o w a n i a *************************/
public function __construct(){
@ $db = new mysqli($this->db_host,$this->db_user, $this->db_pass,$this->db_base);
$this->db = $db;
if(mysqli_errno($db)){
return false;
}else{
return true;
}
}
public function zaloguj($name, $pass){
$zapytanie = "SELECT name, rank FROM users WHERE name = '". $name ."' AND password = '". sha1($pass)."'";
$wynik = $this->db->query($zapytanie);
$wiersz = $wynik->fetch_row();
$user = $wiersz[0];
$this->user = $user;
$this->rank = $wiersz[1];
if(isset($user)){
$this->sesja_start();
return true;
}else{
return false;
}
}
/************************* M e t o d y s e s j i *************************/
public function sesja_start(){
self::sesja_stop();
session_start();
if(!isset($_SESSION['zalogowany'])){
session_regenerate_id();
$_SESSION['zalogowany'] = true;
$_SESSION['name'] = $this->user;
$_SESSION['rank'] = $this->rank;
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
}
if($_SESSION['ip'] != $_SERVER['REMOTE_ADDR']){
self::sesja_stop();
}
}
public static function sesja_trwa(){
session_start();
if(!isset($_SESSION['zalogowany'])){
$new_sesja = new login();
$new_sesja->sesja_start();
}
if($_SESSION['ip'] != $_SERVER['REMOTE_ADDR']){
self::sesja_stop();
}
}
public static function sesja_stop(){ //wylogowywanie
session_start();
$stary_zalogowany=$_SESSION['zalogowany'];// przechowanie do sprawdzenie czy logowanie nastąpiło
$stary_name=$_SESSION['name'];
$stary_rank=$_SESSION['rank'];
unset($_SESSION['zalogowany'],$_SESSION['name'],$_SESSION['rank']);
session_destroy();
if(!empty($stary_zalogowany)){
return true;
}else{
return false;
}
}
/************************* M e t o d y r e j s t r a c j i *************************/
public function zarejstruj($name, $pass, $email){
if(isset($_SESSION['name']) == true){
self::sesja_stop();
}
$id = NULL;//auto_increment samo sobie zwiększy id
$pass = sha1($pass);
$date = date('Y-m-d');
$rank = 2;
$stmt = $this->db->prepare("insert into users values(?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssi", $id, $name, $pass, $date, $email, $rank);
if($stmt->execute()){
return true;
}else{
return false;
}
}
/************************* Z a m k n i ę c i e p o ł ą c z e n i a z b a z ą d a n y c h *************************/
final function __destruct(){
$stmt->free;
$db->close;
}
}
Dodano przez: gieros Ranga: 0 Punktów: 0
Komentarze użytkowników
:: Losowe artykuły
:: Wymiana linków
Modowe inspiracje |
Android Gry i Aplikacje |
ZaplanujTransport.pl: Przeprowadzki, transport, aukcje |
Logo dla firmy |
Change Tires - Car Weather Forecast Reminder |
Laminas: MVC Framework for PHP |
IT Books Reviews and Programming: JS, JAVA, PHP, ANDROID, CSS |
Katalog roślin |
Programming articles: JAVA, PHP, C++, Python, JavaScript |
Kancelaria Adwokacka Łukasz Huszno