Posted By

AlexPrisoner on 04/12/08


Tagged

login register simple system and


Versions (?)

usercp.php


 / Published in: PHP
 

URL: http://www.myworldness.com

Its a good code!

  1. <?PHP session_start(); // NEVER REMOVE THIS!
  2. //Variables you would need to change!
  3. $dbhost = 'localhost'; //Your Database Host!
  4. $dbuser = 'exdeeorg_mwn'; //Your Database Username!
  5. $dbpass = 'mwnalex'; //Your Database Password!
  6. $dbname = 'exdeeorg_mwn'; //Your Database Name!
  7. //END! DON'T EDIT ANYTHING BELOW THIS LINE IF YOU DON'T KNOW WHAT YOUR DOING!!!
  8.  
  9. $connect = mysql_connect($dbhost,$dbuser,$dbpass) or die("Could not connect to database<br/><b>Error:</b>".mysql_error());
  10. mysql_select_db($dbname,$connect) or die("Could not connect to database<br/><b>Error:</b>".mysql_error());
  11.  
  12. function CleanText($value){
  13. return mysql_real_escape_string(htmlentities(stripslashes($value))); //Strips the code of anything that a hacker might try to do ;) Think smart!
  14. }
  15.  
  16. //Login Script
  17. if(isset($_POST['login'])){
  18. $username = CleanText($_POST['username']);
  19. $password = CleanText(md5($_POST['password'])); //MD5 Hash so no one can view the passwords!
  20. $check = mysql_query("SELECT * FROM `users` WHERE username = '{$username}' AND password = '{$password}'") or die("Could not pull User Data to match data<br/><b>Error:</b>".mysql_error());
  21. if(mysql_num_rows($check) == 1){
  22. $userdata = mysql_fetch_array($check);
  23. $_SESSION['user_id'] = $userdata['id']; //Sets SESSION id so we can fetch Userdata!
  24. $_SESSION['logged_in'] = 1; //Sets the session to know that they are logged in!
  25. header("location: index.php");
  26. }
  27. else{
  28. die("Wrong username or password!");
  29. }
  30. }
  31.  
  32. //Logout Script!
  33. if(isset($_GET['logout'])){
  34. die('You have been logged out!<br/>Now being re-directed to home!<meta http-equiv="refresh" content="3;URL=index.php">
  35. ');
  36. }
  37.  
  38. //Register Script!
  39. if(isset($_POST['register'])){
  40. $username = CleanText($_POST['username']);
  41. $password = CleanText(md5($_POST['password'])); //Again MD5 hashed to keep your members safe!
  42. if($username == ''){
  43. die("You didn't enter a username!");
  44. }
  45. if($password == ''){
  46. die("You didn't enter a password!");
  47. }
  48. $checkusername = mysql_query("SELECT * FROM `users` WHERE username = '{$username}'") or die("Could not load users to check username<br/><b>Error</b>".mysql_error());
  49. if(mysql_num_rows($checkusername) == 1){
  50. die("That username has already been taken sorry!");
  51. }
  52. //Okay so if everything goes good time to insert!
  53. mysql_query("INSERT INTO `users` (username,password) VALUES ('{$username}','{$password}')") or die("Could not create account<br/><b>Error:</b>".mysql_error());
  54. die('Account created!<br/><a href="usercp.php?login">Click Here</a> to login!');
  55. }
  56.  
  57. if(isset($_GET['login'])){
  58. echo '<form method="post">
  59. <table width="500" border="0" cellspacing="0" cellpadding="0">
  60. <tr>
  61. <td>Username:</td>
  62. <td><input type="text" name="username"/></td>
  63. </tr>
  64. <tr>
  65. <td>Password</td>
  66. <td><input type="password" name="password"/></td>
  67. </tr>
  68. <tr>
  69. <td colspan="2" align="center"><input type="submit" name="login" value="Login!"/></td>
  70. </tr>
  71. </table>
  72. </form>';
  73. }
  74. if(isset($_GET['register'])){
  75. echo '<form method="post">
  76. <table width="500" border="0" cellspacing="0" cellpadding="0">
  77. <tr>
  78. <td>Username:</td>
  79. <td><input type="text" name="username"/></td>
  80. </tr>
  81. <tr>
  82. <td>Password</td>
  83. <td><input type="password" name="password"/></td>
  84. </tr>
  85. <tr>
  86. <td colspan="2" align="center"><input type="submit" name="register" value="Register!"/></td>
  87. </tr>
  88. </table>
  89. </form>';
  90. }
  91. die("Could not find any data!");
  92. ?>

Report this snippet  

You need to login to post a comment.