PHP Cookies Host Name


Hello i have a question it may be stupid
i wanted to use php cookies but in the parameters of the function i shuld type the host name
to secure the cookies…in xampp serever i used localhost it worked but after uploading the website it doesnt work
what is the host name here or how can i get it ??

In PHP, use the $_SERVER superglobal like this:

$hostname = $_SERVER['SERVER_NAME'];

Then in setcookie(), use $hostname as the hostname.

EDIT: Example:

setcookie('cookie_name', 'cookie_value', time() + (86400 * 30), $_SERVER['SCRIPT_URI'], $_SERVER['SERVER_NAME'], true, false); //86400 is the number of seconds in a day.

I hope you are asking about MySQLhost or server name which is equalent to localhost you mentioned above.

Here is the Admin reply for getting that.


i tried it didnt work this is the code

Does this help? Sorry I’m not good at .php (learning basics though!):

PHP: $_SERVER - Manual

no i’m sorry
i’m asking to know the host name to use it in php cookies
i connected the website to database successfully

What do you mean by “it didn’t work”? What do you actually see?

I connected to database successfully this is not the problem I’m asking for the host name to use in php cookies

What is the error?

i cant set the cookie because i want to know the host name

I think you should mention domain name instead of localhost. in your case

Can you please share the code used for create cookie then only we can solve!

1 Like

it didnt work this is the code
setcookie(‘TEST’, ‘cookie_value’, time() + (86400 * 30), “/”, “”, true, true);

Try like this

setcookie('test', 'lang-en', time() + 96400, '/', '');  

use proper and complete syntax:
setcookie(name, value, expire, path, domain, secure, httponly );


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.