A workaround for PHP when the client disables cookies

Source: Internet
Author: User
Tags ini session id

Cookies are good, but some client browsers will disable cookies, which will cause you to rely on cookies, the program will fail or error, so if the user turned off cookies, PHP should be how to use the session again? Method is still available.

1, set php.ini Session.use_trans_sid = 1 or open enable-trans-sid option, let PHP automatically spread the session ID across the page.

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

3, in the form of files, databases and other forms of saving session_id, in the spread of the process of manual call.

Route 1 provides an example of:

s1.php

1

2session_start ();

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

4$url= "Next page";

5echo $url;

6?>

s2.php

1

2session_start ();

3echo "The passed session variable VAR1 value is:". $_session[' var1 ';

4?>

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

If the client's cookie is turned off at this time, it is estimated that the result is not available, at which point you can set the SESSION.USE_TRANS_SID = 1 in php.ini or turn on--enable-trans-sid option at compile time, and then you can get the result again "source enthusiasts"

Route 2 provides an example of:

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 passed session variable VAR1 value is:". $_session[' var1 ';

5?>

The method of hiding forms is the same as the basics.

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 "username 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:tmpphpsid.txt", "w+");

18fwrite ($fp, $psid);

19fclose ($FP);

20//authentication successful, related actions

21echo "Logged in

";

22echo "Next page";

23?>

mylogin2.php

01

02$fp=fopen ("E:tmpphpsid.txt", "R";

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

04fclose ($FP);

05SESSION_ID ($SID);

06session_start ();

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

08echo "already logged in!";

09}

10else {

11//successful login for related actions

12echo "Not logged in, Access denied";

13echo "Please login after browsing";

14die ();

15}

16?>

Please turn off cookies and test again, Username: youngong Password: 123 This is the file to save the session ID, the file is: E:tmpphpsid.txt. As for the use of the database method, do not cite examples, and file operation method is similar. The above methods have one thing in common, is to get the session ID on the previous page, find a way to pass to the next page, the next page of the Session_Start (), before the code session_id (passed the session ID), I 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.