Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- if(isset($_GET['logout'])){
- unset($_SESSION['logged']);
- redirect();
- }else if(isset($_SESSION['logged'])){
- echo "<a href='?logout'>Logout</a>";
- }else if(isset($_POST['username']) && isset($_POST['password'])){
- //connect database
- db::connect() and die("Can't connect database!");
- db::query("SELECT * FROM db.users");
- $connected = false;
- if(db::$hasResults){
- while($row = db::$result->fetch_assoc()){
- if($row['username'] == $_POST['username'] && $row['password'] == $_POST['password']){
- $connected = true;
- break;
- }
- }
- }
- if($connected){
- //if logged create the session and redirect
- $_SESSION['logged'] = true;
- }
- //close the connection
- db::close();
- //redirect to lose the post refresh
- redirect();
- }else{
- //show login
- echo "
- <form action='' method='POST'>
- <input type='text' name='username' placeholder='Username...'><br>
- <input type='password' name='password' placeholder='Password...'><br>
- <input type='submit' value='Submit'>
- </form>
- ";
- }
- //redirect to self if url its empty
- function redirect($url="top.location.href"){
- echo "<script> top.location.href = ".$url.".split('?')[0]; </script>";
- exit;
- }
- class db{
- static $o;
- static $hostname = "localhost";
- static $username = "root";
- static $password = "";
- static $error = false;
- static $hasResults = false;
- static $result;
- static function connect($hostname="localhost",$username="root",$password=""){
- self::$hostname = $hostname;
- self::$username = $username;
- self::$password = $password;
- self::$o = new mysqli($hostname, $username, $password);
- self::$error = self::$o->connect_error;
- return self::$error;
- }
- static function close(){
- self::$o->close();
- }
- static function query($sql){
- self::$result = self::$o->query($sql);
- if(self::$result->num_rows > 0){
- self::$hasResults = true;
- }else{
- self::$hasResults = true;
- }
- return self::$hasResults;
- }
- static function error(){
- return self::$error;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement