Home Backend Development PHP Tutorial Cookies are disabled, can the Session still be used?

Cookies are disabled, can the Session still be used?

Jul 28, 2016 am 08:28 AM
cookie nbsp php quot session

Cookie and Session are generally considered to be two independent things. Session uses a solution that maintains state on the server side, while Cookie uses a solution that maintains state on the client side. But why can't I get the Session if I disable cookies? Because the Session uses the Session ID to determine the server Session corresponding to the current conversation, and the Session ID is passed through Cookie, disabling Cookie is equivalent to losing the Session ID, and thus the Session is lost.
Is it true that if Cookie is disabled, Session will definitely not be usable?
1. ASP
In ASP, Session must rely on Cookie to be available. Session is stored on the server side, while Cookie is stored on the client side. Relatively speaking, Session is more secure and reliable than Cookie.
2. PHP
In PHP, through relevant configuration, Session can exist without relying on cookies. This is because:
Session is stored on the server side (the Session is stored as a file by default). The user's file is obtained according to the Session ID provided by the client, and the value of the variable is obtained. The Session ID can use the client's Cookie or Http1.1 protocol. Query_String (the part after the "?" of the accessed URL) is sent to the server, and then the server reads the Session directory... In other words, Session ID is the ID card for obtaining the Session variable stored on the service. When the code session_start(); is run, a Session file is generated on the server, and a Session that uniquely corresponds to it is also generated. ID, defines the Session variable to be stored in the Session file just generated in a certain form. Through the Session ID, the defined variables can be retrieved. After crossing the page, in order to use the Session, you must execute session_start() again; another Session file will be generated, and the corresponding Session ID will be generated accordingly. Using this session ID, you cannot retrieve the first Session file mentioned above. variable in because this Session ID is not the "key" to open it.If you add the code session_id($session id); before session_start();, a new Session file will not be generated, and the Session file corresponding to this id will be read directly.
     Session in PHP uses the client’s cookie to save the session ID by default, so when there is a problem with the client’s cookie, it will affect the session. It must be noted that Session does not necessarily have to rely on Cookie, which is also the advantage of Session compared to Cookie. When the client's cookies are disabled or there is a problem, PHP will automatically attach the Session ID to the URL, so that the Session variable can be used across pages through the Session ID. But this attachment also has certain conditions, namely "session.use_trans_sid in php.ini" = 1", or the "--enable-trans-sid" option is turned on when compiling.
Friends who have used the forum know that when entering the forum, you are often prompted to check whether cookies are turned on. This is because most Forums are all based on cookies, which are used to save user information such as user names and passwords for ease of use. Moreover, many friends think that cookies are not safe (this is not the case) and often disable them in PHP programs. Use Session to replace Cookie, which does not depend on whether the client turns on Cookie
So, we can put aside Cookie and use Session, that is, assuming that the user turns off Cookie, use Session. There are several ways to achieve this:
1. Set "session.use_trans_sid = 1" in the php.ini configuration file, or turn on the "--enable-trans-sid" option when compiling to let PHP automatically pass the Session ID across pages
2. Manually pass the value through the URL. , hide the form to pass the Session ID.
3. Save the Session ID in a file, database, etc., and call it manually during the cross-page process.
Example 1:
---------------- -------------------------------------------------- --------------------------------------------------
                                 iguowei2004.blog. 163.com/blog/.""s2.php">Next page";
              echo $url;
        ?>
                                                                                 -------------------------------------------------- --------------------------------------------------
------------------------------------------------ -------------------------------------------------- ----------------
                                                                                                                                                                                                                                        // s2.php var1'];
?>
---------------------------------------- -------------------------------------------------- ------------------------
Run the above code, and if the client cookie is normal, you should be able to get the result "People's Republic of China".
Now if you manually close the cookie on the client and run it again, you may not get the result. If you cannot get the result, set "session.use_trans_sid = 1" in the php.ini file, or turn on the "--enable-trans-sid option" when compiling, and you will get the result "People's Republic of China".
      Path 2 Example:
    ------------------------------------------------ -------------------------------------------------- -----------------------
                                                                                                                                                                         Republic";
            $sn = session_id();
          $url="Next page";
                   echo $url;
                                                                                                      -------------------------------------------------- ---------------------------------------
-------- -------------------------------------------------- -------------------------------------------------- ---- l & lt;? Php
session_id ($ _ get ['s']);
session_start ();
the value of the session variable var1 transmitted by echo "is:". $ _ Session ['var1'];
?>
------------------------------------------------ -------------------------------------------------- ------------------
                                                                                                                                                                                                              -------------------------------------------------- --------------------------------------------------
          login.html
                                           HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                                                        ="Content- Type" c/html; charset=gb2312">
                                                                                                                                                                                                                                               
                                                              use using using using  ="submit" value="Login">
                                                                                                                                                                              -------------------------------------------------- ----------------------------------------
---- -------------------------------------------------- -------------------------------------------------- -----------
mylogin1.php
|| !$pass) {
                                                                                                                            | "; E die ();
}
if (! ($ Name ==" laigw "&& $ pass ==" 1234 ") {
echo" user name or password is incorrect, please & lt; a href = "http ://laiguowei2004.blog.163.com/blog/login.html">Re-login";
            die();
      }
              //Registered user
                                                            using use using ’ ’ s ’       ‐         ‐                              phpsid.txt"," w+"); r fwrite ($ fp, $ psid);
fclose ($ fp);
// authentication is successful, and related operations

& & & lt;";
echo "& lt; a href = "http://laiguowei2004.blog.163.com/blog/mylogin2.php">Next page";
?>
---------------- -------------------------------------------------- -------------------------------------------------- -
------------------------------------------------ -------------------------------------------------- ------------------
mylogin2.php
$fp=fopen("D:tmpphpsid.txt","r");
$sid=fread ($fp,1024);
                        fclose($fp);                                                                                                                                                                                                                                                           ​"{H Echo" has been logged in! ";
} else {
// Successfully log in for related operations






" "



& & lt; a href =" http://laiguowei2004.blog .163.com/blog/login.html">LoginBrowse";
         die();
                     
                                               -                                      -------------------------------------------------- --------------------------------------------------
Please also turn off Cookie testing, username: laigw; password: 1234; this is to save the Session ID through a file, the file is: D:/tmp/phpsid.txt, please decide the file name or path according to your own system.
To summarize, the above methods have one thing in common, which is to obtain the Session ID on the previous page, and then find a way to pass it to the next page. Add the code Session ID (the passed Session ID) before the session_start(); code on the next page. ).

The above has introduced that if Cookie is disabled, can Session still be used? , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles