Advertisement
gitlez

YA: Simple Form Processing of Reg with Upload

Jul 10th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.12 KB | None | 0 0
  1. <?php
  2. /*    Simple functions to save typing    */
  3. function postVal($name){ // Grabs the $_POST value and cleans it up
  4.     $v = (isset($_POST[$name]))? trim($_POST[$name]): '';
  5.     return (get_magic_quotes_gpc())? stripslashes($v) : $v;
  6. }
  7. function mres($v){ // Annonymouse function for mysqli_real_escape_string();
  8.     Global $dbc;
  9.     return mysqli_real_escape_string($dbc, $v);
  10. }
  11. function mres_pv($name){
  12.     return mres(postVal($name));
  13. }
  14.  
  15.  
  16. require_once('appvars.php');
  17. require_once('connectvars.php');
  18.  
  19.  
  20. /*    FORM    */
  21. $form = '
  22.  <hr />
  23.      <form enctype="multipart/form-data" method="post" action="' . $_SERVER['PHP_SELF'] . '">
  24.    <input type="hidden" name="MAX_FILE_SIZE" value="' . GW_MAXFILESIZE . '" />
  25.  
  26.  
  27.     <label for="username">Username:</label>
  28.      <input type="text" name="username" value="' . postVal('username') . '" /><br />
  29.      <label for="password1">Password:</label>
  30.      <input type="password" name="password1" /><br />
  31.      <label for="password2">Password (retype):</label>
  32.      <input type="password" name="password2" /><br />
  33.      <label for="email">E-mail Address:</label>
  34.      <input type="text" name="email" value="' . postVal('email') . '" /><br />
  35.  
  36.  
  37.      <label for="first_name">First Name:</label>
  38.      <input type="text" id="first_name" name="first_name" value="' . postVal('first_name') . '" /></br>
  39.      
  40.  
  41.  
  42.      <label for="last_name">Last Name:</label>
  43.      <input type="text" id="last_name" name="last_name" value="' . postVal('last_name') . '" /><br />
  44.  
  45.  
  46.      <label for="gender">Gender:</label>
  47.      <select name="gender">
  48.          <option value="Male">Male</option>
  49.          <option value="Female">Female</option>
  50.      </select>  </br>
  51.      <label for="dobdate">Date of Birth:</label>
  52.      <select name="dobdate">
  53. ';
  54. for($i=1;$i<=31;++$i){
  55.     $form .= '            <option value="' . $i . '">' . $i . '</option>' . PHP_EOL;
  56. }
  57. $form .= '
  58.        </select>
  59.  
  60.      <label for="dobmonth"></label>
  61.      <select name="dobmonth">
  62.        <option value="" selected="selected">---</option>
  63. ';
  64. foreach(Array("JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER") as $month){
  65.     $form .= '            <option value="' . $month . '">' . $month . '</option>' . PHP_EOL;
  66. }
  67. $form .= '
  68.      </select>
  69.  
  70.  
  71.      <label for="dobyear"></label>
  72.      <select name="dobyear">
  73.  
  74.      <option selected="selected" value="">-----</option>
  75. ';
  76. foreach(range(1921,1993) as $year){
  77.     $form .= '            <option value="' . $year . '">' . $year . '</option>' . PHP_EOL;
  78. }
  79.  
  80. $form .= '
  81.      </select>
  82.      </br>
  83.  
  84.  
  85.  
  86.      <label for="location">Location:</label>
  87.      <select name="location">
  88.      <option selected="selected" value="">---</option>
  89. ';
  90.  
  91. $locations = Array('Albenia','Iran','India');
  92. foreach($locations as $local){
  93.     $form .= '         <option value="' . $local . '">' . $local . '</option>';
  94. }
  95.  
  96. $form .= '
  97.      </select>
  98.      </br>
  99.  
  100.      <label for="about_text">About Me:</label>
  101.      <input type="text" id="about_text" name="about_text" value="' . postVal('about_text') . '" /><br />
  102.  
  103. <label for="screenshot">Screen shot:</label>
  104.    <input type="file" id="screenshot" name="screenshot" /><br />
  105.  
  106.    <hr />
  107.    <input type="submit" value="Add" name="submit" />
  108.  </form>
  109. ';  
  110.  
  111.  
  112.  
  113.  
  114. if( isset( $_POST['submit']) ){
  115.     // Grab the score data from the POST
  116.     // Grab the profile data from the POST
  117.     // Rememeber: All user input data is always possibly dangerous, always sanitize it. Even <select> values can be altered.
  118.     $username = mres_pv('username');
  119.     $password1 = $_POST['password1'];
  120.     $password2 = $_POST['password2'];
  121.  
  122.     $f = $_FILES['screenshot'];
  123.     $screenshot = mysqli_real_escape_string($dbc, trim($f['name']));
  124.     $screenshot_type = $f['type'];
  125.     $screenshot_size = $f['size'];
  126.     $upload_error = $f['error']; // Should be 0, if there's no upload error.
  127.    
  128.     // Error Checking
  129.     $errors = Array(); // Will hold the error messages.
  130.    
  131.     // Required Entries
  132.     if(empty($username) || empty($passsword1)){
  133.         $errors[] = "Username and Password are required entries.";
  134.     }
  135.    
  136.     // Check for existing username
  137.     $stmt = "SELECT username FROM userinfo WHERE username='{$username}' LIMIT 1";
  138.     $result = mysqli_query($dbc, $stmt);
  139.     if( mysqli_num_rows($result) > 0){
  140.         $errors[] = "Username is already Registered, please select another";
  141.     }
  142.    
  143.     // Check for passwords match
  144.     if( $password1 !== $password2){
  145.         $errors[] = 'Passwords do not match.';
  146.     }
  147.    
  148.     // Check for an upload error
  149.     if( $upload_error !== UPLOAD_ERR_OK){ // PHP CONSTANT
  150.         $uploadErrorMsgs = Array(
  151.             1 => 'The selected file exceeds the allowable Server file size. Please select an alternate file. (' . GW_MAXFILESIZE . ' bytes)',
  152.             2 => 'The selected file exceeds the allowable Upload file size. Please select an alternate file. (' . GW_MAXFILESIZE . ' bytes)',
  153.             3 => 'Upload connection was interuppted or dropped. Please try again.',
  154.             4 => 'Please select a file to upload.',
  155.             6 => 'Internal Server Error (#UP606). Please inform site Admin and please try again later.', // Internal Operations Error
  156.             7 => 'Internal Server Error (#UP707). Please inform site Admin and please try again.', // Internal Server Error
  157.             8 => 'Internal Server Error (#UP808). Please inform site Admin and please try again later.' // PHP Extension Library stopped the upload.
  158.         );
  159.         $errors[] = $uploadErrorMsgs[(int)$upload_error];
  160.     }
  161.    
  162.     // Check for allowed file types.
  163.     $allowedFileTypes = Array( 'image/gif','image/jpeg','image/pjpeg','image/jpg','image/png');
  164.     if( !in_array($screenshot_type, $allowedFileTypes)){
  165.         $errors[] = 'Image type in not allowed. Only gif, jpeg and png images are allowed.';
  166.     }
  167.    
  168.     // Error Checks Done
  169.    
  170.     // Check to see if any errors occurred.
  171.     if( count( $errors ) === 0){
  172.         // All Good, continue form processing
  173.        
  174.         // Move Uploaded File
  175.         $target = GW_UPLOADPATH . $screenshot;
  176.         if(!move_uploaded_file($f['tmp_name'], $target) ){
  177.             die('Internal Server Error. Please try again later. #UP1011');
  178.         }
  179.        
  180.         $userData = Array(
  181.             'date' => date('Y-m-d H:i:s', time()),
  182.             'user_name' => $username,
  183.             'password' => sha1($password1),
  184.             'email' => mres_pv('email'),
  185.             'first_name' => mres_pv('first_name'),
  186.             'last_name' => mres_pv('last_name'),  
  187.             'gender' => mres_pv('gender'),
  188.             'dob_date' => mres_pv('dobdate'),
  189.             'dob_month' => mres_pv('dobmonth'),
  190.             'dob_year' => mres_pv('dobyear'),    
  191.             'location' => mres_pv('location'),
  192.             'about_me' => mres_pv('about_text'),
  193.             'screenshot' => $screenshot
  194.         );
  195.         $stmt = "INSERT INTO userinfo(" . implode(',', array_keys($userData)) . ") VALUES('" . implode("','", $userData) . "')";
  196.         if(mysqli_query($dbc, $stmt)){
  197.             $message = '<p>Your new account has been successfully created. You\'re now ready to <a href="login.php">log in</a>.</p>';
  198.         }else{
  199.             $message = '<p>Internal Server Error. Please Try Again Later.</p>';
  200.         }
  201.         mysqli_close($dbc);
  202.     }else{
  203.         $message = "<p>Form errors encountered. Please correct before continueing.<br>";
  204.         foreach($errors as $msg){
  205.             $message .= '• ' . $msg . '<br>';
  206.         }
  207.         $message .= '</p><br>' . $form;
  208.     }
  209. }else{
  210.     $message = $form;
  211. }
  212.  
  213. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  214.   "http://cosmicpals.net/signup5.php">
  215. <html xmlns="http://cosmicpals.net/signup5.php" xml:lang="en" lang="en">
  216. <head>
  217.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  218.   <title>Guitar Wars - Add Your High Score</title>
  219.   <link rel="stylesheet" type="text/css" href="style.css" />
  220. </head>
  221. <body>
  222.   <h2>Guitar Wars - Add Your High Score</h2>
  223.     <?php echo $message; ?>
  224. </body>
  225. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement