简体   繁体   English

如果用户登录到 Laravel,则从 Wordpress 重定向到 Laravel?

[英]Redirect from Wordpress to Laravel if user logged into Laravel?

I have the following situation:我有以下情况:

  • an Apache server on a Linux machine Linux 机器上的 Apache 服务器
  • a Wordpress site at domain.com域 Wordpress 站点 com
  • a Laravel application at software.domain.com software.domain.com 上的 Laravel 应用程序

If a user is logged into the Laravel application, and they visit the Wordpress site, they should be redirected back to the Laravel application.如果用户登录到 Laravel 应用程序,并且他们访问 Wordpress 站点,他们应该被重定向回 Laravel 应用程序。

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.我一直在考虑 cookies,但在这种情况下,Laravel 应用程序必须创建一个 cookie,而 Wordpress 网站应该能够以某种方式读取它。

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.可能某种类型的 API 从 Wordpress 到 Laravel 的调用可以工作,但我不确定如何在 Wordpress 端进行设置。

Perhaps an .htaccess solution is also possible, but I don't see how.也许.htaccess解决方案也是可能的,但我不知道如何。

Yes, cookie would be the most suitable approach to achieve this.是的,cookie 将是实现这一目标的最合适方法。 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.从 Laravel 登录后,您可以设置一个 cookie ,该 cookie 可以识别用户是否登录。 If you're using Laravel's authentication scaffolding, you'll be doing this in your authenticated() function.如果您正在使用 Laravel 的身份验证脚手架,您将在您的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.确保您已将.env设置为SESSION_DOMAIN=.domain.com .domain.com ,以便 cookie 位于域的顶级而不是子域。 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.如果出于任何原因需要将SESSION_DOMAIN设置为子域,则需要将顶级域传递到参数中。 Also remember to remove the cookie once the user logout.还请记住在用户注销后删除 cookie。 If you're using Laravel scaffolding, use the loggedOut() function.如果您使用 Laravel 脚手架,请使用loggedOut() function。

Now you can have your WordPress read this cookie and do the redirect if the logged_in cookie is set.现在你可以让你的 WordPress 读取这个 cookie 并在设置了logged_in cookie 的情况下进行重定向。 Alternatively, you could do the redirection using Apache .htaccess .或者,您可以使用 Apache .htaccess进行重定向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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