Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.09 KB | None | 0 0
  1. <?php
  2. //mysql server connection information
  3. $dbhost = "localhost";
  4. $dbname = "web";
  5. $dbuser = "xxx";
  6. $dbpass = "xxx";
  7.  
  8. //connection query
  9. mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
  10. mysql_select_db($dbname) or die(mysql_error());
  11.  
  12. //gathers information from the .html page and sends it through POST
  13. $code = $_POST['code'];    
  14. $username = $_POST['username'];
  15. $password = $_POST['password'];
  16.  
  17. //if conditions to check if username/password/code field are empty
  18. if(empty($_POST['username'])){
  19.     echo "Username field is empty.";
  20.     exit();
  21. } elseif(empty($_POST['password'])) {
  22.     echo "Password field is empty.";
  23.     exit();
  24. } elseif(empty($_POST['code'])) {
  25.     echo "Code field is empty.";
  26.     exit();
  27. }
  28.  
  29. //tries to find the code they input in the database
  30. $checkcode = mysql_query("SELECT code FROM codes WHERE code='$code'");
  31. $checkcode_exist = mysql_num_rows($checkcode);
  32.  
  33. if ($checkcode_exist < 1) {
  34.     echo "No code found.";
  35.     exit();
  36. }
  37.  
  38. $row = mysql_fetch_row($checkcode); //checks the row of the code they input
  39.  
  40. if ($row[2] = 1) { //if the third column (ID, CODE, USED) is equal to 1 (the code is used) it will stop running and print error
  41.     echo "Code has already been used!";
  42.     exit();
  43. }
  44.  
  45. //checks to see if the username they input exists
  46. $checkuser = mysql_query("SELECT username FROM login WHERE username='$username'");
  47. $username_exist = mysql_num_rows($checkuser);
  48.  
  49. if($username_exist > 0){ //if it exists, print error, unset the $username variable
  50.     echo "Username taken.";
  51.     unset($username);
  52.     include 'index.html';
  53.     exit();
  54. }
  55.  
  56. $query = "INSERT INTO login (username, password) VALUES('$username', '$password')"; //registery success, insert the username/password they input into the database
  57. $codequery = "UPDATE codes SET used='1' where code='$code'"; //set the value of "used" in the code to 1, preventing them from re-using the same code
  58. mysql_query($query) or die(mysql_error()); //execute above query
  59. mysql_query($codequery) or die(mysql_error()); //execute above query
  60. mysql_close();
  61. print "Registration success!";
  62. exit();
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement