samedi 9 mars 2019

Is this a bug in PHP strpos()?

Not wanting to keep sending emails while developing and testing, i have this code in PHP/Laravel 5.4

public function register(Request $request) {

    // validation and registration code here

   if (strpos($request->getHost(), 'localhost') == false) {
      \Illuminate\Support\Facades\Log::info($request->getHost());
      Mail::to($user->email)->send(new AdminConfirmAccount($user->firstname,$url));
   }

}

This problem is the emails still get sent everytime. Why? According to strpos() documentation, the method returns the position of where the needle exists relative to the beginning of the haystack string (zero been the starting position not 1) or false if the string does not exist in the text to search. The bug shows up when the text to search (haystack) is exactly the same as the text been sought for (needle). In this case $request->getHost() returns 'localhost' exactly. At this time, the method returns 0 which is the starting position of the needle which PHP also interprets as false. Is this a known bug or am I missing something? Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire