Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.91 KB | None | 0 0
  1. <?php
  2.  
  3. $code = md5(uniqid());
  4.  
  5. if(!is_valid_request()
  6.     # If the request validated, we can expect all these fields to exist
  7.    # in $_POST, otherwise PHP would not reach this part of the condition:
  8. || !create_new_user($code, $_POST['username'], $_POST['email'], $_POST['pass'])
  9. ) {
  10.     redirect('error_page.html'); # Redirect will exit.
  11. }
  12.  
  13. # I removed the code to redirect to more specific error pages,
  14. # you might rather start a session and create an error message
  15. # to store in the session, and then print it on the error page,
  16. # or do the checks first in javascript, and then if a user ignores
  17. # them or disables JS, just fail with a generic
  18. # "Your request could not be processed" page.
  19.  
  20. $message = "Your confirmation link \r\n";
  21. $message.= "Click on this link to activate your account \r\n";
  22. $message.= "MYWEBSITE.org/confirmation.php?passkey=$code";
  23.  
  24. print $message;
  25.  
  26. # =============================================================================
  27. # Below are all functions used above:
  28. # =============================================================================
  29.  
  30. # This is not reusable, it really just does the check for this page
  31. # I put it into a function with a clear name so that it can be separate
  32. # from the actual action that the page will take on success:
  33. function is_valid_request() {
  34.     $required = array('username', 'email', 'email2', 'pass', 'pass2');
  35.     # The moment any one of these fails, PHP will stop checking and return false.
  36.    return fields_are_present($required, $_POST)
  37.         && ctype_alnum($_POST['username']) # Check for alphanumeric
  38.        && $_POST['email'] === $_POST['email2'] # Check equals
  39.        && $_POST['pass']  === $_POST['pass2'] # check equals
  40.        && user_is_unique($_POST['username'], $_POST['email']);
  41. }
  42.  
  43. # This is somewhat reusable, if we have another page to create a user on,
  44. # it can be done there as well, as long as getPDOInstance is also present
  45. # and working.
  46. function create_new_user($code, $username, $email, $password) {
  47.     $db = getPDOInstance();
  48.     $sql = 'INSERT INTO temp (code, username, email, password) VALUES (?, ?, ?, ?)';
  49.     $stmt = $db->prepare($sql);
  50.     try {
  51.         $stmt->execute(array($code, $username, $email, $pass));
  52.     } catch(\PDOException $e) {
  53.         error_log($e->getMessage()); # assuming your error log is set up
  54.        return false;
  55.     }
  56.     return true;
  57. }
  58.  
  59. # This is basic, but reusable:
  60. function redirect($url) {
  61.     header('Location: '. $url);
  62.     exit('You are being redirected to: <a href="'.$url.'">'.$url.'</a>');
  63. }
  64.  
  65. # Reusable, check of an array of keys (fields) are in another array:
  66. function fields_are_present(array $fields, array $array) {
  67.     foreach($fields as $field) {
  68.         if(empty($array[$field])) {
  69.             return false;
  70.         }
  71.     }
  72.     return true;
  73. }
  74.  
  75. # Does the username already exist? You'd be better off just trying to insert
  76. # it and letting SQL give a non-unique error, but anyway, I'll just do the check
  77. # I don't usually do checks like this but I think this would work
  78. function user_is_unique($username, $email) {
  79.     $db = getPDOInstance();
  80.     $sql = 'SELECT id FROM users WHERE username = ? OR email = ? LIMIT 1'
  81.     $stmt = $db->prepare($sql);
  82.     $stmt->execute(array($username, email));
  83.     $result = $db->fetchColumn();
  84.     # We have a unique user if we got an empty result:
  85.    return empty($result);
  86. }
  87.  
  88. # Get a PDO instance based on made up parameters.
  89. # Once an instance exists, it is 'static' and will only be created once,
  90. # so every time you use this function, you will get the same connection
  91. # that was created the first time.
  92. function getPDOInstance() {
  93.     static $instance;
  94.     if(empty($instance)) {
  95.         $config = include('config.php');
  96.         $instance = new PDO($config['connection'], $config['user'], $config['pass']);
  97.         $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  98.     }
  99.     return $instance;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement