theminer3746
theminer3746

Reputation: 918

PHP artisan return Undefined index: HTTP_USER_AGENT

I tried to run php artisan in a project folder using Command Prompt. However, this error keeps popping up:

  [ErrorException] 
  Undefined index: HTTP_USER_AGENT

Running the same command in other projects' folder (With Laravel 4.2 and 5.1) worked fine.

From what I've read on Laracast: any service providers need to be commented out. However, in this particular project I don't have any.

Upvotes: 4

Views: 1452

Answers (2)

theminer3746
theminer3746

Reputation: 918

Finally fixed it!

The problem is caused by a line in routes.php. To be specific, the code is designed to filter out IE users by verifying the user agent like this:

 if(preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false))

So with artisan (which runs through the CLI), there is no HTTP_USER_AGENT header. That caused this error:

  [ErrorException] 
  Undefined index: HTTP_USER_AGENT

The fix is quite simple. Just add isset($_SERVER['HTTP_USER_AGENT']) && to the if logic like this:

if(isset($_SERVER['HTTP_USER_AGENT']) && (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false)))

Upvotes: 0

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111869

You should verify your controllers constructors or artisan command constructors. They might be launched when running php artisan and if in any of them you are using directly HTTP_USER_AGENT or launching any piece of code that requires HTTP_USER_AGENT it makes the problem.

Upvotes: 3

Related Questions