home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / simplog.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  2.4 KB  |  79 lines

  1. Simple, Full, PHP/MySQL based login screen 
  2.  
  3.  
  4. This is a simple PHP script that uses a MySQL table to store the login/password. I have included some extra code which is to be put into some sort of include file which is in turn to be included on every page you want to be protected (ie not viewable unless logged in via the login screen). Note: I know that this line: $msg = "<meta http-equiv=\"Refresh\" content=\"0;url=./mainpage.php\">"; is real lame but it's the only thing I could think of that is fairly compatable. The reason I did not use header() here is because using header() after setcookie() is not compatible with some versions of IE. 
  5.  
  6.  
  7.  
  8. <? 
  9. if($username && $password) { 
  10.   mysql_connect() or die ( "Whoops"); 
  11.   $password = md5($password); 
  12.   $sql =  "select * from login where username='$username'"; 
  13.   $r = mysql_db_query( "reg_users",$sql); 
  14.  
  15.   if(!mysql_num_rows($r)) { 
  16.     header( "Location: $SCRIPT_NAME"); 
  17.   } 
  18.   $user = mysql_fetch_array($r); 
  19.   if($user[ "password"] == $password) { 
  20.     $password = serialize($password); 
  21.     setcookie( "login_cookie", "$username $password"); 
  22.     $msg =  "<meta http-equiv=\"Refresh\" content=\"0;url=./mainpage.php\">"; 
  23.   }else{ 
  24.      header( "Location: $SCRIPT_NAME"); 
  25.   } 
  26. if($msg) echo $msg; 
  27. ?> 
  28. <html> 
  29. <title>Login to PHP Coders DB</title> 
  30. <body bgcolor="yellow" text="black"> 
  31. <form method="post" action=" <?echo $SCRIPT_NAME;?>"> 
  32.  
  33. <center><font size=+5><b>Welcome!</b></font></center> 
  34. <br> 
  35. <br> 
  36. <br> 
  37. <table cellspacing=0 cellpadding=0 width=320 align="center"> 
  38. <tr><td> 
  39. Username: 
  40. </td><td> 
  41. <input name="username" type="text" width=10> 
  42. </td></tr> 
  43. <tr><td> 
  44. Password: 
  45. </td><td> 
  46. <input name="password" type="password" width=10> 
  47. </td></tr> 
  48. <tr><td colspan=2 align="center"> 
  49. <input name="login" type="submit"> 
  50. </td></tr> 
  51. </table> 
  52. </form> 
  53. </html> 
  54.  
  55. <?  /* Here is that include file code */  ?> 
  56.  
  57. <? 
  58. if(!$login_cookie) 
  59.   header( "Location: ./login.php"); 
  60.  
  61. if($phpcoders) { 
  62.   mysql_connect() or die ( "Whoops"); 
  63.   $user = explode( " ", "$login_cookie"); 
  64.   $sql =  "select * from login where username='$user[0]'"; 
  65.   $r = mysql_db_query( "reg_users",$sql); 
  66.  
  67.   if(!mysql_num_rows($r)) { 
  68.     header( "Location: ./login.php"); 
  69.     mysql_free_result($r); 
  70.   } 
  71.  
  72.   $chkusr = mysql_fetch_array($r); 
  73.   mysql_free_result($r); 
  74.   if(unserialize($user[1]) != $chkusr[1]) 
  75.     header( "Location: ./login.php"); 
  76. ?>
  77.