Home >Backend Development >PHP Tutorial >PHP simply implements HTTP and HTTPS cross-domain sharing session solutions, httpssession_PHP tutorial
A solution for session sharing under HTTP and HTTPS protocols to resolve cookie failure: (Maybe not the best, but Practical)
The principle is to set the session id to the local cookie,
Copy code The code is as follows:
$currentSessionID = session_id();
session_id($currentSessionID );
Simple example code:
(HTTP)
Copy code The code is as follows:
session_start();
$currentSessionID = session_id();
$_SESSION['testvariable'] = 'Session worked';
$secureServerDomain = 'www.sjolzy.cn';
$securePagePath = '/safePages/securePage.php'
echo 'Click here to jump to the HTTPS protocol';
(HTTPS)
Copy code The code is as follows:
$currentSessionID = $_GET['session'];
session_id($currentSessionID);
session_start();
if (!emptyempty($_SESSION['testvariable'])) {
echo $_SESSION['testvariable'];
} else {
echo 'Session did not work.';
}
There is a bit of a security issue, because the transmission of the session ID is not encrypted in this way, and others can sniff and detect it, obtain the session ID, and then obtain your session data. So if necessary, you can consider encrypting this ID.
The cookie setting of a magento site has been invalid, and I struggled for many days. Finally, I realized that the cross-domain between HTTP and HTTPS made the cookie invalid. .