Php solution to disable session invalidation when cookie is disabled on the client _ PHP Tutorial

Source: Internet
Author: User
Php does not invalidate the session when the cookie is disabled on the client. 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, P cookie is 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.