PHP Sessions with disabled cookies, does it work?

前端 未结 9 1776
暖寄归人
暖寄归人 2020-11-28 03:58

Today I had skype interview for a job as PHP developer, one of the questions asked was about Cookies and PHP Sessions.

The question was, can PHP session be set and r

相关标签:
9条回答
  • 2020-11-28 04:21

    Yes.. It will Work
    1.PHP will pass one GET parameter in URL with the name PHPSESSID but it can be changed session.name in php.ini file.
    2. It add one hidden input in forms with same name.

    0 讨论(0)
  • 2020-11-28 04:25

    If it was me, I would say "Yes"

    Since you could store session in form / url somewhere to passed to next page (very bad idea). So, based on his question "can PHP session be set and read, used, if Cookies are disabled in users Browser?"

    Then, it should be yes. It can read and used.

    However, If user close browser, then it's gone, and that's it. (since that guy didn't ask about this part)

    0 讨论(0)
  • 2020-11-28 04:26

    "A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL. "

    Sessions: Introduction

    0 讨论(0)
  • 2020-11-28 04:28

    If session.use_cookies = 1 (Cookie enabled.)

    If session.use_cookies = 0 (Cookie disabled.)

    If session.use_cookies = 1 then session stores the sessionId into cookie. Calling session_id() get the stored sessionId from cookie and saved data into session array will be found on all the pages. If session.use_cookies = 0 In this case session does not store sessionId into cookie and you will get each time a new sessionId using session_id() and data stored into session on other pages will not be found on another pages.

    0 讨论(0)
  • 2020-11-28 04:28

    You are right, Session cannot work without cookies. To illustrate this try doing the following actions.

    1. Login To Gmail.
    2. After login disabled the cookies.
    3. Refresh the page.

    You will be redirected to the login page again as the server cannot identify the session.

    1. Now again enable the cookies.
    2. Refresh the page. (Note: Don't click on login button).
    3. You will be automatically redirected to the Gmail inbox.

    Hence, we can say without cookies session will not work.

    Also, If you are trying to login into the gmail( taking as example you can take any website) with diabled cookies then it will message as "Your browser has cookies disabled. Make sure your cookies are enabled and try again."

    0 讨论(0)
  • 2020-11-28 04:33

    // tell the PHP we want to use cookies from the session

     ini_set('session.use_cookies', '0');
     ini_set('session.use_only_cookies', '0');
     ini_set('session.use_trans_sid','1');
     session_start();
    

    // then pass the session ID in the URL(inspect, navigate the network refresh the page you will see in the headers your session ID)

    0 讨论(0)
提交回复
热议问题