Why is the strpos function always returning a false?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Franco Cassar
    New Member
    • Jan 2011
    • 13

    Why is the strpos function always returning a false?

    Ok, so I need to make the PHP form detect whether a string starts with the substring "DonVito:" or not. However, it's always returning a false even when DonVito: is present.

    Code:
    <?php
    $host="localhost"; // Host name
    $username="****"; // Mysql username
    $password="***"; // Mysql password
    $db_name="*******"; // Database name
    $tbl_name="post"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("ERROR: Cannot connect to MySQL Server on 'localhost'.<br/><br/><i>Remember this is only a prototype demo!</i>");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // username and password sent from form
    $postcontent=$_POST['like_content'];
    
    // To protect MySQL injection (more detail about MySQL injection)
    $postcontent = stripslashes($postcontent);
    $postcontent = mysql_real_escape_string($postcontent);
    
    $sql="INSERT INTO $tbl_name (post_content) VALUES('$postcontent')";
    mysql_query($sql)or die("database error");
    $homeID = "DonVito:";
    
    if (strpos($postcontent, $homeID)===true){
        header("location:result.php?p=$postcontent&mail=Sent");
    }
    else{
    //redirect back to site
    header("location:result.php?p=$postcontent&debug=noMailSent");
    }
    ?>
    Thanks. Also, I'd like it in a way that it returns true only if DonVito: is at the beginning of the string, and not anywhere else in the String (ex. middle etc)
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    The issue here is that strpos does not return a boolean true if the string is found. It does however return boolean false if the string is not found. strpos will return the position at which the string was found. So, with that in mind, to check that a string is at the beginning of another string, we might do:
    [code=php]
    # Note: we're using strong equality here.
    # false == 0 -> true
    # false === 0 -> false
    if ( strpos( $string, $needle ) === 0 )
    {
    # $needle was found at beginning of $string
    }
    [/code]

    Comment

    • Franco Cassar
      New Member
      • Jan 2011
      • 13

      #3
      Thanks. However it's still unsolved.
      Code:
      $homeID = "DonVito:";
      
      if ( strpos( $postcontent, $homeID ) === 0 ){
          header("location:result.php?p=$postcontent&mail=Sent");
      }
      else{
      //redirect back to site
      header("location:result.php?p=$postcontent&debug=noMailSent");
      }
      Still sending me to result.php?p=$p ostcontent&debu g=noMailSent instead of result.php?p=$p ostcontent&mail =Sent.

      Any clue? :S

      $postcontent is correct because it does replace $postcontent correctly in the parameter p=

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        A call to header() adds headers to the HTML response to an HTML request. That's all it does. It doesn't abort/close the script/request. So, successive calls to header() will either overwrite the previous header (of the same type) or the last header will take precedence - I'm not sure what happens here (I'll investigate when I have chance).

        With that in mind, you have to manually terminate the script by calling exit after the header call.

        Edit: nevermind - I misread what your reply.

        Comment

        • Franco Cassar
          New Member
          • Jan 2011
          • 13

          #5
          Nah doing exit(); after the header makes no difference.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            If strpos is not returning 0, then your search string is not at the beginning of the content string.

            Post the output of the following:
            Code:
            var_dump( $postcontent );
            var_dump( strpos( $postcontent, $homeID ) );

            Comment

            • Franco Cassar
              New Member
              • Jan 2011
              • 13

              #7
              Okay, I submit "DonVito" in the form and I get this output:

              string(7) "DonVito" bool(false)

              Comment

              • Franco Cassar
                New Member
                • Jan 2011
                • 13

                #8
                If I submit "dad" I get this output:

                string(3) "dad" bool(false)

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Which makes sense. The string "DonVito:" is in neither of those.

                  Comment

                  • Franco Cassar
                    New Member
                    • Jan 2011
                    • 13

                    #10
                    Awesome. How could I have missed that! Thanks :)

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Happens to all of us!

                      Comment

                      Working...