Session IF

Discussion in 'PHP' started by Agent_Smith, May 25, 2008.

  1. #1
    Hello,

    Currently making a captcha, having troubles on it checking if its correct on my php process page.

    Code:

    $code = strip_tags(trim($_POST['code']));
    
    if(!$_SESSION['captcha'] == $code) {
          echo "ERROR";
          unset($_SESSION['captcha']);
          exit();
       } else {
    //do my action
    }
    
    PHP:
    For some reason, its just always doing the action if its correct, even if i enter the wrong code. I've also echo'ed both the session and code just to check they are going through.

    Thanks
     
    Agent_Smith, May 25, 2008 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    $code = strip_tags(trim($_POST['code']));
    
    if($_SESSION['captcha'] != $code) {
          echo "ERROR";
          unset($_SESSION['captcha']);
          exit();
       } else {
    //do my action
    }
    
    PHP:
    You probably don't need to striptags on the _POST
     ... seems a pointless operation ...
    Code (markup):
     
    krakjoe, May 25, 2008 IP
    Agent_Smith likes this.
  3. Agent_Smith

    Agent_Smith Well-Known Member

    Messages:
    890
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    145
    #3
     
    Agent_Smith, May 25, 2008 IP
  4. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #4
    You have to consider that ! operator has a higher precedence than ==.

    So
    if(!$_SESSION['captcha'] == $code)
    PHP:
    and
    if(!($_SESSION['captcha'] == $code))
    PHP:
    will give the different results.
     
    wmtips, May 25, 2008 IP