# PHP

Here's an example of integrating the use of the First-id system in PHP Symfony.

  • We check if the user has just been redirected from First-id and intercept the firstId query parameter in the URL to create the cookie.
  • If the user has given consent but does not yet have a First-id cookie, we forge the redirection URL and redirect them to the First-id system.

Cookie configuration

You have to create a Secure cookie on your main domain (see Cookie deposit).

HTTPOnly

To retrieve the firstid cookie in client-side JavaScript, we do not recommend setting the httpOnly attribute.

  • PHP Symfony
 <?php

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;

class FirstIdCookieSubscriber implements EventSubscriberInterface
{
    public function onKernelRequest(RequestEvent $event): void
    {

        $request = $event->getRequest();
        // check if we get "firstId" query param in URL (when user has redirected)
        if ($request->query->has('firstId') && $request->query->has('redirectUri')) {
            $firstId = $request->query->get('firstId');
            $redirectUri = $request->query->get('redirectUri');
            // create firstId cookie and redirect user without the query params "firstid"
            // build the redirect url without any query string
            $urlToRedirect = $request->getSchemeAndHttpHost().urldecode($redirectUri);

            // remove firstId query string
            $params = $request->query->all();
            unset($params['firstId']);
            unset($params['redirectUri']);

            $finalUrl = $urlToRedirect;
            if (!empty($params)) {
                $finalUrl .= '?'.http_build_query($params);
            }
            $response = new RedirectResponse(
                $finalUrl
            );
            $response->headers->setCookie(
                new Cookie(
                    'firstid', // cookie name
                    $firstId, // cookie value
                    time() + (60*60*24*180) // cookie expiration, here 180 days
                )
            );

            $event->setResponse($response);
            $event->stopPropagation();
            return;
        }

        // check is cookie consent is accept by user
        // maybe adjuste the value cookie consent name and value
        if ($request->cookies->has('YOUR_CONSENT_COOKIE') && $request->cookies->get('YOUR_CONSENT_COOKIE') === 'yes') {
            //if user not have a First-id cookie, redirect to First-id system
            if (!$request->cookies->has('firstid')) {
                $redirectHostUrlEncode = urlencode($request->getSchemeAndHttpHost());
                $redirectUriUrlEncode = urlencode($request->getRequestUri());

                $event->setResponse(new RedirectResponse(
                   "https://gate.first-id.fr?redirectHost=$redirectHostUrlEncode&redirectUri=$redirectUriUrlEncode",
                    Response::HTTP_FOUND // return a 302 HTTP Code
                ));
                $event->stopPropagation(); // force Symfony to return response
            }
        }
    }

    public static function getSubscribedEvents(): array
    {
        return [
            'kernel.request' => 'onKernelRequest',
        ];
    }
}

URL encoding

The redirectHost and redirectUri parameters must be encoded in urlEncode to be correctly interpreted.

Preserve User Referrer

To preserve the referrer of my user, I must take several actions described here.

Excluding Redirections

Warning, you should not perform redirections for the following file types:

jpe?g|gif|png|json|js|woff2?|map|css|svg|ico|bmp|xml|ttf|txt

Please add a filter on these file types in your configuration.