Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.90 KB | None | 0 0
  1. <?php
  2. // Load requires
  3.  
  4. $page_title = 'Create A Character';
  5. require_once('startsession.php');
  6. require_once('header.php');
  7. require_once('dbc.php');
  8. require_once('navmenu.php');
  9.  
  10. // Set connect vars
  11. $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  12.  
  13. if (!isset($_SESSION['ID']))
  14. {
  15.     $ready = true;
  16.    
  17.     if (isset($_COOKIE['ID']) && isset($_COOKIE['username']))
  18.     {
  19.         $_SESSION['ID'] = $_COOKIE['ID'];
  20.         $_SESSION['username'] = $_COOKIE['username'];
  21.     }
  22.  
  23.     if (isset($_POST['submit']))
  24.     {
  25.         $username = mysqli_real_escape_string($dbc, trim($_POST['username']));
  26.         $password1 = mysqli_real_escape_string($dbc, trim($_POST['password1']));
  27.         $password2 = mysqli_real_escape_string($dbc, trim($_POST['password2']));
  28.         $email = mysqli_real_escape_string($dbc, trim($_POST['email']));
  29.         $charname = mysqli_real_escape_string($dbc, trim($_POST['charname']));
  30.         $race = mysqli_real_escape_string($dbc, trim($_POST['race']));
  31.         $class = mysqli_real_escape_string($dbc, trim($_POST['class']));
  32.  
  33.  
  34.         if ((!empty($username) && !empty($password1) && !empty($password2) && !empty($email) &&
  35.             !empty($charname) && !empty($race) && !empty($class)))
  36.         {
  37.             // Start Username Check
  38.             if (!preg_match('/^[a-zA-Z]+$/', $username))
  39.             {
  40.                 $ready = false;
  41.                 echo '<p class="error">Username must contain only letters</p>';
  42.             }
  43.             // End Username Check
  44.  
  45.             // Start Email Check
  46.             if (!preg_match('/[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', $email))
  47.             {
  48.                 $ready = false;
  49.                 echo '<p class="error">Your email address is invalid.</p>';
  50.             }
  51.             else
  52.             {
  53.                 // Strip out everything but the domain from the email
  54.                 $domain = preg_replace('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/','' , $email );
  55.                 // Now check if $domain is registered
  56.                 if (!checkdnsrr($domain))
  57.                 {
  58.                     echo '<p class="error">Your email address is invalid.</p>';
  59.                     $ready = false;
  60.                 }
  61.             }
  62.             // End Email Check
  63.  
  64.             // Start Charname Check
  65.             if (!preg_match('/^[a-zA-Z]+$/', $charname))
  66.             {
  67.                 $ready = false;
  68.                 echo '<p class="error">Character name must contain only letters</p>';
  69.             }
  70.             // End Charname Check
  71.  
  72.             // Start Password Check
  73.             if ($password1 != $password2)
  74.             {
  75.                 $ready = false;
  76.                 echo '<p class="error">Your passwords must match.</p>';
  77.             }
  78.             // End Password Check
  79.  
  80.             // Check For Duplicate Username Start
  81.             $query = "SELECT * FROM users WHERE username = '$username'";
  82.             $data = mysqli_query($dbc, $query);
  83.            
  84.             if (mysqli_num_rows($data) == 0)
  85.             {
  86.                 if ($ready == true)
  87.                 {
  88.                     // INSERT student into table if no errors
  89.                     $query = "INSERT INTO users (username, password, email, charname, race, class)
  90.                               VALUES ('$username', SHA('$password1'), $email, $charname, $race, $class)";
  91.  
  92.                     mysqli_query($dbc, $query);
  93.  
  94.                     echo '<p>Your new account has been successfully created. You\'re now ready to <a href="login.php">log in</a>.</p>';
  95.  
  96.                     mysqli_close($dbc);
  97.                     exit();
  98.                 }
  99.             }
  100.             else
  101.             {
  102.                 echo '<p class="error">An account already exists for this username. Please use a different username.</p>';
  103.             }
  104.             // Check For Duplicate Username End
  105.         }
  106.         else
  107.         {
  108.             echo '<p class="error">Fields cannot be left blank!</p>';
  109.         }
  110.     }
  111.     mysqli_close($dbc);
  112.  
  113.     ?>
  114.     <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  115.         <fieldset id="container">
  116.             <p>Please enter your information to apply.</p>
  117.             <fieldset><legend>User Information</legend>
  118.             <!-- Username -->
  119.             <label for="username">Username:</label>
  120.             <input type="text" id="username" name="username"
  121.                    value="<?php if (!empty($username)) echo $username; ?>" /><br />
  122.  
  123.             <!-- Password -->
  124.             <label for="password1">Password:</label>
  125.             <input type="text" id="password1" name="password1"
  126.                    value="<?php if (!empty($password1)) echo $password1; ?>"/><br />
  127.  
  128.             <label for="password2">Password (retype):</label>
  129.             <input type="text" id="password2" name="password2"
  130.                    value="<?php if (!empty($password2)) echo $password2; ?>"/><br />
  131.  
  132.             <!-- Email -->
  133.             <label for="email">Email:</label>
  134.             <input type="text" id="email" name="email"
  135.                    value="<?php if (!empty($email)) echo $email; ?>"/><br />
  136.             </fieldset>
  137.  
  138.             <fieldset><legend>Character Information</legend>
  139.             <!-- Email -->
  140.             <label for="charname">Character name:</label>
  141.             <input type="text" id="charname" name="charname"
  142.             value="<?php if (!empty($charname)) echo $charname; ?>"/><br />
  143.  
  144.             <!-- Race -->
  145.             <label for="race">Race:</label>
  146.             <select id="race" name="race">
  147.                 <option value="human">Human</option>
  148.                 <option value="dwarf">Dwarf</option>
  149.                 <option value="elf">Elf</option>
  150.             </select><br />
  151.  
  152.             <!-- Class -->
  153.             <label for="class">Class:</label>
  154.             <select id="class" name="class">
  155.                 <option value="monk">Monk</option>
  156.                 <option value="warrior">Warrior</option>
  157.                 <option value="thief">Thief</option>
  158.             </select><br />
  159.             </fieldset>
  160.  
  161.             <input type="submit" id="submit" value="Register" name="submit" />
  162.         </fieldset>
  163.     </form>
  164.     <?php
  165. }
  166. else
  167. {
  168.     echo 'Please log out to sign up.';
  169. }
  170. ?>
  171.  
  172. <?php
  173. // Insert the page footer
  174. require_once('footer.php');
  175. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement