Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //mysql server connection information
- $dbhost = "localhost";
- $dbname = "web";
- $dbuser = "xxx";
- $dbpass = "xxx";
- //connection query
- mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
- mysql_select_db($dbname) or die(mysql_error());
- //gathers information from the .html page and sends it through POST
- $code = $_POST['code'];
- $username = $_POST['username'];
- $password = $_POST['password'];
- //if conditions to check if username/password/code field are empty
- if(empty($_POST['username'])){
- echo "Username field is empty.";
- exit();
- } elseif(empty($_POST['password'])) {
- echo "Password field is empty.";
- exit();
- } elseif(empty($_POST['code'])) {
- echo "Code field is empty.";
- exit();
- }
- //tries to find the code they input in the database
- $checkcode = mysql_query("SELECT code FROM codes WHERE code='$code'");
- $checkcode_exist = mysql_num_rows($checkcode);
- if ($checkcode_exist < 1) {
- echo "No code found.";
- exit();
- }
- $row = mysql_fetch_row($checkcode); //checks the row of the code they input
- 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
- echo "Code has already been used!";
- exit();
- }
- //checks to see if the username they input exists
- $checkuser = mysql_query("SELECT username FROM login WHERE username='$username'");
- $username_exist = mysql_num_rows($checkuser);
- if($username_exist > 0){ //if it exists, print error, unset the $username variable
- echo "Username taken.";
- unset($username);
- include 'index.html';
- exit();
- }
- $query = "INSERT INTO login (username, password) VALUES('$username', '$password')"; //registery success, insert the username/password they input into the database
- $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
- mysql_query($query) or die(mysql_error()); //execute above query
- mysql_query($codequery) or die(mysql_error()); //execute above query
- mysql_close();
- print "Registration success!";
- exit();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement