0
votes

Is there a way to get the url that was redirected to login url?

Here is the context so that you know why I am asking this question. I start by showing the code I am using. This is how I am registering the security provider:

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'client' => array(
            'pattern' => '^/(en|fr)/client',
            'form' => array('login_path' => "/fr/login", 'check_path' => "/fr/client/login_check"),
            'users' => function () use ($app) {
                return new UserProvider($app['pdoqueries']);
            },
            'logout' => array('logout_path' => '/logout', 'invalidate_session' => true),
            ),
        )
));

Notice here that I am securing English and French urls (/en/client and /fr/client). I did not find a way to make login_path point to the right login url with the right locale, so I am hardcoding it to the French URL.

Now here is the login route:

$app->get('/{_locale}/login', function(Request $request) use ($app, $arrConfig) {
    $referer = $request->headers->get('referer');
    error_log(__FILE__ . ": "  . __LINE__ . ":  " .  __FUNCTION__  ."     : "  . var_export($referer, true));
    return $app['twig']->render('auth/form.twig', array(
        'error'         => $app['security.last_error']($request),
        'last_username' => $app['session']->get('_security.last_username'),
        'title'         => 'Login',
        'assetsUrl'             => $arrConfig['assets_url']
    ));
})
->bind('login');

You can see that I already tried to get the referer from the request headers. It does not work unfortunately. I need a way to get the url that the user tried to access to get the locale from it and show the login form text in that locale. This information must be stored somewhere because after successful login the user is redirected to that url.

Thanks

1

1 Answers

0
votes

I think that what you're really after is something like this : https://stackoverflow.com/a/25651658/1741150

But in case I misunderstood, here is some tips on the referer :

Maybe the SilexSecurityProvider does expose this setting like Symfony does :

firewalls:
    main:
        # ...
        form_login:
            # ...
            use_referer: true

(Source : http://symfony.com/doc/current/security/form_login.html#using-the-referring-url)

If it doesn't, you can still try to use a good old $_SERVER['HTTP_REFERER'], but keep in mind that not every client sets it :

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

http://php.net/manual/en/reserved.variables.server.php