Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are cookies only sent to http://www.example.com and NOT http://example.com?

Tags:

php

cookies

I have a PHP login which sets 2 cookies once someone login. The problem is that if you login from http://www.example.com and you go to http://example.com, you will find yourself not logged in. I think that is because the browser only send the cookies to the first syntax.

It is only one domain, the difference is the www. before the domain name, so how to set cookies to the whole domain whatever there is www. or not?

<?php setcookie('username',$username,time()+3600); ?>
like image 634
CodeOverload Avatar asked Dec 30 '25 19:12

CodeOverload


1 Answers

setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain)

Use $domain = '.site.com' instead of 'www.site.com'

like image 186
Amy B Avatar answered Jan 01 '26 09:01

Amy B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!