Laravel shared cookie detection issue in domain and subdomain -
i working on laravel 5.4.30.
imagine have domain example.com
, subdomain of dev.example.com
. main domain master branch , dev subdomain develop branch. have cookie notice system hidden after clicking on hide cookie notice
button. works setting cookie forever. have set session_domain
configs each domain each environment.
for main domain:
session_domain=example.com
for dev subdomain:
session_domain=dev.example.com
now issue comes here. if go example.com
, click on hiding cookie notice, cookie set forever main domain. after go dev.example.com
, same. cookie set subdomain well. cookie has been set after previous one. (the order important) if refresh subdomain, see notice again! (not hidden) browser has read main cookie because of .example.com
set in domain
parameter of cookie in browser, every subdomain affected. view still shows notice because cannot read cookie hiding.
anyway don't want share cookie across subdomains. how can achieve that? think should add prefix cookie name. don't know how it, laravel automatically adds prefix cookie name.
any solutions?
you need implement own "retrieving" , "setting" cookie.
retrieving (has, get) cookies
create new class (anywhere like, app/foundation/facades/) name cookie.
use \illuminate\support\facades\cookie cookiestock; class cookie extends cookiestock { //implement own has(...); public static function has($key) { return ! is_null(static::$app['request']->cookie(prefix . $key, null)); //get prefix .env file case app_env } //implement own get(...); public static function get($key = null, $default = null) {...} }
now open config/app.php , change corresponding alias (cookie).
setting (make) cookies
create new provider (use artisan), , copy-paste code illuminate\cookie\cookieserviceprovider.php , change namespaces. again open config/app.php , change corresponding service provider new one.
create new class (anywhere like, app/foundation/cookie/) name cookiejar.
use \illuminate\cookie\cookiejar cookiejarstock; class cookiejar extends cookiejarstock { //override method think relevant (my guess make(), not sure @ moment queue related methods) public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httponly = true) { // check before applying prefix if (!empty($name)) { $name = prefix . $name; // prefix same way before } return parent::make($name, $value, $minutes, $path, $domain, $secure, $httponly); } }
update code in own cookie service provider use implementation of cookiejar (line 19).
run $ composer dump-autoload
, , should done.
Comments
Post a Comment