Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- error_reporting(0);
- $db = mysqli_connect("127.0.0.1", "root", "", "mytest");
- /*Fill in values if not set*/
- if( !( isset($_SESSION["logged_in"]) && isset($_SESSION["active_user"]) ) )
- {
- $_SESSION["logged_in"] = 0;
- $_SESSION["active_user"] = "None";
- }
- /*Awesome shorthand logic*/
- $postUser = ((isset($_POST["user"]) && !empty(trim($_POST["user"]))) ? true : false);
- $postPass = ((isset($_POST["pass"]) && !empty(trim($_POST["pass"]))) ? true : false);
- /*Make mysql query, and if both values return true, rows will equal 1*/
- if($postUser && $postPass)
- {
- $mysqlPostUser = mysqli_real_escape_string($db, $_POST["user"]);
- $mysqlPostPass = mysqli_real_escape_string($db, $_POST["pass"]);
- if((mysqli_num_rows(mysqli_query($db, "SELECT * FROM logins WHERE username='{$mysqlPostUser}' AND password='{$mysqlPostPass}'"))) > 0)
- {
- $_SESSION["logged_in"] = 1;
- $_SESSION["active_user"] = $_POST["user"];
- }
- else
- {
- $_SESSION["logged_in"] = 0;
- $_SESSION["active_user"] = "nope";
- }
- header("Location: /");
- }
- /*Prevents browser asking for resubmit if only one value is submitted*/
- if( $postUser xor $postPass )
- header("Location: /");
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Login Test</title>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <style type="text/css">
- html, body
- {
- margin:0px;
- padding:0px;
- }
- html {overflow-y: scroll;}
- body
- {
- padding:50px;
- }
- .text
- {
- font-family: Verdana;
- font-size:16px;
- font-weight: normal;
- }
- .box
- {
- box-sizing: border-box;
- display: block;
- }
- .inline
- {
- box-sizing: border-box;
- display: inline-block;
- }
- #wrapper
- {
- width:100%;
- max-width:280px;
- border:1px dashed gray;
- padding:10px;
- margin:auto;
- }
- form
- {
- border-bottom:1px solid lightgray;
- padding-bottom:10px;
- }
- .inputs
- {
- /*border:2px solid blue;*/
- }
- .row
- {
- /*border:1px solid green;*/
- height:40px;
- line-height: 40px;
- }
- .left {float:left;text-align: left;width:40%;}
- .right {float:right;text-align: right;width:60%;}
- input[type="text"], input[type="password"]
- {
- width:100%;
- padding:4px;
- border-radius: 3px;
- border:1px solid #ccc;
- }
- </style>
- </head>
- <body>
- <div id="wrapper" class="box">
- <form class="box text" method="post" action="">
- <div class="box inputs">
- <div class="left row box text">Username: </div>
- <div class="right row box"><input class="inline text" type="text" name="user" autocomplete="off" placeholder="..." onfocus="this.removeAttribute('readonly');" readonly /></div>
- <br style="clear:both">
- </div>
- <div class="box inputs">
- <div class="left row box text">Password: </div>
- <div class="right row box"><input class="inline text" type="password" name="pass" autocomplete="off" placeholder="..." onfocus="this.removeAttribute('readonly');" readonly /></div>
- <br style="clear:both">
- </div>
- <input type="submit" value="Login" style="float:right;margin-top:5px" class="text">
- <br style="clear:both">
- </form>
- <div class="box text" style="padding-top:5px">
- <?php echo "Logged in:" . $_SESSION["logged_in"] . "<br>" . "Current User:" . $_SESSION["active_user"];?>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement