Advertisement
Jelbs

Login

Dec 6th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.17 KB | None | 0 0
  1. <?php
  2. session_start();
  3. include('db.php');
  4. include("simple-php-captcha.php");
  5. $captchasession =  $_SESSION['captcha']['code'];
  6. $_SESSION['captcha'] = simple_php_captcha();
  7. $_SESSION['cart'] = '';
  8. $username = $_POST['username'];
  9. $password = $_POST['password'];
  10. $type_user = $_POST['type_user'];
  11. ?>
  12. <html>
  13.     <head>
  14.         <meta name="viewport" content="width=device-width, initial-scale=1">
  15.         <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  16.         <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
  17.         <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
  18.         <meta http-equiv="cache-control" content="max-age=0" />
  19.         <meta http-equiv="cache-control" content="no-cache" />
  20.         <meta http-equiv="expires" content="0" />
  21.         <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
  22.         <meta http-equiv="pragma" content="no-cache" />
  23.         <title>Login</title>
  24.     </head>
  25.     <body>
  26.         <?php  
  27.              
  28.             if($_POST) {
  29.                 $message = '';
  30.                 $username = htmlspecialchars($_POST['username']); // sanitize post data from dangerous caracters
  31.                 $password = htmlspecialchars($_POST['password']);
  32.                 $captcha = htmlspecialchars($_POST['captcha']);
  33.                 $type_user = htmlspecialchars($_POST['type_user']);
  34.                 if (empty($username) || empty($password)) {
  35.                     $message .= "Missing Username or password";
  36.                 } elseif (strtolower($captchasession) != strtolower($captcha))  {
  37.                     $message .= "Captcha failed";
  38.                 } else {
  39.                     try{
  40.                         $host = '127.0.0.1';
  41.                         $dbname='shop';
  42.                         $user='root';
  43.                         $pass='';
  44.                         $DBH = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
  45.                         $q = $DBH->prepare("SELECT * FROM logindetails WHERE username = :username and password = :password LIMIT 1 and type_user= :type_user");
  46.                         $q->bindValue(':username', $username);
  47.                         $q->bindValue(':password', hash('sha512', $password)); // hashing clear txt password on a fly with sha512 (most common hash for passwords in ind standard)
  48.                         $q->bindValue(':type_user', $type_user);
  49.                         $q->execute();
  50.                         $check=$q->fetch(PDO::FETCH_ASSOC);
  51.                         $message = "";
  52.                         if(!empty($check))
  53.                         {
  54.                                 $username = $check['username'];
  55.                                 $password = $check['password'];
  56.                                 $type_user = $check['type_user'];
  57.                                 $message='Logging in';
  58.                                 $_SESSION['username'] = $check['username'];
  59.                                
  60.                                     if($type_user == '1')
  61.                                     {
  62.                                         echo '<script> window.location="admin.php"; </script>';
  63.                                     }
  64.                                     if($type_user == '2')
  65.                                     {
  66.                                         echo '<script> window.location="staff.php"; </script>';
  67.                                     }
  68.                                     if($type_user == '3')
  69.                                     {
  70.                                         echo '<script> window.location="delivery.php"; </script>';
  71.                                     }
  72.                                     if($type_user == '4')
  73.                                     {
  74.                                         echo '<script> window.location="costumer.php"; </script>';
  75.                                     }      
  76.                         }else {
  77.                             $message="Sorry your details are incorrect";
  78.                            
  79.                         }
  80.                     }catch(PDOException $e){echo "error: ${e}";}
  81.                    
  82.                 }
  83.             }
  84.         ?>
  85.         <div data-role="page" >
  86.           <div data-role="header">
  87.                <h1>Building Supplies</h1>
  88.           </div>
  89.         <div data-role="navbar">
  90.             <ul>
  91.                 <li><a href="main.php" class="ui-btn ui-icon-back ui-btn-icon-left">Return</a></li>
  92.                 <li><a href="main.php" class="ui-btn ui-icon-home ui-btn-icon-left">Home</a></li>
  93.                 <li><a href="products.php" class="ui-btn ui-icon-info ui-btn-icon-left">Products</a></li>
  94.             </ul>
  95.         </div>
  96.        
  97.         <h2>Login</h2><br>
  98.        
  99.         <form action="?" method="POST">
  100.             Username <input type="text" name="username" /><br>
  101.             Password <input type="password" name="password" /><br>
  102.             <img src="<?= $_SESSION['captcha']['image_src'] ?>" ><br>
  103.             Securuty token <input type="text" name="captcha" />
  104.             <fieldset class="ui-grid-a">
  105.                 <div class="ui-block-a"><button type="submit" data-theme="c">Submit</button></div>
  106.                 <div class="ui-block-b"><button type="reset" data-theme="c">reset</button></div>       
  107.             </fieldset>
  108.                
  109.         <?php
  110.             if(!empty($message)){
  111.                 echo "<br>$message";
  112.             }
  113.         ?>
  114.         </form>
  115.        
  116.         <br>
  117.        
  118.         <div data-role="footer">
  119.                 <h1>JelbsWorks 2016</h1>
  120.         </div>
  121.        
  122.     </body>
  123. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement