Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. <?php
  2.  
  3.     //Collect data
  4.     $registrationData = [
  5.         $_POST['firstname'],
  6.         $_POST['lastname'],
  7.         $_POST['email'],
  8.         $_POST['phone'],
  9.         $_POST['city'],
  10.         $_POST['store'],
  11.         $_POST['experience'],
  12.         $_POST['event'],
  13.         $_POST['time']
  14.     ];
  15.  
  16.     //Stop progress entirely if registration data is incomplete
  17.     foreach($registrationData as $data){
  18.         if(!isset($data)){
  19.             die("Incomplete Form Data");
  20.         }
  21.     }
  22.    
  23.     //Verify Re-Captcha
  24.     $captcha_verify_post = [
  25.         'secret' => 'THANKS JONNY :)',
  26.         'response' => $_POST['g-recaptcha-response'],
  27.         'remoteip' => $_SERVER['REMOTE_ADDR']
  28.     ];
  29.  
  30.     $ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
  31.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  32.     curl_setopt($ch, CURLOPT_POSTFIELDS, $captcha_verify_post);
  33.  
  34.     $captcha_verified = json_decode(curl_exec($ch), true)["success"];
  35.     curl_close($ch);
  36.  
  37.     //Stop if captcha failed
  38.     if(! $captcha_verified){
  39.         die("Incorrect Captcha");
  40.     }
  41.  
  42.     //Connect to db
  43.     $host = 'localhost';
  44.     $user = 'bosch';
  45.     $pass = '!p@55w0rd!';
  46.     $db = 'bosch_registrants';
  47.  
  48.     $link = mysqli_connect($host, $user, $pass, $db);
  49.  
  50.     //Sanitize data
  51.     function sanitize($data){
  52.         global $link;
  53.         $data = htmlspecialchars($data);
  54.         $data = mysqli_real_escape_string($link, $data);
  55.         return $data;
  56.     }
  57.  
  58.     $registrationData = array_map("sanitize", $registrationData);
  59.  
  60.     //Add submission time and IP address to data
  61.     date_default_timezone_set('America/New_York');
  62.     array_push($registrationData, date('Y-m-d H:i:s'), $_SERVER['REMOTE_ADDR']);
  63.  
  64.     //Insert into db
  65.     mysqli_query($link,
  66.         "INSERT INTO `registrants`(
  67.             `firstname`,
  68.             `lastname`,
  69.             `email`,
  70.             `phone`,
  71.             `city`,
  72.             `store`,
  73.             `experience`,
  74.             `event`,
  75.             `time`,
  76.             `submissiontime`,
  77.             `ipaddress`
  78.         )
  79.         VALUES (" . implode(',', $registrationData) . ")"
  80.     );
  81.  
  82.     mysqli_close($link);
  83.  
  84.     echo("success!");
  85.  
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement