简体   繁体   English

PHP如何在不使用函数setcookie()或setrawcookie()的情况下将PHPSESSID设置为$ _COOKIE超全局变量?

[英]How does PHP set the PHPSESSID into $_COOKIE superglobal variable without using the function setcookie() or setrawcookie()?

I'm learning PHP's most important and difficult to understand concepts viz. 我正在学习PHP最重要和最难理解的概念,即。 "Session and Cookies" “会话和Cookie”

As per my understanding cookies can only be set into the browser by using either of setcookie() or setrawcookie() functions. 根据我的理解,只能使用setcookie()setrawcookie()函数将cookie设置到浏览器中。

As per my understanding of session process, it works in following way. 根据我对会话过程的理解,它的工作方式如下。

  • Every session is started by using the session_start( ) function. 通过使用session_start()函数来开始每个会话。
  • The session_start( ) function must come before any HTML, including blank lines, on the page. session_start()函数必须位于页面上任何HTML(包括空行)之前。
  • The session_start( ) function generates a random Session Id and stores it in a cookie on the user's computer (this is the only session information that is actually stored on the client side.) session_start()函数生成一个随机的会话ID,并将其存储在用户计算机上的cookie中(这是唯一实际存储在客户端的会话信息。)
  • The default name for the cookie is PHPSESSID . Cookie的默认名称是PHPSESSID To reference the session Id in my PHP code, I would therefore reference the variable $PHPSESSID (it's a cookie name) 为了在我的PHP代码中引用会话ID,因此,我将引用变量$ PHPSESSID(这是一个cookie名称)

Please correct me if my understanding is wrong anywhere in above explanation. 如果以上说明中我的理解有误,请纠正我。

My doubt is as session is setting some value in $_COOKIE superglobal it's nowhere using any of the functions setcookie() or setrawcookie() then still how could session id gets stored as a cookie variable? 我的疑问是,由于会话正在$_COOKIE超全局变量中设置一些值,因此无法使用setcookie() or setrawcookie()任何函数,那么会话ID仍如何存储为cookie变量?

If I assume that the session id is set as a cookie variable and setccookie() or setrawcookie() might have been called internally then what are the parameter values set while calling either of the functions setccookie() or setrawcookie() to set the cookie value? 如果我假设将会话ID设置为cookie变量,并且内部可能已调用setccookie() or setrawcookie()那么在调用函数setccookie() or setrawcookie()来设置cookie时设置的参数值是什么?值?

Thank You. 谢谢。

The default name for the cookie is PHPSESSID . Cookie的默认名称是PHPSESSID To reference the session Id in my PHP code, I would therefore reference the variable $PHPSESSID 要在PHP代码中引用会话ID,因此,我将引用变量$PHPSESSID

No, it is just the name of the cookie, it will not be set as a global variable. 不,它只是cookie的名称,不会将其设置为全局变量。 You can access the value in $_COOKIE['PHPSESSID'] . 您可以在$_COOKIE['PHPSESSID']访问值。 But really, this should not concern you at all, you should only use the session_* functions and the $_SESSION superglobal to interact with PHP's session API, the underlying cookie being used is none of your concern for most intents and purposes. 但是,实际上,这根本不关心您,您只应使用session_*函数$_SESSION超全局变量与PHP的会话API进行交互,对于大多数意图和目的,所使用的基础cookie都不是您所关心的。

… it's nowhere using any of the functions setcookie() or setrawcookie() …在任何地方都无法使用setcookie()setrawcookie()函数……

If I assume that the session id is set as a cookie variable and setccookie() or setrawcookie() might have been called internally … 如果我假设将会话ID设置为cookie变量,并且内部可能已调用setccookie()setrawcookie()

Yes, PHP is calling some functions internally that will set the cookies. 是的,PHP内部调用了一些将设置cookie的函数。 It's probably neither setcookie nor setrawcookie but some internal C function that sets the cookie. 它可能既不是setcookie也不是setrawcookie而是一些内部C函数来设置cookie。 Again, it's none of your concern really. 再次,这实际上与您无关。 You just need to understand that calling session_start will somehow internally cause a cookie to be set. 您只需要了解调用session_start会在某种程度上内部导致设置cookie。

… then what are the parameter values set while calling either of the functions setccookie() or setrawcookie() to set the cookie value? …那么调用函数setccookie()setrawcookie()设置cookie值时设置的参数值是多少?

Those are determined by the various session.cookie_* parameters you can set via session_set_cookie_params or ini_set . 这些由您可以通过session_set_cookie_paramsini_set设置的各种session.cookie_*参数确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM