Php does not invalidate session when the cookie is disabled on the client

Source: Internet
Author: User

Cookies are good, but some client browsers will disable cookies, which will cause the program on which you are dependent to become invalid or wrong. If the user closes cookies, how Should PHP use session again? Methods are still available.

1. Set session. use_trans_sid = 1 for php. ini or enable the enable-trans-sid option to enable PHP to automatically pass the session id across pages.

2. Manually pass session IDs through URL values and hidden forms.

3. Save session_id in the form of files and databases, and manually call it during the cross-page process.

Example of Method 1:

S1.php

1

2session_start ();

3 $ _ SESSION ['var1'] = "Source code enthusiast ";

4 $ url = "next page ";

5 echo $ url;

6?>

S2.php

1

2session_start ();

3 echo "the value of the passed session variable var1 is:". $ _ SESSION ['var1'];

4?>

Run the above Code. When the client cookie is normal, you should be able to get the result "Source code lovers ".

If the client cookie is disabled at this time, it is estimated that no result will be obtained. You can set php. session in ini. use_trans_sid = 1 or enable the -- enable-trans-sid option during compilation. At this time, you can obtain the result "Source code enthusiast" again"

Example of Method 2:

S1.php

1

2session_start ();

3 $ _ SESSION ['var1'] = "Source code enthusiast ";

4 $ sn = session_id ();

5 $ url = "next page ";

6 echo $ url;

7?>

S2.php

1

2session_id ($ _ GET ['s ']);

3session_start ();

4 echo "the value of the passed session variable var1 is:". $ _ SESSION ['var1'];

5?>

The basic principle of hiding a form is the same as above.

Example 3: login.html

 

Mylogin1.php

01

02 $ name = $ _ POST ['name'];

03 $ pass = $ _ POST ['pass'];

04if (! $ Name |! $ Pass ){

05 echo "the user name or password is blank. Please log on again ";

06die ();

07}

08if (! ($ Name = "youngong" & $ pass = "123 "){

09 echo "the user name or password is incorrect. Please log on again ";

10die ();

11}

12 // registered user

13ob_start ();

14session_start ();

15 $ _ SESSION ['user'] = $ name;

16 $ psid = session_id ();

17 $ fp = fopen ("e: \ tmp \ phpsid.txt", "w + ";

18 fwrite ($ fp, $ psid );

19 fclose ($ fp );

20 // authentication successful, perform related operations

21 echo "logged on

";

22 echo "next page ";

23?>

Mylogin2.php

01

02 $ fp = fopen ("e: \ tmp \ phpsid.txt", "r ";

03 $ sid = fread ($ fp, 1024 );

04 fclose ($ fp );

05session_id ($ sid );

06session_start ();

07if (isset ($ _ SESSION ['user']) & $ _ SESSION ['user'] = "laogong "{

08 echo "logged on! ";

09}

10 else {

11 // Successful Logon

12 echo "not logged on, not authorized to access ";

13 echo "log on and browse ";

14die ();

15}

16?>

Disable the cookie and try again. Username: youngong password: 123 this is the session id saved through the file, the file is: e: \ tmp \ phpsid.txt. The database method is similar to the file operation method. The above method has one thing in common: Get the session id on the previous page, and find a way to pass the session id to the next page, and add the code session_id (the session id passed) to the next page ); we hope to provide you with some reference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.