PHP workaround for the session not to expire when the client disables Cookies _php tutorial

Source: Internet
Author: User
Cookies are good, but some client browsers will disable cookies, which will result in the failure or error of the program you rely on, so how can PHP use the session again if the user closes the cookie? The method is still there.

1, set php.ini Session.use_trans_sid = 1 or open the ENABLE-TRANS-SID option, let PHP automatically pass the session ID across pages.

2, manually pass the URL value, hide the form passed session ID.

3, file, database and other forms to save session_id, in the process of cross-page manual call.

Route 1 illustrates:

s1.php

1

2session_start ();

3$_session[' var1 ']= "source enthusiasts";

4$url= "Next page";

5echo $url;

6?>

s2.php

1

2session_start ();

3echo "The value of the SESSION variable var1 passed is:". $_session[' var1 ');

4?>

Run the above code, in case the client cookie is normal, should be able to get the result "source fan".

If the client's cookie is closed at this time, it is estimated that the result is not available, you can set the php.ini in Session.use_trans_sid = 1 or at compile time to open the--ENABLE-TRANS-SID option ", at this time again can get results" source fan "

Route 2 illustrates:

s1.php

1

2session_start ();

3$_session[' var1 ']= "source enthusiasts";

4$SN = session_id ();

5$url= "Next page";

6echo $url;

7?>

s2.php

1

2session_id ($_get[' s ');

3session_start ();

4echo "The value of the SESSION variable var1 passed is:". $_session[' var1 ');

5?>

The basics of how to hide a form are the same.

Route 3 Example: login.html

 

mylogin1.php

01

02$name=$_post[' name '];

03$pass=$_post[' Pass ';

04if (! $name | |! $pass) {

05echo "User name or password is blank, please login again";

06die ();

07}

08if (! ( $name = = "Youngong" && $pass = = "123") {

09echo "User name or password is incorrect, please login again";

10die ();

11}

12//Registered Users

13ob_start ();

14session_start ();

15$_session[' user ']= $name;

16$PSID=SESSION_ID ();

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

18fwrite ($fp, $psid);

19fclose ($FP);

20//authentication successful, related operations

21echo "Logged in

";

22echo "Next page";

23?>

mylogin2.php

01

02$fp=fopen ("E:\tmp\phpsid.txt", "R";

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

04fclose ($FP);

05SESSION_ID ($SID);

06session_start ();

07if (isset ($_session[' user ') && $_session[' user ']= "Laogong" {

08echo "logged in!";

09}

10else {

11//successful login for related operations

12echo "Not logged in, not authorized to access";

13echo "Please log in after browsing";

14die ();

15}

16?>

Please turn off cookie re-test, username: youngong Password: 123 This is the file to save the session ID, the file is: E:\tmp\phpsid.txt. As for the use of the database method, it does not give an example, similar to the way the file is manipulated. The above method has a common point, is to get the session ID on the previous page, try to pass to the next page, the next page of the Session_Start (), before adding code session_id (pass the session ID); I hope to provide you with some reference.

http://www.bkjia.com/PHPjc/765434.html www.bkjia.com true http://www.bkjia.com/PHPjc/765434.html techarticle cookies are good, but some client browsers will disable cookies, which will result in the failure or error of the program on which you rely on cookies, so if the user closes the cookies, P ...

  • Related Article

    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.