跨域共享session (实现http跳转https 共享session)

在网上找了很多,终于搞明白了,也行不是最好的办法,但确实非常使用的方法。

 

其中最重要的就是 设置session id 至 本地 cookies 当中, 采用如下方法:

 

$currentSessionID = session_id();
 

 

session_id($currentSessionID );

 

 

简单实例:

 

Script 1(HTTP) :

 

<?php

// This script will create a session and display a link to your secure server address
// to transfer your session ID. In this example, the secure page to receive the session
// ID is located at http://www.yoursite.com/safePages/securePage.php

// Start a session using the current session ID stored in a cookie, or create
// a new session if none is set.

session_start();

$currentSessionID = session_id();

// Set a variable that will be retrieved with the HTTPS script.
$_SESSION['testvariable'] = 'It worked';

// $secureServerDomain is the domain of your secure server
$secureServerDomain = 'www.yoursite.com';

// $securePagePath is the path to the page that will receive and set the session ID.
$securePagePath = '/safePages/securePage.php'

echo '<a href="https://' . $secureServerDomain . $securePagePath . '?session="' . $currentSessionID . '">Click here to transfer your session to the secure server</a>';

?>
 

Script 2(HTTPS) :

 

<?php

// Retrieve the session ID as passed via the GET method.
$currentSessionID = $_GET['session'];

// Set a cookie for the session ID.
session_id($currentSessionID);

// Start a session.
session_start();

// Test retrieval of variable set when using HTTP.
if (!empty($_SESSION['testvariable'])) {
      echo $_SESSION['testvariable'];
} else {
      echo 'It did not work.';
}

?>
 

 

但是要注意的是:

http://www.mysite.com/page.php   跳转到 https://www.mysite.com/page.php

或者

http://mysite.com 跳转到 https://mysite.com/page.php.

 

关于安全性:

 

应该讲和传统的登录验证安全性一样。都是不太安全的。因为sid的传输是没有加密的,别人也可以通过监听,嗅探来获取这个session id,也就获取了你的session数据。因此后面可以考虑将session id信息加密之后进行传输。

 

 

另一种就是采用数据库的方式:

 

见附件。

 

 

require_once "session.class.php";
$oSession = new Session();
print_r($_SESSION); // First
$_SESSION['hi'] = "lisha"; // Comment this Once sessoin is set
$_SESSION['test'] = "gideon"; // Comment this Once sessoin is set

echo '===========';
//Now use php sessions as usual
print_r($_SESSION); // First
 

 

说明一下的是,需要用到 session_set_save_handler 函数,它要配合 ini_set('session.save_handler', 'user'); 一起使用!

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值