How to create IP blacklist in Symfony2?

前端 未结 6 1310
刺人心
刺人心 2021-02-08 17:53

Yes, I know there\'s Voter tutorial in cookbook. But I\'m looking for something slightly different. I need two different layers of blacklisting:

  1. deny certain IP to
6条回答
  •  情话喂你
    2021-02-08 18:09

    It's not a best practice. Insight (analyse by Sensio) returns : "Using PHP response functions (like header() here) is discouraged, as it bypasses the Symfony event system. Use the HttpFoundationResponse class instead." and "$_SERVER super global should not be used."

    getClientIp();
    $authorized_hosts = ['127.0.0.1', 'fe80::1', '::1', 'localhost', 'yourIpAddress'];
    
    // Securisation
    if (!in_array($client_ip, $authorized_hosts)) {
        $response = new Response(
            "Forbidden",
            Response::HTTP_FORBIDDEN,
            array('content-type' => 'text/html')
        );
        $response->send();
        exit();
    }
    
    $kernel = new AppKernel('prod', false);
    $kernel->loadClassCache();
    $response = $kernel->handle($request);
    $response->send();
    $kernel->terminate($request, $response);
    

    It's ok for SensioInsight

提交回复
热议问题