Advertisement
Guest User

Untitled

a guest
Mar 11th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. <?php
  2. session_start();
  3. $onward = true;
  4.  
  5. /*
  6. * Virgin Mobile Free Fest Sweepstakes submission code
  7. *
  8. * Please edit the following lines to fit your installation.
  9. *
  10. */
  11.  
  12. $databaseLocation = 'localhost'; // generally, this is "localhost";
  13. $databaseUsername = 'marrsent_michael';
  14. $databasePassword = 'michaelPW';
  15. $databaseName = 'marrsent_virgin';
  16.  
  17. /* x message */
  18. $successMessage = "Thank you for your submission!";
  19.  
  20. /* returned error messages */
  21. $errorText['header'] = 'There were a few problems with your submission';
  22. $errorText['missingData'] = 'Not all requred data was given';
  23. $errorText['invalidEmail'] = 'The email you submitted isn\'t valid';
  24. $errorText['invalidPhoneNumber'] = 'That doesn\'t look like a good phone number';
  25. $errorText['invalidMobileNumber'] = 'That doesn\'t look like a good cell phone number';
  26. $errorText['invalidAge'] = 'You must check that you are 13 years of age before you can participate';
  27.  
  28. /*
  29. * don't edit anything below this line unless you know what you're doing
  30. */
  31.  
  32. include 'SqlBuilder.php';
  33.  
  34. // blank variables
  35. $errors = array();
  36.  
  37. // if submit was pressed
  38. if (isset($_POST['input-submit']) && $_POST['input-submit'] == '1') {
  39.  
  40. // inputs we'll be wanting
  41. $getThese = array(
  42. 'sex',
  43. 'first-name',
  44. 'middle-initial',
  45. 'last-name',
  46. 'address-1',
  47. 'address-2',
  48. 'city',
  49. 'state',
  50. 'zip-code',
  51. 'phone-number',
  52. 'mobile-number',
  53. 'birth-month',
  54. 'birth-day',
  55. 'birth-year',
  56. 'email-address',
  57. 'check-updates',
  58. 'check-wireless-updates',
  59. 'age-cert'
  60. );
  61.  
  62. // these could be null, and we don't want that...
  63. $couldBeNull = array(
  64. 'sex',
  65. 'check-updates',
  66. 'check-wireless-updates',
  67.  
  68. );
  69.  
  70. // roll through the inputs that could be null and set them to zero if they're not set
  71. foreach($couldBeNull as $value) {
  72. if (!isset($_POST['input-' . $value])) {
  73. $_POST['input-' . $value] = 0;
  74. }
  75. }
  76.  
  77. // grab the inputs we want
  78. foreach($getThese as $value) {
  79. $inputs[$value] = $_POST['input-' . $value];
  80. }
  81.  
  82. $_SESSION['sticky'] = $_POST;
  83.  
  84. function inputToDatabase(&$inputs, &$errorText) {
  85. // these inputs are required
  86. foreach(array('first-name', 'last-name', 'phone-number', 'email-address') as $value) {
  87. if (strlen($inputs[$value]) == 0) {
  88. return $errorText['missingData'];
  89. }
  90. }
  91.  
  92. // validate email address
  93. // init some blank variables for the matches
  94. $matches = array();
  95.  
  96. // check if the email address passes and give us some matching parts.
  97. if (eregi(
  98. "^[\._a-z0-9-]+((\+[\._a-z0-9-]+))*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|jobs|mobi|museum|name|travel))$",
  99. $inputs['email-address'],
  100. $matches)) {
  101. /*
  102. // if the local piece of the email holds a plus sign (using filters)
  103. if (isset($matches[1]) && strstr($matches[1], '+')) {
  104. $inputs['email-address-send'] = $inputs['email-address']; // switch it up
  105. $inputs['email-address'] = substr_replace($inputs['email-address-send'], '', strpos($inputs['email-address'], $matches[1]), strlen($matches[1])); // remove the filter for the send to address
  106. } else {
  107. $inputs['email-address-send'] = &$inputs['email-address']; // no filter there, point them both to the same address
  108. }
  109. */
  110. } else {
  111. return $errorText['invalidEmail'];
  112. }
  113.  
  114. // reset $matches
  115. $matches = array();
  116.  
  117. // validate their phone number if they submitted it
  118. if (strlen($inputs['phone-number']) > 0 && !eregi(
  119. "^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[0-9]{7})$",
  120. $inputs['phone-number'], $matches)) {
  121. return $errorText['invalidPhoneNumber'];
  122. }
  123.  
  124. // reset $matches
  125. $matches = array();
  126.  
  127. // validate their mobile number if they submitted it
  128. if (strlen($inputs['mobile-number']) > 0 && !eregi(
  129. "^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[0-9]{7})$",
  130. $inputs['mobile-number'], $matches)) {
  131. return $errorText['invalidMobileNumber'];
  132. }
  133.  
  134. if ($inputs['age-cert'] != '1') {
  135. return $errorText['invalidAge'];
  136. }
  137.  
  138. // create a new sql builder object
  139. $sql = new SqlBuilder();
  140.  
  141. // roll through all the inputs and throw 'em in
  142. foreach($inputs as $key => $value) {
  143. $sql->insert('submissions', $key, $value);
  144. }
  145. // log the users IP address
  146. $sql->insert('submissions', 'ip-address', (isset($_SERVER['HTTP_X_FORWARD_FOR']) ? $_SERVER['HTTP_X_FORWARD_FOR'] : $_SERVER['REMOTE_ADDR']));
  147.  
  148. $query = $sql->build('insert');
  149.  
  150. global $databaseLocation, $databaseUsername, $databasePassword, $databaseName;
  151. $db = new mysqli($databaseLocation, $databaseUsername, $databasePassword, $databaseName);
  152.  
  153. if ($db->query($query)) {
  154. $_POST = array();
  155. return 'success';
  156. }
  157.  
  158. return false;
  159. }
  160.  
  161. $errors[] = inputToDatabase($inputs, $errorText);
  162. }
  163.  
  164. function displayErrors(&$errorText, &$errors) {
  165. if (count($errors) > 0 && $errors[0] != null) {
  166. if ($errors[0] == 'success') {
  167. global $successMessage;
  168.  
  169. unset($_SESSION['sticky']);
  170. $_SESSION['message'] = $successMessage; // Set our message in the session
  171. header('Location: thanks.php '); // Onward
  172. exit;
  173.  
  174.  
  175.  
  176. } else {
  177. $errorOutput = implode('</li><li>', $errors);
  178. $_SESSION['errors'] = "<div id=\"errors\"><span id=\"error-header\">". $errorText['header'] ."</span><ul><li>". $errorOutput ."</li></ul></div>";
  179. header('Location: index.php ');
  180. exit;
  181. }
  182. } else {
  183. header('Location: index.php ');
  184. exit;
  185. }
  186. }
  187.  
  188. displayErrors($errorText, $errors);
  189. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement