Internal Server Error: strpos(): Empty needle
When we navigate to one of our website pages, we receive the following error message:
Internal Server Error
strpos(): Empty needle
What can we do to make this error go away and make the page accessible again?
2 Replies
When you run into this type of error, it typically indicates that there's a server-side issue. It's usually a good idea to review your web server logs for additional clues.
In this case, it looks like you received a unique error message that is related to the strpos string function in PHP:
strpos(): Empty needle
From the PHP documentation I linked above, it looks like this function allows you to find the position of the first occurrence of a substring in a string. If you look over the Parameters section, you'll see that the function requires a few parameters. The haystack parameter is the string you're searching in, and the needle parameter is the substring you're looking for.
The error you received seems to indicate that the needle parameter is empty in the function you're using on your website, so you'll likely want to take a look at the strpos function in the relevant PHP file to spot the exact issue.
If your project uses composer
( https://getcomposer.org ), you can do this…
- Add this to your
composer.json
file:
{
"require": {
...,
"filp/whoops": "^1.1",
...
}
}
Run
composer update
.In your
index.php
, add the following somewhere near the top (but after yourequire "autoload.php"
):
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();
Now, when stuff like this happens, you'll get a nice tombstone in your browser with a backtrace. Once you're done debugging/testing, you can comment out the 3 lines in index.php
to return to the way you were (it's ok to leave the composer.*
files the way they are).
See https://github.com/filp/whoops
DANGER WILL ROBINSON!!! DANGER! DANGER!
Doing this may expose other bugs in your code you didn't know were there. PHP does a lot of stuff silently that a lot of coders take for granted. I don't suggest doing this on a production site.
-- sw