June 21, 2013

PHP ['SCRIPT_NAME'] returning empty string

I was working with Slim (v2.2.x) the other day and saw that a PHP warning was being thrown in one of the interal Slim files:

Warning: strpos(): Empty needle in /vendor/slim/slim/Slim/Environment.php on line 143

The code throwing the warning was the following if statement:

if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) {

The actual issue was that $_SERVER['SCRIPT_NAME'] was an empty string, so strpos was complaining as that is invalid syntax. Strangely, this was all working fine when we had the apache rewrite rules in an .htaccess file:

RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

However once we moved the rules to the VirtualHost we got the warning. Some googling told me that this is a 6 year old problem with either apache, php, mod_php or the boogey man…depending on who you believe. It seems that various rewrite settings working differently if you have them in .htaccess vs the VirtualHost block.

I considered sending a pull request to Slim to handle the scenario where SCRIPT_NAME is empty:

if (strlen($_SERVER['SCRIPT_NAME']) && strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) {

But instead we added the PT (passthrough) rewrite flag. This works fine for what we’re doing and $_SERVER['SCRIPT_NAME'] now returns /index.php as it should.

RewriteRule ^(.*)$ /index.php/$1 [QSA,PT,L]

© Michael Sharman 2017