PHPEdit.net Community
   
1 2 3 4
Tutorials Tips Pasties Code Snippets
 

Home > Pasties > Pastie #13e

Created on 21/04/2010 07:03 by Anonymous

  1. <?php session_start(); ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <title>Register a Username and Password</title>
  7. </head>
  8. <body>
  9. <?php
  10. $host = "localhost";
  11. $user = "nicole";
  12. $pass = "ur8zujdcNBCdSseW";
  13. $db = "users";
  14.  
  15. // This part sets up the connection to database
  16. // (so do not need to reopen the connection
  17. // again on the same page).
  18. $ms = mysql_pconnect($host, $user, $pass);
  19. if ( !$ms )
  20. {
  21. echo "Error connecting to database.\n";
  22. }
  23.  
  24. // Make sure the database is selected.
  25.  
  26. //Input vaildation and the dbase code
  27. if ( $_GET["op"] == "reg" )
  28. {
  29. $bInputFlag = false;
  30. foreach ( $_POST as $field )
  31. {
  32. if ($field == "")
  33. {
  34. $bInputFlag = false;
  35. }
  36. else
  37. {
  38. $bInputFlag = true;
  39. }
  40. }
  41. // If we had problems with the input, exit with error
  42. if ($bInputFlag == false)
  43. {
  44. die( "Problem with your registration. "
  45. ."Please go back and try again.");
  46. }
  47.  
  48. // Fields are clear, add user to database
  49. // Setup query
  50. $q = "INSERT INTO `dbUsers` (`username`,`password`,`email`) "
  51. ."VALUES ('".$_POST["username"]."', "
  52. ."PASSWORD('".$_POST["password"]."'), "
  53. ."'".$_POST["email"]."')";
  54. // Run query
  55. $r = mysql_query($q);
  56.  
  57. // Make sure query inserted user successfully
  58. if ( !mysql_insert_id() )
  59. {
  60. die("Error: User not added to database.");
  61. }
  62. else
  63. {
  64. // Redirect to thank you page.
  65. Header("Location: register.php?op=thanks");
  66. }
  67. } // end if
  68.  
  69. //The thank you page
  70. elseif ( $_GET["op"] == "thanks" )
  71. {
  72. echo "<h2>Thank you for registering!</h2>";
  73. }
  74.  
  75. //The web form for input ability
  76. else
  77. {
  78. echo "<form action=\"?op=reg\" method=\"POST\">\n";
  79. echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n";
  80. echo "Password: <input type=\"password\" name=\"password\"
    MAXLENGTH=\"16\"><br />\n"
    ;
  81. echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br
    />\n"
    ;
  82. echo "<input type=\"submit\">\n";
  83. echo "</form>\n";
  84. }
  85. // EOF
  86. ?>
  87. </body>
  88. </html>
  89.  

 
PHPEdit User Community, © 2008 WaterProof SARL
Powered by PHPEdit