Current location: Hot Scripts Forums » Programming Languages » PHP » Blocking domains with preg_match?


Blocking domains with preg_match?

Reply
  #1 (permalink)  
Old 11-17-09, 08:04 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
Blocking domains with preg_match?

I've been toying with the idea of using explode() and a preg_match to disallow all emails from .ru domains and gawab.com from registering on my site due to the fact that they are mainly used by spammers/hackers and bots.

However, I must be doing something wrong with the preg_match (lines 87 to 92). I currently have the preg_match commented out because it causes the website to not pull up, it just shows a white background. If I comment out the preg_match it works fine. I've been experimenting with the preg_match in RegexBuddy but I still can't quite get it to work.

Also, the "echo $domain[1]" code is there as a debug so I could make sure that the explode was working properly.

For now I'm only trying to disallow all .ru domains but I'd like to add gawab.com to the list as well at some point.

php Code:
  1. <?php
  2. include('../includes/constants.php');
  3. ?>
  4. <!DOCTYPE html>
  5. <html lang="en">
  6. <head>
  7. <meta charset="utf-8">
  8. <script type="text/javascript"
  9. var RecaptchaOptions = { 
  10.    theme : 'clean' 
  11. }
  12. </script>
  13. <title>Welcome to my Website!</title>
  14. </head>
  15. <body>
  16. We are hard at work getting the site up and running! We are hoping to get running around December or January.
  17. If you would like us to notify you when we get setup fill out the form below with your name and email address.
  18. <?php
  19. require_once('../reCAPTCHA/recaptchalib.php');
  20.  
  21. // Keys from [url]http://recaptcha.net/api/getkey[/url].
  22. $publickey = "[deleted]";
  23. $privatekey = "[deleted]";
  24.  
  25. # the response from reCAPTCHA
  26. $resp = null;
  27. # the error code from reCAPTCHA, if any
  28. $error = null;
  29.  
  30.     if (isset($_POST&#91;'submitted'])) {
  31.         require_once('../includes/sanitize.php');
  32.        
  33.         //Sanitize data before validation
  34.         $c_fname = sanitize(3, 15, $_POST&#91;'first_name']);
  35.         $c_lname = sanitize(3, 25, $_POST&#91;'last_name']);
  36.         $c_email = sanitize(5, 60, $_POST&#91;'email']);
  37.         $c_email_2 = sanitize(5, 60, $_POST&#91;'email_2']);
  38.        
  39.         ////////////////////////////////////////////////////
  40.         // BotScout.com "BotBuster" check
  41.         require_once('../includes/BotScout.php');
  42.         ////////////////////////////////////////////////////
  43.  
  44.         $resp = recaptcha_check_answer ($privatekey,
  45.                                         $_SERVER&#91;"REMOTE_ADDR"],
  46.                                         $_POST&#91;"recaptcha_challenge_field"],
  47.                                         $_POST&#91;"recaptcha_response_field"]);
  48.  
  49.         if ($resp->is_valid) {
  50.         require_once(MYSQL);
  51.                 
  52.         //Validate the first name
  53.         $fResult = preg_match("/^[a-zA-Z\.]{1,15}/", $c_fname);
  54.         if (!$fResult) /* If it didn't match okay. */
  55.          return false;
  56.            
  57.         //Validate last name
  58.         $lResult = preg_match("/^[a-zA-Z\-.]{1,25}/", $c_lname);
  59.         if (!$lResult) /* If it didn't match okay. */
  60.           return false;
  61.        
  62.         //Is the email valid and not empty?
  63.         if (empty($c_email_2)) {
  64.             echo '<p>Please go back and enter your email address in both fields.</p>';
  65.             exit();
  66.         } elseif ($c_email !== $c_email_2) {
  67.             echo '<p>The email fields do not match. Please go back and confirm them again.</p>';
  68.             exit();
  69.         } else {
  70.             function isValidEmail( $c_email = null ) {
  71.             $eResult = preg_match( "/^
  72.             [\d\w\/+!=#|$?%{^&}*`'~-]
  73.             [\d\w\/\.+!=#|$?%{^&}*`'~-]*@
  74.             [A-Z0-9]
  75.             [A-Z0-9.-]{1,60}
  76.             [A-Z0-9]\.
  77.             [A-Z]{2,6}/", $c_email);
  78.             if (!$eResult) /* If it didn't match okay. */
  79.               return false;
  80.                 }
  81.         }
  82.             /* This code doesn't quite work yet....
  83.             $domain = explode('@', $c_email);
  84.            
  85.             echo $domain[1];
  86.             $dResult = preg_match("/^[A-Z0-9]{1,60}\.ru$/", $domain[1]);
  87.             if ($dResult)
  88.                 return false;
  89.             echo "<p>An error has occurred, please try again.</p>";
  90.             exit();
  91.             */
  92.         if (!empty($c_email) && $c_email == $c_email_2) {
  93.             $access = "SELECT Email FROM customers WHERE Email='$c_email'";
  94.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  95.         }
  96.        
  97.         //Insert information if email is unique:
  98.         if (mysqli_num_rows($r) == 0) {
  99.             $access = "INSERT INTO customers (Email, First, Last) VALUES ('$c_email', '$c_fname', '$c_lname')";
  100.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  101.         } elseif (mysqli_num_rows($r) == 1) {
  102.             echo "<p>Your address has previously been recorded. We will notify you at $c_email when we are up and running.</p>";
  103.         } else {
  104.             echo '<p>Your email address could not be recorded due to an error, please try again.</p>';
  105.         }
  106.        
  107.         //If user fails reCAPTCHA.
  108.         } else {
  109.              # set the error code so that we can display it
  110.              $error = $resp->error;
  111. } // End reCAPTCHA else.
  112. mysqli_close($dbc);
  113. } // End Submit IF.
  114. ?>
  115. <form action="" method="post">
  116. <fieldset>
  117. <p><b>First name:</b> <input type="text" name="first_name" size="20" maxlength="15" value="<?php if(isset($c_fname)) echo $c_fname; ?>" /></p>
  118. <p><b>Last name:</b> <input type="text" name="last_name" size="20" maxlength="25" value="<?php if(isset($c_lname)) echo $c_lname; ?>" /></p>
  119. <p><b>Email Address:</b> <input type="text" name="email" size="30" maxlength="60" value="<?php if(isset($c_email)) echo $c_email; ?>" /> (Required)</p>
  120. <p><b>Confirm email address:</b> <input type="text" name="email_2" size="30" maxlength="60" value="<?php if(isset($c_email_2)) echo $c_email_2; ?>" /> (Required)</p>
  121. <?php echo recaptcha_get_html($publickey, $error); ?>
  122.  
  123. <br />
  124. </fieldset>
  125. <div class="center"><input type="submit" name="submit" value="Register" /></div>
  126. <input type="hidden" name="submitted" value="TRUE" />
  127. </form>
  128. </body>
  129. </html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-17-09, 09:34 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Unless I misunderstand what you want to do, this will match any basic *.ru domain: (.*)\.ru

It won't match zzz.ru.somedomain.com, but it'll match some-bad.ru, foo.bar.ru, and so on.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 11-18-09, 12:01 AM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
Basically, I'm just looking to block anything that ends with .ru (i.e. mail.ru, yandex.ru, thisisatest.ru etc.) so that should work perfectly.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-20-09, 09:04 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Quote:
Basically, I'm just looking to block anything that ends with .ru
You could also use strrchr.

PHP: strrchr - Manual

PHP Code:

$mRu=strrchr('.ru',strtolower($sEmail));

if (
$mRu!=false)
  
$mRu=($mRu=='.ru'); 
Not tested.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
35% Off on all Shared/Reseller Packages - 2 GB / 35 GB Starting at $3.22 headnucleon General Advertisements 0 03-25-06 09:19 PM
Xtreme-WebhostฎUp to 2GB Free Web Hosting Jason7fd General Advertisements 0 12-06-05 10:37 AM
S * A * L * E --Shared Hosting Blowouts, Reseller Plans, and More! thehostrack General Advertisements 0 09-29-05 11:13 PM
S • A • L • E --Shared Hosting Blowouts, Reseller Plans, and More! thehostrack General Advertisements 0 09-09-05 12:55 AM


All times are GMT -5. The time now is 01:06 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.