Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Creating the Login Form:
- <form name="login" action="login.php" method="post">
- Username: <input type="text" name="username" size="30" maxlength="255" /><br/>
- Password: <input type="password" name="password" size="30" maxlength="255" /><br/>
- <input type="submit" value="Login" />
- </form>
- Creating the login.php file:
- The login.php file will check to see that the username and password that have been entered into the form are valid. If they are, we will set a cookie with their information. This script assumes you are storing your passwords in the database using SHA1. Here is what login.php will look like:
- <?php
- //include the database connection here...
- //convert username to uppercase... make it non-case sensitive.
- //then use the MD5 algorithm on the password
- $username = strtoupper($_POST['username']);
- $password = SHA1($_POST['password']);
- //query to run to database. also converts username to uppercase.
- //counts how many times the username and password match (will either be 0 or 1)
- $query = "select * FROM users WHERE
- UPPER(username) = '$username' AND
- password = '$password'";
- $result = mysql_query($query);
- $count = mysql_num_rows($result);
- if ($count > 0)
- {
- //the username and password match. we can now store the userid and password in the cookie.
- //we can use this for subsequent pages to figure out who the user is.
- $result = mysql_fetch_array($result);
- $userId = $result['userid'];
- //set the cookie
- setcookie("userId", $userId, time()+60*60*24*30);
- setcookie("userPass", $password, time()+60*60*24*30);
- //let them know they're logged in now.
- echo 'You are now logged in!';
- }
- else
- {
- //something wasn't right, so we can't log them in.
- echo 'Either your username or password were not correct. Try again.';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment