Empty string returns negative value with a check

Discussion in 'PHP' started by lampie1978, Jun 8, 2007.

  1. #1
    Hi all,

    I am stuck on this stupid problem and i don't know how to solve the problem :confused:

    What did I create:
    I created a form that posts several variables, each variable that the form posts needs to be checked to protect the DB. After the check turns out ok then the DB is updated with the value of the variable.

    The problem is that the variables that are optional and have no data are returned false and thus i cann't update the DB. I tried to skip the check when the variable is empty, but that doesn't work correctly.

    Who can help, since the form has many optional variables I'll just post one.

    Before i start the form:
    
    if ($_POST[voorvoegsel] == '') { $voorvoegsel = ' '; } else { $voorvoegsel = $_POST[voorvoegsel]; }
    
    PHP:
    The form:
    
    <form name="persoonlijk" method="post" action="persoonlijk.php">
    <input type="text" name="voorvoegsel" maxsize="10">
    <input type='submit' name='updaten' value='Updaten'>
    </form>
    
    HTML:
    After the button update:
    
    function anti_injection($voorvoegsel)
    {
        $verboden = array("bla", "bla");
    
        if ($voorvoegsel != ' ')
        {
        if (eregi("[a-zA-Z0-9]+", $voorvoegsel)) { $voorvoegsel = trim(str_replace($verboden, '', strtolower($voorvoegsel))); } else { $voorvoegsel = HACK; echo "voorvoegsel:" .$voorvoegsel. "<br>"; }
        }
    
        $array = array('voorvoegsel'=>$voorvoegsel);
    
        if (in_array(HACK, $array)) { die ('Sorrij uw hackpoging is mislukt.'); } else { return $array; }
    }
    
    PHP:
    After this the array continously returns the HACK for this variable.
    Who knows what to do because i'm nearing the state of becoming crazy

    Thanx
     
    lampie1978, Jun 8, 2007 IP
  2. donteatchicken

    donteatchicken Well-Known Member

    Messages:
    432
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    118
    #2
    Put this at the top:



    $voorvoegsel = $_REQUEST["voorvoegsel"];
    Code (markup):
     
    donteatchicken, Jun 8, 2007 IP
  3. lampie1978

    lampie1978 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nope, still the same old song
     
    lampie1978, Jun 8, 2007 IP
  4. donteatchicken

    donteatchicken Well-Known Member

    Messages:
    432
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    118
    #4
    is register globals enabled?

    have u tried with session variables?
     
    donteatchicken, Jun 8, 2007 IP
  5. lampie1978

    lampie1978 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Jip globals work, I allready used them on other pages.

    Didn't try session, but can't use it either. I am allready using them for other purposes, for login and abstracting user data from DB for the different pages.
     
    lampie1978, Jun 8, 2007 IP
  6. Nefarious

    Nefarious Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'm not sure but shouldn't there be quotes on the post var?
    $_POST['voorvoegsel']

    not sure if this will solve it but was something that stood out to me.
     
    Nefarious, Jun 8, 2007 IP
  7. SeLfkiLL

    SeLfkiLL Active Member

    Messages:
    85
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    50
    #7
    Make sure you check that the variable is set first:

    isset($_POST['var']) //if the variable isn't set, this will return false
    Code (markup):
    Then you can make sure it's not empty:

    empty($_POST['var']) //if the variable is empty, this will return true
    Code (markup):
     
    SeLfkiLL, Jun 8, 2007 IP
  8. Free Directory

    Free Directory Peon

    Messages:
    89
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Simple ways are always the best.
    Advice of other functionalities of php array functions:
    array_key_exists, strlen;)
    But, the problem it's in the in_array(HACK,$array)
    As HACK it's a constant, you must define it somewhere, no?
    Your function will return the array if $voorvoegsel != HACK.
    be sure you call the function with proper parameter: anti_injection(@$_POST['voorvoegsel ']);
     
    Free Directory, Jun 8, 2007 IP
  9. fsmedia

    fsmedia Prominent Member

    Messages:
    5,163
    Likes Received:
    262
    Best Answers:
    0
    Trophy Points:
    390
    #9
    or you could try using if ( !empty ($_POST['blah blahb lah')) { .. }
     
    fsmedia, Jun 8, 2007 IP