adrianTNT Posted October 18, 2007 Share Posted October 18, 2007 I have this code, the strpos in the function should return false when string is not found, correct? But instead it seems to return false for found string? What am I soing wrong? I can use the code like this too but it looks worong. <?php function check_referer($the_referer_to_check){ // list of urls to remove from referers list when reading the list of referers $bad_referers = array('yahoo','google','.mail.live.com','search.msn','abv','google.de','google.se'); for($i=0;$i<count($bad_referers);$i++){ // if string not found then value is FALSE, correct? // so why does it return values for *found values? if(strpos($the_referer_to_check, $bad_referers[$i]) == 'FALSE'){ echo $bad_referers[$i].' NOT found in ' .$the_referer_to_check.'<br>'; } } } check_referer('google.com'); check_referer('site.com'); // this one is not in the bad_referers check_referer('abv.bg'); ?> Returns: google NOT found in google.com abv NOT found in abv.bg Quote Link to comment https://forums.phpfreaks.com/topic/73842-solved-strpos-returns-false-for-found-values-it-should-return-false-for-not-found/ Share on other sites More sharing options...
BlueSkyIS Posted October 18, 2007 Share Posted October 18, 2007 add another = and unquote 'FALSE'. it should be === FALSE. with the quotes, you're matching to the literal string "FALSE", not the binary comparison of true vs. false. Quote Link to comment https://forums.phpfreaks.com/topic/73842-solved-strpos-returns-false-for-found-values-it-should-return-false-for-not-found/#findComment-372547 Share on other sites More sharing options...
Orio Posted October 18, 2007 Share Posted October 18, 2007 Because google in google.com returns 0, meaning the string "google" was found starting from char number 0- at the start. To avoid that, use the strict comparison: if(strpos($the_referer_to_check, $bad_referers[$i]) === FALSE) Notice the three '='. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/73842-solved-strpos-returns-false-for-found-values-it-should-return-false-for-not-found/#findComment-372548 Share on other sites More sharing options...
tibberous Posted October 18, 2007 Share Posted October 18, 2007 If it... == 'FALSE'? Like, the string, FALSE? Loose the quotes, and use the !== operator. The !== operator checks type and value, not just converted value. <?php function check_referer($the_referer_to_check){ $bad_referers = array('yahoo','google','.mail.live.com','search.msn','abv','google.de','google.se'); for($i=0;$i<count($bad_referers);$i++) if(strpos($the_referer_to_check, $bad_referers[$i]) !== FALSE) return TRUE; return FALSE; } echo (check_referer('google.com') ? "Bad" : "Good"), "\n"; echo (check_referer('site.com') ? "Bad" : "Good"), "\n"; echo (check_referer('abv.bg') ? "Bad" : "Good"), "\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/73842-solved-strpos-returns-false-for-found-values-it-should-return-false-for-not-found/#findComment-372564 Share on other sites More sharing options...
adrianTNT Posted October 18, 2007 Author Share Posted October 18, 2007 Thanks for the replies guys, yes, I needed === (three of them), !== actually did exactly what I wanted. I receive fast accurate replies here (like always). Great community. Quote Link to comment https://forums.phpfreaks.com/topic/73842-solved-strpos-returns-false-for-found-values-it-should-return-false-for-not-found/#findComment-372695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.