Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active May 4, 2022 17:04
Show Gist options
  • Save Shelob9/5d5fe23c84d7bfdd68572c183903438f to your computer and use it in GitHub Desktop.
Save Shelob9/5d5fe23c84d7bfdd68572c183903438f to your computer and use it in GitHub Desktop.
A Laravel middleware to prevent Internet Explorer from accessing route(s)
<?php
namespace App\Http\Middleware;
use Closure;
use UserAgentParser\Exception\NoResultFoundException;
use UserAgentParser\Provider\WhichBrowser;
/**
* Class DeviceDetect
*
* A middleware to prevent Internet Explorer from being used.
*
* README: In our app we only have one real HTML generating route -- it loads the VueJS front-end -- so this is only applied to that one route. I'd worry about applying this on every page load.
*
* @package App\Http\Middleware
*/
class NoIE
{
/** @var WhichBrowser */
protected $whichBrowser;
public function __construct( WhichBrowser $whichBrowser )
{
$this->whichBrowser = $whichBrowser;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
try {
/* @var $result \UserAgentParser\Model\UserAgent */
$result = $this->whichBrowser->parse($_SERVER['HTTP_USER_AGENT']);
} catch (NoResultFoundException $ex){
return $next($request);
}
if( false !== strpos( $result->getBrowser()->getName(), 'Internet Explorer' ) ) {
return redirect( '/browser-fail' );
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment