Check for entry and prevent duplicate inputs

Discussion in 'PHP' started by Mr.Bill, Feb 10, 2008.

  1. #1
    I have a small subscription script but its missing to vital things. Its does not check to see if the entry exists and it will allow duplicate entries. SO I would like so this script checks if the entry has already been submitted if it hasnt then it adds it. If it does exsist then it gives a nice statement like "you have already subscribed for our newsletter" and does not add it again to the table :)

    
    <?php
    function checkEmail($email)
    {
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email))
    {
    return false;
    }
    $email_array = explode("@", $email);
    $local_array = explode(".", $email_array[0]);
    for ($i = 0; $i < sizeof($local_array); $i++)
    {
    if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i]))
    {
    return false;
    }
    }
    if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1]))
    {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2)
    {
    return false;
    }
    for ($i = 0; $i < sizeof($domain_array); $i++)
    {
    if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i]))
    {
    return false;
    }
    }
    }
    return true;
    }
    
    $subscribed = "";
    
    if ($_REQUEST['Submit'] == "Subscribe")
    {
    require("some.php");
    if ( checkEmail($_REQUEST['email']) == true )
    {
    mysql_connect($db_addr, $db_user, $db_pass) or die(mysql_error());
    mysql_select_db($db_name) or die(mysql_error());
    $md5 = md5(microtime () * mktime());
    $id = substr( $md5,0,8 );
    $query = mysql_query("INSERT INTO `subscribers` ( `mail`, `unsubscribe` )
    VALUES ('$_REQUEST[email]', '$id')")
    or die(mysql_error());
    $subscribed = "<center><font color='#223C57'><u>You have successfuly subscribed for newsletters...</u></font></center><br>";
    }
    else
    {
    $subscribed = "<font color='#FF0000'>Incorrect e-mail address! Please try again...</font>";
    }
    }
    else if ($_REQUEST['tellAFriend'] == "Tell a Friend")
    {
    if ( checkEmail($_REQUEST['fEmail']) == true && checkEmail($_REQUEST['myEmail']) == true)
    {
    $to = $_REQUEST['fEmail'];
    $subject = "Hi, I've found a very interesting website";
    $body = "I've found a great site - YoMama.com and I wanted to tell you about it.
    It's Great!";
    $headers = "From: ".$_REQUEST['myEmail']."\n";
    
    @mail($to,$subject,$body,$headers);
    $subscribed = "<font color='#00FF66'>Thanks ! Your friend now knows about the great website you've found!</font>";
    }
    else
    {
    $subscribed = "<font color='#FF0000'>Incorrect e-mail address! Please try again...</font>";
    }
    }
    
    ?>
    
    PHP:

     
    Mr.Bill, Feb 10, 2008 IP
  2. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #2
    I tried to add this

           if ($subscibers->mail == $mail) {
               displayError("You have already subscribed to our newsletter.");
               exit();
           }
    PHP:
    Got a "displayError" error so I then tried
    
           if ($subscibers->mail == $mail) {
               $subscribed("You have already subscribed to our newsletter.");
               exit();
           }
    PHP:
    Didnt work I am at a lose on this one :(
     
    Mr.Bill, Feb 10, 2008 IP
  3. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Anyone able to help me out on this one?
     
    Mr.Bill, Feb 11, 2008 IP
  4. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's really basic, what you want to do is run a SELECT query, and check if mail=$_REQUEST['mail'] and then check if mysql_numrows($query) > 0 or not. It's really really basic, so I'll let you interpret that and put it all together. And really, you need to be sanitizing all your inputs, you're just begging for someone to come and sql inject the shit out of your server. Instead of just puttin $_REQUEST['mail'] right into your query, do mysql_real_escape_string($_REQUEST['mail']). Also, it's better to use $_GET or $_POST depending on which you want, $_REQUEST is just generic and is even easier to abuse or get negligent with.
     
    projectshifter, Feb 11, 2008 IP
  5. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #5
    If someone can fix this please let me know how much it will cost. Just need this fixed.
     
    Mr.Bill, Feb 11, 2008 IP
  6. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Where did you get the subscription script from? Whithout seeing this, or more of the code that you are using it will be difficult (impossible) to fix.

    Brew
     
    Brewster, Feb 11, 2008 IP
  7. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #7
    It was made for a site of mine. As far as I know this is the complete script. Its all I have and it works. Just doesnt check for duplicate entries
     
    Mr.Bill, Feb 11, 2008 IP
  8. cwboaze

    cwboaze Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    <?php
    function checkEmail($email)
    {
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email))
    {
    return false;
    }
    $email_array = explode("@", $email);
    $local_array = explode(".", $email_array[0]);
    for ($i = 0; $i < sizeof($local_array); $i++)
    {
    if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i]))
    {
    return false;
    }
    }
    if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1]))
    {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2)
    {
    return false;
    }
    for ($i = 0; $i < sizeof($domain_array); $i++)
    {
    if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i]))
    {
    return false;
    }
    }
    }
    return true;
    }
    
    $subscribed = "";
    if ($_REQUEST['Submit'] == "Subscribe")
    {
    require("some.php");
    if ( checkEmail($_REQUEST['email']) == true )
    {
    mysql_connect($db_addr, $db_user, $db_pass) or die(mysql_error());
    mysql_select_db($db_name) or die(mysql_error());
    $md5 = md5(microtime () * mktime());
    $id = substr( $md5,0,8 );
    $sql = "SELECT `mail` FROM `subscribers` WHERE `mail` = '$_REQUEST[email]'";
    $result = mysql_query($sql) or die(mysql_error());
    if(mysql_num_rows($result) > 0) {
               $subscribed("You have already subscribed to our newsletter.");
    		   exit();
    	}
    $query = mysql_query("INSERT INTO `subscribers` ( `mail`, `unsubscribe` )
    VALUES ('$_REQUEST[email]', '$id')")
    or die(mysql_error());
    $subscribed = "<center><font color='#223C57'><u>You have successfuly subscribed for newsletters...</u></font></center><br>";
    }
    else
    {
    $subscribed = "<font color='#FF0000'>Incorrect e-mail address! Please try again...</font>";
    }
    }
    else if ($_REQUEST['tellAFriend'] == "Tell a Friend")
    {
    if ( checkEmail($_REQUEST['fEmail']) == true && checkEmail($_REQUEST['myEmail']) == true)
    {
    $to = $_REQUEST['fEmail'];
    $subject = "Hi, I've found a very interesting website";
    $body = "I've found a great site - YoMama.com and I wanted to tell you about it.
    It's Great!";
    $headers = "From: ".$_REQUEST['myEmail']."\n";
    
    @mail($to,$subject,$body,$headers);
    $subscribed = "<font color='#00FF66'>Thanks ! Your friend now knows about the great website you've found!</font>";
    }
    else
    {
    $subscribed = "<font color='#FF0000'>Incorrect e-mail address! Please try again...</font>";
    }
    }
    
    ?>
    
    PHP:
    Added

    
    <?php
    $sql = "SELECT `mail` FROM `subscribers` WHERE `mail` = '$_REQUEST[email]'";
    $result = mysql_query($sql);
    if(mysql_num_rows($result) > 0) {
               $subscribed("You have already subscribed to our newsletter.");
    		   exit();
    	}
    ?>
    
    PHP:
    After

    
    <?php
    $md5 = md5(microtime () * mktime());
    $id = substr( $md5,0,8 );
    ?>
    
    PHP:
    and Before

    
    <?php
    $query = mysql_query("INSERT INTO `subscribers` ( `mail`, `unsubscribe` )
    VALUES ('$_REQUEST[email]', '$id')")
    or die(mysql_error());
    ?>
    
    PHP:
    Give it a try and let me know!
     
    cwboaze, Feb 11, 2008 IP
  9. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #9
    Gives a "Call to undefined function ()"
     
    Mr.Bill, Feb 11, 2008 IP
  10. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #10
    <?php
    function checkEmail($email) {
      if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
        return false;
      }
      $email_array = explode("@", $email);
      $local_array = explode(".", $email_array[0]);
      for ($i = 0; $i < sizeof($local_array); $i++) {
        if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
          return false;
        }
      }
      if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
        $domain_array = explode(".", $email_array[1]);
        if (sizeof($domain_array) < 2) {
          return false;
        }
        for ($i = 0; $i < sizeof($domain_array); $i++) {
          if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
            return false;
          }
        }
      }
      return true;
    }
    
    $subscribed = "";
    
    if ($_REQUEST['Submit'] == "Subscribe") {
      require("some.php");
      if ( checkEmail($_REQUEST['email']) == true ) {
        mysql_connect($db_addr, $db_user, $db_pass) or die(mysql_error());
        mysql_select_db($db_name) or die(mysql_error());
    
        $query = sprintf("SELECT * FROM subscribers WHERE mail='%s'", mysql_real_escape_string($_REQUEST['email']));
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
          $subscribed = '<span style="color:#FF0000">You have already subscribed to our newsletter.</span>';
        } else {
          $md5 = md5(microtime () * mktime());
          $id = substr( $md5,0,8 );
          $query = sprintf("INSERT INTO subscribers (`mail`, `unsubscribe`) VALUES ('%s', '%s')", mysql_real_escape_string($_REQUEST['email']), $id);
          mysql_query($query) or die(mysql_error());
          $subscribed = '<span style="color:#223C57">You have successfuly subscribed for newsletters...</span>';
        }
      } else {
        $subscribed = '<span style="color:#FF0000">Incorrect e-mail address! Please try again...</span>';
      }
    } else if ($_REQUEST['tellAFriend'] == "Tell a Friend") {
      if ( checkEmail($_REQUEST['fEmail']) == true && checkEmail($_REQUEST['myEmail']) == true) {
        $to = $_REQUEST['fEmail'];
        $subject = "Hi, I've found a very interesting website";
        $body = "I've found a great site - YoMama.com and I wanted to tell you about it. It's Great!";
        $headers = "From: ".$_REQUEST['myEmail']."\n";
    
        @mail($to,$subject,$body,$headers);
        $subscribed = '<span style="color:#00FF66">Thanks ! Your friend now knows about the great website you\'ve found!</span>';
      } else {
        $subscribed = '<span style="color:#FF0000">Incorrect e-mail address! Please try again...</span>';
      }
    }
    ?>
    PHP:
    ...is more or less what projectshifter was suggesting. It includes the mysql_real_escape_string fix and I have swapped out your deprecated font tags for span tags with inline styles.
     
    stoli, Feb 12, 2008 IP
  11. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #11
    stoli Thank you very much, This seems to be working perfectly. I greatly appreciate. If you want something for your time please let me know.
     
    Mr.Bill, Feb 12, 2008 IP
  12. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #12
    No problem Mr.Bill, glad to be able to help.
     
    stoli, Feb 12, 2008 IP
    Mr.Bill likes this.