PHP: how to use the session cookie after the client is disabled

On the server side, to use the session, the most essential issue is to be able to get the session ID on the server side.

Typically, in the open session of the pages that use the session_start () of the page, when the first visit, it will generate a new session, and there is a corresponding sessionID, the server will respond to this sessionID via http set-cookie header fields returned to the client (browser), the browser will save it in a cookie. When visiting the page again, sessionID http cookie will be placed in the header field of the request, sent to the server, to obtain the corresponding session content through this sessionID.

When the client is disabled cookie, it can not be more than a way to use the session.

Intrinsically cookie is saved sessionID, and sessionID sent to the server. If there are other ways sessionID will be sent to the server, the problem will be solved.

Solution is to write the sessionID to URL or written form of hidden components, so that you can get a sessionID on the server side.

  • Modify php.ini

Modify session.use_trans_sid = 1, indicate when the client browser cookie prohibited, and will be based on the links on the page url passed SESSIONID. But many people just set this option does not achieve the desired effect, I have encountered this problem, but some studies have found there are two options in the php.ini file

session.use_cookies=1
session.use_only_cookies=1

Carefully pondering the above English will find its meaning
session.use_cookies indicates whether session cookies session began based on the
session.use_only_cookies only indicates whether the session open session based cookies the way
so if you want to open when the browser cookie with cookie-based way, at the time of cookie use url unopened way to set as follows (the most common way is recommended)
in the php.ini file

session.use_trans_sid=1
session.use_only_cookies=0
session.use_cookies=1

This can, in essence, is to rewrite the URL, the sessionID way to add query string in the URL of.

a.php

<?php
session_start();
$_SESSION['var1']="源码爱好者";
$url="<a href=".""b.php">下一页</a>";
echo $url;
?>

b.php

<?php
session_start();
echo "传递的session变量var1的值为:".$_SESSION['var1'];
?>

You can personally test.
This example is below the top of the effect is the same, but the top example is the server automatically rewrite the URL, and the example below is manually overridden.

c.php

<?php
session_start();
$_SESSION['var1']="源码爱好者";
$sn = session_id();
$url="<a href=".""d.php?s=".$sn."">下一页</a>";
echo $url;
?>

d.php

<?php
session_id($_GET['s']);
session_start();
echo "传递的session变量var1的值为:".$_SESSION['var1'];
?>

Finally, the practice of using a hidden form with the above example of the same, just write the sessionID is a hidden form components and then take to the sessionID on the server.
----------------
Disclaimer: This article is CSDN blogger "Do not forget why the departure" of the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_27988539/article/details/77621040

UFI
Published 16 original articles · won praise 2 · Views 1192

Guess you like

Origin blog.csdn.net/qq_39075021/article/details/104046510