简体   繁体   中英

Redirect from Wordpress to Laravel if user logged into Laravel?

I have the following situation:

  • an Apache server on a Linux machine
  • a Wordpress site at domain.com
  • a Laravel application at software.domain.com

If a user is logged into the Laravel application, and they visit the Wordpress site, they should be redirected back to the Laravel application.

Is there a way to accomplish this?

I've been considering cookies, but in this case, the Laravel app would have to create a cookie, and the Wordpress site should be able to read it somehow.

Possibly some type of API call from Wordpress to Laravel could work, but I'm not sure how to set that up on the Wordpress side.

Perhaps an .htaccess solution is also possible, but I don't see how.

Yes, cookie would be the most suitable approach to achieve this. Since security is not really a concern, then it's even easier.

Once logged in from Laravel, you can set a cookie which can identify if the user is logged in or not. If you're using Laravel's authentication scaffolding, you'll be doing this in your authenticated() function.

protected function authenticated(Request $request)
{
    // cookie('name', 'value', $minutes, $path, $domain, $secure, $httpOnly)
    return response('Logged in.')->cookie('logged_in', true, 120);
}

Make sure you've set your .env with SESSION_DOMAIN=.domain.com , so that the cookie is at the top level of the domain rather than subdomain. If for any reason you need to set your SESSION_DOMAIN to the subdomain, you'll then need to pass the top-level domain into the parameter. Also remember to remove the cookie once the user logout. If you're using Laravel scaffolding, use the loggedOut() function.

Now you can have your WordPress read this cookie and do the redirect if the logged_in cookie is set. Alternatively, you could do the redirection using Apache .htaccess .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM