Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.17 KB | None | 0 0
  1.  
  2. <html>
  3. <head>
  4. <link rel="stylesheet" href="layout.css" type="text/css"/>
  5. </head>
  6. <body>
  7.  
  8. <div id="logon">
  9. <h1>Login</h1>
  10. <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
  11. <div><label>Username</label><input type="text" name="l_username" /><br /></div>
  12. <div><label>Password</label><input type="password" name="l_password" /><br /></div>
  13. <div><input type="hidden" name="action" value="login">
  14. <div><input type="submit" value="Login" /></div>
  15. </form>
  16. <div>
  17.  
  18. <hr />
  19.  
  20. <h1>Change Password</h1>
  21. Following rules are enforced:
  22. <ul>
  23. <li>Must not be one of your <em>five</em> previous passwords
  24. <li>Must be at least 7 characters long, contain <em>ALL</em> of the following: uppercase, lowercase, digits, special characters ( ][$^?!+*()@&pound;|\ )
  25. <!-- Expire Date, uncomment below line (remove the <!--) -->
  26. <li>Your password will expire after 70 days.
  27. </ul>
  28. <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
  29. <div><label>Username</label><input type="text" name="c_username" /><br /></div>
  30. <div><label>New Password</label><input type="password" name="c_new_password" /><br /></div>
  31. <div><label>Old Password</label><input type="password" name="c_old_password" /><br /></div>
  32. <div><input type="hidden" name="action" value="change">
  33. <div><input type="submit" value="Change Password" /></div>
  34. </form>
  35.  
  36. <hr />
  37.  
  38. <?php
  39. // database stuff
  40. /*
  41. $db_host = "localhost";
  42. $db_user = "root";
  43. $db_pass = "";
  44. $db_name = "test";
  45. */
  46.  
  47. $db_host = 'studentnet.kingston.ac.uk';
  48. $db_user = 'k0614087';
  49. $db_pass = 'password';
  50. $db_name = 'db_k0614087';
  51.  
  52. $conn = mysql_connect ($db_host, $db_user, $db_pass) or die ('MySQL connect failed. ' . mysql_error());
  53. mysql_select_db($db_name) or die('Cannot select database. ' . mysql_error());
  54.  
  55. if(isset($_POST['action']))
  56. {
  57.     if($_POST['action'] == 'login')
  58.     {
  59.         // do login
  60.         if(isset($_POST['l_username']) && strlen($_POST['l_username']) > 0 && isset($_POST['l_password']) && strlen($_POST['l_password']) > 0)
  61.         {
  62.             $q = "Select * from passwords where username = '" . $_POST['l_username'] . "' order by timestamp desc";
  63.             //echo $q . "<br />";
  64.             $result = mysql_query($q) or die(mysql_error());
  65.             $num = mysql_num_rows($result);
  66.             if($num == 0)
  67.             {
  68.                 echo "Username not found"; 
  69.             }
  70.             if($num > 0)
  71.             {
  72.                 // check password  
  73.                 $row = mysql_fetch_assoc($result);
  74.                 //echo $row['password'] . " " . $_POST['l_password'] . "" . $row['timestamp'] . "<br />";
  75.                 if(strcmp($row['password'],$_POST['l_password']) == 0)
  76.                 {
  77.                     // Expire Date, uncomment this (remove the /* and */)
  78.  
  79.                     $number_of_days = 70;
  80.                     $expire_time = (24 * 60 * 60) * $number_of_days;
  81.                         //echo "Current time: " . time() . " : " . date("Y-m-d G:H:s",time()) . "<br />";
  82.                         //echo "Passwod time: " . strtotime($row['timestamp']) . " : " . date("Y-m-d G:H:s",strtotime($row['timestamp'])) . "<br />";
  83.                     $a = strtotime($row['timestamp']);
  84.                     $a = $a + $expire_time;
  85.                         //echo "Pwd time +7d: " . $a . " : " . date("Y-m-d G:H:s",strtotime($row['timestamp']) + (7 * 24 * 60 * 60)) . "<br />";
  86.                    
  87.                    
  88.                     if(time() > $a)
  89.                     {
  90.                         echo "Password expired. Please change before logging in";  
  91.                     }
  92.                     else
  93.                     {
  94.                         echo "You are logged in";  
  95.                     }
  96.                 }
  97.                 else
  98.                 {
  99.                     echo "Your password was incorrect";
  100.                 }
  101.             }
  102.         }
  103.         else
  104.         {
  105.             echo "Please enter both a username and a password to login";
  106.         }
  107.     }
  108.    
  109. // split action methods here
  110.  
  111. if($_POST['action'] == 'change')
  112.     {
  113.         if(isset($_POST['c_username']) && strlen($_POST['c_username']) > 0 && isset($_POST['c_old_password']) && strlen($_POST['c_old_password']) > 0 && isset($_POST['c_new_password']) && strlen($_POST['c_new_password']) > 0)
  114.         {
  115.             // check old password and username match first
  116.             $q = "Select * from passwords where username = '" . $_POST['c_username'] . "' order by timestamp desc";
  117.             $result = mysql_query($q) or die(mysql_error());
  118.             $num = mysql_num_rows($result);
  119.             if($num == 0)
  120.             {
  121.                 if($_POST['c_old_password'] == "NEW_USER")
  122.                 {
  123.                         $q = "Insert into passwords (username,password) values ('" . $_POST['c_username'] . "','" . $_POST['c_new_password'] . "')";   
  124.                         $result = mysql_query($q) or die(mysql_error());
  125.                         echo "New user added.";
  126.                 }
  127.                 else
  128.                 {
  129.                     echo "Username not found"; 
  130.                 }
  131.             }
  132.             if($num > 0)
  133.             {
  134.                 // check old password  
  135.                 $row = mysql_fetch_assoc($result);
  136.                 if(strcmp($row['password'],$_POST['c_old_password']) == 0)
  137.                 {
  138.                     $password_score = 0;
  139.                     $isValid = false;
  140.                     // check password complexity
  141.                         // length
  142.                         $minimum_length = 7;
  143.                         if(strlen($_POST['c_new_password']) => $minimum_length)
  144.                         {
  145.                             $password_score = $password_score + 1; 
  146.                             //echo $password_score . " Pass min length requirement<br />"; 
  147.                         }      
  148.                         // upper case
  149.                             // if new password does not equal new password all lower case,
  150.                             // there must have been uppercase! add 1 to score
  151.                         if($_POST['c_new_password'] != strtolower($_POST['c_new_password']))
  152.                         {
  153.                             $password_score = $password_score + 1; 
  154.                             //echo $password_score . " Pass upper case requirement<br />";
  155.                         }
  156.                         // lower case
  157.                             // similar explanation to above
  158.                         if($_POST['c_new_password'] != strtoupper($_POST['c_new_password']))
  159.                         {
  160.                             $password_score = $password_score + 1; 
  161.                             //echo $password_score . " Pass lower case requirement<br />";
  162.                         }
  163.                         // specials, this will match ][$^?!+*()@£|\
  164.                         if(preg_match('{[][$^?!+*()@£|\\]]}',$_POST['c_new_password']))
  165.                         {
  166.                             $password_score = $password_score + 1;
  167.                             //echo $password_score . " Pass specials requirement<br />";   
  168.                         }
  169.                        
  170.                         // digits
  171.                         if(preg_match('#[0-9]#',$_POST['c_new_password']))
  172.                         {
  173.                             $password_score = $password_score + 1; 
  174.                             //echo $password_score . " Pass digit requirement<br />";
  175.                         }
  176.                        
  177.                         // yes or no this password
  178.                         if($password_score >= 5)
  179.                         {
  180.                             $isValid = true;   
  181.                         }  
  182.                         else
  183.                         {
  184.                             echo "Sorry, your password does not meet the minmum requirements<br />";   
  185.                         }              
  186.                     // check password isn't same as last 5
  187.                         $result = mysql_query($q) or die(mysql_error());
  188.                         for($i = 0; $i < $num; $i++)
  189.                         {
  190.                             $row = mysql_fetch_assoc($result);
  191.                             $p = "";
  192.                             similar_text($row['password'], $_POST['c_new_password'], $p);
  193.                             //echo $i . ":" . $num . " " . $p . " " . $row['password'] . " " . $_POST['l_password'] . "" . $row['timestamp'] . "<br />";
  194.                             if($p > 70)
  195.                             {
  196.                                 echo "Your new password is too similar to a previous one.<br />";
  197.                                 $isValid = false;
  198.                             }      
  199.                         }
  200.                     // check to make sure history is less than 5
  201.                         // delete oldest if it isn't
  202.                         //echo $num . "<br />";
  203.                         if($num > 4)
  204.                         {
  205.                             echo "Trimming password history<br />";
  206.                             $q = "Select * from passwords where username = '" . $_POST['c_username'] . "' order by timestamp asc";
  207.                             $result = mysql_query($q) or die(mysql_error());
  208.                             $row = mysql_fetch_assoc($result);
  209.                             $q = "Delete from passwords where timestamp = '" . $row['timestamp'] . "' and username = '" . $row['username'] . "'";
  210.                             //echo $q;
  211.                             $result = mysql_query($q) or die(mysql_error());
  212.                         }
  213.                    
  214.                     // set new password
  215.                     if($isValid)
  216.                     {
  217.                         $q = "Insert into passwords (username,password) values ('" . $_POST['c_username'] . "','" . $_POST['c_new_password'] . "')";   
  218.                         //echo $q . "<br />";
  219.                         $result = mysql_query($q) or die(mysql_error());
  220.                         echo "Password change successful";
  221.                     }
  222.                 }
  223.                 else
  224.                 {
  225.                     echo "Your password was incorrect";
  226.                 }
  227.             }
  228.         }
  229.         else
  230.         {
  231.             echo "Please enter your username, old password and new password to change your password";
  232.         }      
  233.     }
  234. }
  235.  
  236. ?>
  237.  
  238. <hr />
  239.  
  240. <?php
  241. if($_POST['action'] == 'login')
  242. {
  243.     echo "L_USER: " . $_POST['l_username'] . "<br />";
  244.     echo "L_PASS: " . $_POST['l_password']. "<br />";
  245. }
  246. if($_POST['action'] == 'change')
  247. {
  248. echo "USERNAME: " . $_POST['c_username']. "<br />";
  249. echo "OLD_PASS: " . $_POST['c_old_password']. "<br />";
  250. echo "NEW_PASS: " . $_POST['c_new_password']. "<br />";
  251. }
  252.  
  253. ?>
  254. </body>
  255. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement