Advertisement
gitlez

YA: Login Checks 20130617184009AAPPF2Y

Jun 18th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. <?php
  2. // Yahoo Answers' Question: http://answers.yahoo.com/question/index?qid=20130617184009AAPPF2Y
  3. include('includes/functions.php');
  4. session_start();
  5.  
  6. /***   Message Declaration   ***/
  7. $message = '';
  8.  
  9.  
  10. /***   Functions   ***/
  11. function pv($name){
  12.     $var = (isset( $_POST[$name] ))? trim( $_POST[$name] ) : '';
  13.     return (@mysql_ping())? mysql_real_escape_string( $var ) : addslashes( $var );
  14. }
  15.  
  16.  
  17. if( isset( $_POST['login'] ) ){
  18.     // Database Connection Needs to go Here.
  19.     // Should at the least be MySQLi Functions
  20.     // MySQL Functions are deprecated
  21.    
  22.     $username = pv('username');
  23.     $password = pv('password');
  24.    
  25.     if( isset($username{0}) && isset($password{0})){
  26.         // MD5 Hash Password.
  27.         $password = md5($password);
  28.         $stmt = "SELECT username FROM users WHERE username='{$username}' AND password='{$password}' LIMIT 1";
  29.         $result = mysql_query($stmt);
  30.         if( !$result ){
  31.             $message .= 'Query Statement Error: ' . mysql_error() . '<br />' . PHP_EOL;
  32.         }else if( mysql_num_rows($result) === 1){
  33.             // Username and password are correct.
  34.             $_SESSION['user'] = mysql_fetch_object($result)->username;
  35.             header('Location: index.php');
  36.             exit;
  37.         }else{
  38.             $message .= 'Username / Password Combo is incorrect. Please try again.<br />' . PHP_EOL;
  39.         }
  40.     }else{
  41.         $message .= 'Username and Password are both required fields.<br />' . PHP_EOL;
  42.     }
  43.    
  44. }
  45.  
  46. /***   Message Formatting   ***/
  47. if( isset($message{1}) ){
  48.     $message = '<div style="width: 500px;margin: auto;position: relative;border: 1px solid #999;">
  49.        <h3 style="margin: 0;padding: 4px;background-color: #999;color: #fff;">Message</h3>
  50.        <p style="padding: 8px;">' . $message . '</p>
  51.    </div>';
  52. }
  53.  
  54. include('login.php');
  55. ?>
  56.  
  57.  
  58. also For HTML;
  59.  
  60.  
  61. <?php
  62. session_start(); // Has to be called before any output.
  63. if( isset($_SESSION['user']) ){
  64.     header('Location: index.php');
  65.     exit;
  66. }
  67. ?>
  68. <html>
  69.     <head>
  70.         <title>Admin Area Login page</title>
  71.     </head>
  72.     <body>
  73.         <?php if(isset($message)){ echo $message; } ?>
  74.         <form action="dologin.php" method="POST">
  75.             <table>
  76.                 <tr>
  77.                     <td><span>Username:</span></td>
  78.                     <td><input type="text" name="username" /></td>    
  79.                 </tr>
  80.                 <tr>
  81.                     <td><span>Password:</span></td>
  82.                     <td><input type="password" name="password" /></td>    
  83.                 </tr>
  84.                 <tr>
  85.                     <td colspan="2" align="right"><input type="submit" name="login" value="Login" /></td>    
  86.                 </tr>
  87.             </table>
  88.         </form>
  89.     </body>
  90. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement