Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 30th, 2012  |  syntax: None  |  size: 1.93 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. /**
  4.  * Ajax Newsletter signup
  5.  * checking email and stance for razorboarders.
  6.  **/
  7. // Includes first
  8. require_once 'is_email/is_email.php';   // library for email validation
  9.  
  10.  
  11. // Initialize constants and variables
  12. define( 'SUBSCRIBERS', 'db/subscribers.xml' );
  13. /* xml file must have a tag present:
  14. <?xml version="1.0"?>
  15. <subscribers></subscribers>
  16. */
  17.  
  18. $email = $_REQUEST['email'];    // Todo: validate input
  19. $email = "no@no.no";
  20. $stance = $_REQUEST['stance'];
  21. $stance = "switch";
  22.  
  23.  
  24. // Check email not empty and valid  (is_email often fails on dns checks, add "false" to disable)
  25. if( !empty( $email ) && is_email( $email ) )
  26. {
  27.         // Check xml file exists and writable
  28.         if( file_exists( SUBSCRIBERS ) && is_writable( SUBSCRIBERS ) )
  29.         {
  30.                 // Create simplexml object
  31.                 $xml = simplexml_load_file( SUBSCRIBERS );
  32.                 $sxml = new SimpleXMLElement( $xml->asXML() );  // Can get variable from php file here and skip last line.
  33.  
  34.                 // Loop through subscribers
  35.                 foreach( $sxml->person as $person )
  36.                 {
  37.                         // Die if email exists
  38.                         if( (string) $person->email === $email )
  39.                                 die( "Email already in list, you can subscribe again with a different one if you wish." );
  40.                 }
  41.                
  42.                 // Create new person xml tag
  43.                 $new_person = $sxml->addChild( 'person' );
  44.  
  45.                 // Add email
  46.                 $new_person->addChild( 'email', $email );
  47.  
  48.                 // Add stance
  49.                 switch( (string) $stance )
  50.                 {
  51.                         case 'regular':
  52.                                 $new_person->addChild( 'stance', 'regular' );   // coule be $stance, but not sure if always lowercase then
  53.                                 break;
  54.                         case 'goofy':
  55.                                 $new_person->addChild( 'stance', 'goofy' );
  56.                                 break;
  57.                         case 'switch':
  58.                                 $new_person->addChild( 'stance', 'switch' );
  59.                                 break;
  60.                 }
  61.                 //$rating->addAttribute('type', 'racing'); // off-piste, pipe, freestyle, bigjump, kids..
  62.  
  63.                 // Write to xml file
  64.                 $sxml->asXML( SUBSCRIBERS );
  65.                 echo "Ok\n"; //or true etc..
  66.         }
  67.         else
  68.                 die( "File error." );
  69. }
  70. else
  71.         die( "Email not valid, please try again." );