Ad

Route Protection From Multiple Referrer - Laravel

- 1 answer

I'm trying to protect a route if not accessed from a particular page and have the below piece of code that doesn't work as expected;

$referer = Request::header('referer');

if ($referer != "http://www.xxx.co.uk/login" || $referer != "http://www.xxx.co.uk/signup") {
    return redirect()->route('/');
}

I'm doing something wrong with my if statement because the below works;

$referer = Request::header('referer');

if ($referer != "http://www.xxx.co.uk/login") {
    return redirect()->route('/');
}

The difference being that I removed the or part. But I need it protected if not accessed from both paths. Also adding another else if statement breaks it aswell

Ad

Answer

You have to use && operator instead of ||.

In your case, using ||, when a single condition is falsy, the other is certain truthy, so your statement will be always executed.

Ad
source: stackoverflow.com
Ad