Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- require_once("class.DB.php");
- class Security {
- public $message = "";
- public function __construct(){}
- public function register(){
- if($_SERVER['REQUEST_METHOD'] === 'POST'){
- if(isset($_POST['login']) && isset($_POST['pass']) && isset($_POST['repeat'])){
- $login = htmlspecialchars($_POST['login']);
- $pass = $_POST['pass'];
- $repeat = $_POST['repeat'];
- $db = new DB();
- $t = $db->query("select * from users where username=\"$login\"");
- if(count($t) > 0){
- $this->message = "Twój login jest zajęty!";
- return;
- }
- if($pass != $repeat){
- $this->message = "Hasła muszą być takie same!";
- return;
- }
- if(strlen($login) < 4){
- $this->message = "Login musi zawierac przynajmniej 4 znaki";
- return;
- }
- if(strlen($pass) > 4){
- $db->myquery("insert into users(username, password) values(\"$login\", \"$pass\")");
- $this->message = "Użytkownik $login zostal utworzony.<br>Za chwile zostaniesz przekierowany do logowania!";
- header("refresh:2, /login.php");
- }else{
- $this->message = "Hasło musi składać się z przynajmniej 5 znaków!";
- return;
- }
- }
- }
- }
- public function login(){
- if($_SERVER['REQUEST_METHOD'] === 'POST'){
- if(isset($_POST['username']) && isset($_POST['password'])){
- $username = $_POST['username'];
- $password = $_POST['password'];
- $db = new DB();
- $t = $db->query("select * from users where username=\"$username\"");
- if(count($t) == 1){
- if($t[0]['password'] == $password){
- $_SESSION['username'] = $username;
- $this->message = "Zalogowales się poprawnie";
- }else{
- $this->message = "Zły login lub hasło";
- }
- }else{
- $this->message = "Zły login lub hasło";
- }
- }
- }
- }
- public function logout(){
- unset($_SESSION['username']);
- }
- public function isLogin(){
- return isset($_SESSION['username']);
- }
- public function checkLogin(){
- if(!$this->isLogin()){
- header("Location: login.php");
- die();
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement