J
J
JackShcherbakov2018-02-04 20:41:28
PHP
JackShcherbakov, 2018-02-04 20:41:28

How to correctly pass the 6th argument to the setcookie() function and how to understand it?

ATTENTION! Below is a stream of noob questions!
Hello! I'm using openserver and don't have a complete understanding of the domain name and server name. As far as I know the domain provided to the setcookie() function must match the end of the server name. But what is my server name then? asd? As the 6th argument, you can specify the domain to which these cookies will be available.
My "site" is located along this path http://asd/.(what kind of path is this in general? The path to the server? Then what is my server name?)
I write like this -

setcookie("cookiewithdomain", "ralph", 0, "/files.php", ".asd"); //С точкой не работает

I write like this and it works
setcookie("cookiewithdomain", "ralph", 0, "/files.php", "asd"); //Без точки  работает

What is the effect of this argument? And why does the domain provided to the setcookie() function have to match the end of the server name? What is the relationship between domain and server name?
I would be eternally grateful for a clear, clear answer.
Please do not throw a link to the documentation, I have already seen it)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2018-02-04
@BorisKorobkov

RTFM ru2.php.net/manual/ru/function.setcookie.php
There is a clear description of what path, domain are and what values ​​to specify there.
Come on?
Your question:
Open the documentation and read:

path
Path to the directory on the server from which the cookies will be available. If set to '/', cookies will be available in the entire domain. If set to '/foo/', cookies will only be available from the /foo/ directory and all its subdirectories (for example, /foo/bar/) of the domain. The default value is the current directory where the cookie is set.
domain
(Sub) domain to which cookies are available. Specifying a subdomain (eg 'www.example.com') will make the cookie available in it and all its subdomains (eg w2.www.example.com). To make cookies available for an entire domain (including subdomains), you just need to specify the domain name (i.e. 'example.com').

Your question:
Open the documentation and read:
secure
Specifies that the cookie value should be transmitted from the client over a secure HTTPS connection. If set to TRUE, the cookie will be passed from the client to the server only if a secure connection is established.

J
John Doe, 2018-02-04
@rabbit418

session_start();
$currentCookieParams = session_get_cookie_params();
$sidvalue = session_id();

if(!isset($_COOKIE['PHPSESSID'])) {
  setcookie(
    'PHPSESSID', //name  
    $sidvalue, //value  
    0, //expires at end of session  
    $currentCookieParams['path'], //path  
    $currentCookieParams['domain'], //domain  
    true //secure  
  );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question