Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the question of whether Session must rely on Cookie

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand whether the Session must rely on cookies", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take you to learn "how to understand whether the Session must rely on cookies"!

The session in php can use client-side cookies by default (in order to distinguish them from cookies in the ordinary sense, I call them session cookies, cookies in the ordinary sense) to save the session id, but whether the session in php can only use session cookies?

Of course not, otherwise why also get a session out, it is better to calculate directly with cookies. One of the advantages of session is that when the client cookies are disabled automatically attached to the session id url, so that you can remember the session variable through the session id.

Here I write two files to confirm it, first in the browser settings disable cookies.

The copy code is as follows:

The copy code is as follows:

Now type "http://localhost/test1.php" in your browser, move your mouse over the link and look at the address on the status bar, not simply "http://localhost/test2.php", but this form:"http://localhost/test2.php?" phpsessid=6e3610749f7ded3784bc4a4dd10f879b".

You can also view html source files in this form:

goto test2.php

So this is all php credit, and browser independent, that is to say, no matter what browser session you use are valid, rather than some people think only useful for ie.

However, our hyperlink statement is output by the echo statement. What if the hyperlink is not included in the tag of php? Let's write an example to verify this by modifying test1.php slightly:

The copy code is as follows:

(html form)goto test2.php

Type "http://localhost/test1.php" in your browser and hover over each link to see if it's different. As you can see, both links are identical, with session id attached automatically. So don't worry about links that aren't included in php tags failing,php isn't that stupid.

However, when using it, you must first use the session_start() function to tell php to start using session, even if you only have html code in this file, such as:

gogogo

…………

Remember someone said that this advantage can only be played out under linux/unix, and I use win2000p+apache1.3.17+ php4.0.4pl1, php for apache module mode, but still can. On the contrary, I switched to linux to test it out. It's actually an option at compile time--enable-trans-sid that controls whether or not this feature is useful. This feature is not turned on when compiling according to php default, just add it when recompiling. My configuration is apache1.3.17+ php4.0.4pl1, php is apache module mode, after linux recompiled with netscape navigator4.7 test can pass (this more proof and browser independent).

Session alone is not used across windows, even if you enable cookies, when you have a legitimate session id in a window (recorded in session cookies, not url), and then open a window to enter the same page, you will have a new session id, and the previous window does not affect each other.

If you want to use the same session id across windows, you can only specify the session id after the url, that is to say, if you copy the url of the window with session id, paste it in the newly opened window, or use it as usual. Know the principle of session id to achieve cross-window session or not difficult, you can combine cookies with session, first get the current legal session id, and then record it in cookies, read cookies in other windows can get the current session id.

At this point, I believe that everyone has a deeper understanding of "how to understand whether Session must rely on cookies", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report