rohitarora
rohitarora

Reputation: 1365

how to set domain in setcookies()

I have tried setting cookies but the problem is in setting domain. When i tried to set domain in setcookies() it doesn't set any value. Without domain setting it will automatically set my domain (for ex. localhost).

If I am using any .com it will set it by default but I cannot set domain in cookies.

Can any one please help me setting domain in php.

  setcookie('session_id',$sessionID[1],strtotime($expireTime[1]),'/',$domain);

When I set it without domain it sets the cookies to my localhost or on which domain I am.

Can any one help me.

Upvotes: 2

Views: 157

Answers (2)

Alain
Alain

Reputation: 37004

If you put a domain to setcookie, you'll see in your header that PHP has set your cookie with the right domain name. But, your browser will just ignore it for safety reasons.

If you need to set a cookie for an auto-login or such, you need to play with your hosts file to make your browser believe that you're on the same domain than the domain where you want to set a cookie.

Example :

If you add :

127.0.0.1   autologin.amazon.co.uk

in your hosts file, and go to http://autologin.amazon.co.uk instead of http://localhost, your remote script will be allowed to set any .amazon.co.uk cookie.

Upvotes: 1

NappingRabbit
NappingRabbit

Reputation: 1918

you can not set a cookie attributed to a domain other than that you are using. this is generally considered a good thing.

Upvotes: 1

Related Questions