Advertisement
gitlez

Untitled

Jul 6th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.97 KB | None | 0 0
  1. <?php
  2. session_start(); // Sessions need to be started before any output and whitespace, to use $_SESSION variables.
  3. function processPassword($i){ // Once you have hashed passwords with this function, do not alter it, as it will produce different results, which will result in incorrect passwords.
  4.     return sha1(md5($i));
  5. }
  6.  
  7. $host = "localhost"; // Host name
  8. $username = "****"; // Mysql username
  9. $password = "********"; // Mysql password
  10. $db_name = "mammoth8_user"; // Database name
  11. $tbl_name = "members"; // Table name
  12.  
  13. // Connect to server and select databse.
  14. $conn = mysql_connect($host, $username, $password) or die("cannot connect"); // Storing the link is needed to close the connection when the script is complete. Or the queries are complete on long scripts. Closing the connection allows for system resources to be freed for other scripts.
  15. mysql_select_db($db_name, $conn) or die("cannot select DB");
  16.  
  17. // To protect MySQL injection (more detail about MySQL injection)
  18. if(get_magic_quotes_gpc()){ // returns true if Magic Quotes are turned on on your server
  19.     $uname = mysql_real_escape_string(stripslashes($_POST['uname']));
  20.     $upass = processPassword(stripslashes($_POST['upass']));
  21. }else{
  22.     $uname = mysql_real_escape_string($_POST['uname']);
  23.     $upass = processPassword($_POST['upass']);
  24. }
  25.  
  26. $result = mysql_query("SELECT * FROM {$tbl_name} WHERE username='{$uname}' AND password='{$upass}'");
  27. if(mysql_num_rows($result) > 0){
  28.     // Register $uname, $upass and redirect to file "login_success.php"
  29.     // session_start() was included at the top of the script. To access the contents, simply refere to it.
  30.     $_SESSION['uname'] = $uname;
  31.     // Not a good idea to store the password or even the hash of the password in session variables. Session hijacking is possible.
  32.     header("Location: login_success.php");
  33. }else {
  34.     echo "Wrong Username or Password"; // Excellent! Never tell the user that the password is wrong for that username.
  35. }
  36. mysql_close($conn);
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement