Advertisement
gitlez

YA: Login Checks 20130618122444AAd3LfO

Jun 18th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.77 KB | None | 0 0
  1. <?php
  2. // Yahoo Answers' Question: http://answers.yahoo.com/question/index?qid=20130618122444AAd3LfO
  3.  
  4. session_start();
  5. $message = '';
  6.  
  7. if( isset($_POST['login']) ){
  8.     $username = addslashes( strip_tags( trim( $_POST['username'] ) ) ); // Although you should be using your databases escaping function
  9.     $password = addslashes( strip_tags( trim( $_POST['password'] ) ) );
  10.  
  11.     if( isset($username{0}) && isset($password{0}) ){
  12.         // No Reason to waste the connection, unless it is actually needed.
  13.         require_once( 'dbConnect.php' );
  14.        
  15.         // MD5 Hash the password
  16.         $password = md5($password);
  17.        
  18.         $stmt = "SELECT userName,active,email FROM user WHERE userName='{$username}' AND password='{$password}' LIMIT 1";
  19.         $result = mysql_query($stmt);
  20.        
  21.         if( !$result ){
  22.             $message .= 'Internal Error: ' . mysql_error() . '<br />';
  23.         }else if( mysql_num_rows($result) === 0){
  24.             // Never tell someone that the username is correct, while the password
  25.             // is incorrect. It simply opens yourself up to brute force attacks.
  26.             $message .= 'Username/Password combo is incorrect. Please try again.';
  27.         }else{
  28.             $row = mysql_fetch_assoc($result); // Only one row returned, no need for a while loop for one.
  29.             $active = (int)$row['active'];
  30.             $email = $row['email'];
  31.             if( $active === 0 ){
  32.                 // Never show a full email address, when the viewer might not be the owner of the address.
  33.                 // This will show the user the first three characters of the username, then filled with '*',
  34.                 // then the domain. Enough for someone who knows, to know which of their addresses they
  35.                 // registered with (Privacy).
  36.                 list( $eUser, $domain) = explode( '@', $row['email'], 2); // Although it doesn't account for all emails, it does account for 99.99% of email addresses.
  37.                 $len = strlen($eUser);
  38.                 $eUser = substr($eUser, 0, 3) . str_pad('', ($len - 3), '*');
  39.                 $email = $eUser . '@' . $domain;
  40.                 $message .= "You haven't activated your account, Please check ($email) to activate this account.<br />";
  41.             }else{
  42.                 $_SESSION['username'] = $row['username'];
  43.                 header('Location: login.php'); // This might not be the page you wanted.
  44.                 exit;
  45.             }
  46.         }
  47.     }else{
  48.         $message .= 'Both Username and Password are required.';
  49.     }
  50. }
  51.  
  52. if( isset($message{1})){
  53.     $message = '<p style="font-weight: bold;">' . $message . '</p>' . PHP_EOL;
  54. }
  55.  
  56. if( isset($_SESSION['username']) ){
  57.     echo "You are logged in, " . $_SESSION['username'] . ". <a href='logout.php'>Log out</a>";
  58. }else{
  59.     echo '
  60.    <div id="login-wrapper" class="png_bg">
  61.        <div id="login-top">
  62.            <img title="Greeny Logo" alt="Greeny Logo" src="images/Logo.png" />
  63.        </div>
  64.        ' . $message . '
  65.        <div id="login-content">
  66.            <form method="post" action="login.php">
  67.                <p>
  68.                    <label>Username</label>
  69.                    <input class="text-input" type="text" name="username" />
  70.                </p>
  71.                <br style="clear: both;">
  72.                <br />
  73.                <p>
  74.                    <label>Password</label>
  75.                    <input class="text-input" type="password" name="password" />
  76.                </p>
  77.                <br style="clear: both;">
  78.                <br />
  79.                <p>
  80.                    <input class="button" type="submit" value="Sign In" name="login" />
  81.                </p>
  82.            </form>
  83.        </div>
  84.    </div>
  85.    <div id="dummy"></div>
  86.    <div id="dummy2"></div>';
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement