Guest User

Untitled

a guest
Dec 1st, 2017
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. $_POST["emailAdd"] = "johno@email.com";
  2. $_POST["fullName"] = "John O";
  3. // $_POST["accountNum"] = "ZAP AND 1=1 --";
  4. // $_POST["accountNum"] = "ZAP OR 1=1 --";
  5. // $_POST["accountNum"] = "234-123456-123";
  6.  
  7. $upTwo = realpath(__DIR__ . '/../..');
  8. require_once $upTwo . '/vendor/autoload.php';
  9. include $upTwo . '/src/helper.php';
  10.  
  11. $errors = array();
  12. $userData = array();
  13. $inputArray = array();
  14. $config = new CONFIGConfig();
  15.  
  16. // sanitise user data
  17. if (empty($_POST['fullName'])) {
  18. $errors['fullName'] = '- Please input your Full Name as per Passport';
  19. } else {
  20. $fullName = filter_var($_POST['fullName'], FILTER_SANITIZE_STRING);
  21. array_push($inputArray, $fullName);
  22. }
  23.  
  24. if (empty($_POST['emailAdd'])) {
  25. $errors['emailAdd'] = '- Please input your Email address';
  26. } else {
  27. if (!filter_var($_POST['emailAdd'], FILTER_VALIDATE_EMAIL)) {
  28. $errors['emailAdd'] = '- Please input your Email address';
  29. } else {
  30. $email = filter_var($_POST['emailAdd'], FILTER_SANITIZE_EMAIL);
  31. array_push($inputArray, $email);
  32. }
  33. }
  34.  
  35. if (empty($_POST['accountNum'])) {
  36. $errors['accountNum'] = '- Please input your Account Number';
  37. } else {
  38. // regex looks for exact 14 character combination of 3 digits, one dash, 6 digits, one dash and 3 digits
  39. if (!filter_var($_POST['accountNum'], FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^d{3}-d{6}-d{3}$/")))) {
  40. $errors['accountNum'] = '- Please enter correct account number';
  41. } else {
  42. $accountNum = filter_var($_POST['accountNum'], FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^d{3}-d{6}-d{3}$/")));
  43. array_push($inputArray, $accountNum);
  44. }
  45. }
  46.  
  47. // save user details in db
  48. if (empty($errors)) {
  49.  
  50. try {
  51.  
  52. // save user details in db if not sanitisation errors
  53. $dbh = new PDO("mysql:host=" . $config::DB_HOST . ";dbname=" . $config::DB_NAME . "", $config::DB_USER, $config::DB_PASSWORD);
  54.  
  55. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  56.  
  57. $stmt = $dbh->prepare("INSERT INTO " . $config::DB_TABLE . "(full_name, email_address, account_number) VALUES (?, ?, ?)");
  58. $stmt->bindParam(1, $inputArray[0]);
  59. $stmt->bindParam(2, $inputArray[1]);
  60. $stmt->bindParam(3, $inputArray[2]);
  61.  
  62. $response = $stmt->execute(); // $response variable added for debugging purposes
  63. var_dump($response); // var_dump() added for debugging purposes
  64.  
  65. $userData["lastId"] = $dbh->lastInsertId();
  66.  
  67. } catch (Exception $e) {
  68.  
  69. logError($e->getMessage(), $e->getFile(), $e->getLine());
  70. $errors['dbInsert'] = $e->getMessage();
  71. throw new Exception($e->getMessage() . " | File: " . $e->getFile() . " | Failure on line: " . $e->getLine());
  72.  
  73. }
  74.  
  75. }
  76.  
  77. johno:php johno$ php process.php
  78. array(1) {
  79. ["accountNum"]=>
  80. string(37) "- Please enter correct account number"
  81. }
  82. array(2) {
  83. [0]=>
  84. string(6) "John O"
  85. [1]=>
  86. string(15) "johno@email.com"
  87. }
  88. johno:php johno$ php process.php
  89. array(1) {
  90. ["accountNum"]=>
  91. string(37) "- Please enter correct account number"
  92. }
  93. array(2) {
  94. [0]=>
  95. string(6) "John O"
  96. [1]=>
  97. string(15) "johno@email.com"
  98. }
  99. johno:php johno$ php process.php
  100. array(0) {
  101. }
  102. array(3) {
  103. [0]=>
  104. string(6) "John O"
  105. [1]=>
  106. string(15) "johno@email.com"
  107. [2]=>
  108. string(14) "234-123456-123"
  109. }
  110. bool(true)
Add Comment
Please, Sign In to add comment