need an experienced coder asap ! (php regular expressions)

Discussion in 'Programming' started by Shimurai, Nov 29, 2011.

  1. #1
    hi,

    i am trying to get a simple form done and I need to validate an input which stands for a title, this is the code I currently have:

    if(!eregi('^[a-zA-Z0-9 ._-]+$',$_POST['editsite_4'])) {
    echo "some error";
    }
    PHP:
    it's working fine, but I would like to allow some more characters like: & * ] # ! [ : /

    I tried to add the characters there but always getting an error:
    but keep it secure because that variable will go to a database..

    characters i don't want to allow: ' = "

    I am paying $5 for this simple code, you can reply directly to this thread if you wish and pm me your paypal account.
     
    Shimurai, Nov 29, 2011 IP
  2. ChiChiBaChi

    ChiChiBaChi Active Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    ChiChiBaChi, Nov 29, 2011 IP
  3. Shimurai

    Shimurai Well-Known Member

    Messages:
    186
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    As Seller:
    100% - 2
    As Buyer:
    100% - 0
    #3
    I'm also using mysql_real_escape_string() before adding to the database, should I also use the addslashes() function ?
     
    Shimurai, Nov 29, 2011 IP
  4. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    No mysq_real_escape_string is better than addslashes
     
    Einheijar, Nov 29, 2011 IP
  5. ChiChiBaChi

    ChiChiBaChi Active Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    thats better
     
    ChiChiBaChi, Nov 29, 2011 IP
  6. omgitsfletch

    omgitsfletch Well-Known Member

    Messages:
    1,222
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    145
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #6
    if (preg_match('@^[&\*#!:/a-zA-Z0-9 ._-]+$@', $title))
    Code (markup):
    ChiChi's solution is poor because it only discounts the = character. It is better to use a whitelist regex as that way you can be confident with what characters are allowed. Have you seen the UTF-8 character set? There are THOUSANDS of characters, it's better to allow only a few than disallow one character. The reason you had out of range errors is that some of your allowed characters are special characters that have meaning within a regex. When you want to use the literal representation of that character (with no special meaning), you need to use the \ character before it to escape it so that the system understands what you mean.

    PayPal
     
    Last edited: Nov 30, 2011
    omgitsfletch, Nov 30, 2011 IP