Heim > php教程 > php手册 > Hauptteil

asp.net和php中用window.open打开的窗口session丢失的解决办法

WBOY
Freigeben: 2016-07-09 09:10:18
Original
1245 Leute haben es durchsucht

 

希望对那些正好遇到此问题的人有点帮助。下面是asp.net的源代码,可以下载.

window.open打开窗口Session丢失

 

window.open虽然很不讨人喜欢,但它有时候又有一定的用处,现在很多办公自动化系统(OA),

为了给用户更大的操作空间,都在用户登陆以后用window.open打开一个只有标题栏的窗口。

但是在open出来的窗口中,却找不到在登陆窗口产生的session了。

 

一:在asp.net中的解决办法:

1:假若登录窗口为default.aspx,我们在后台可以这样写:

1 string user = Request.Form["user"];
2 Session["_uid"= user;
3 string sid = Session.SessionID;
4 Page.RegisterStartupScript
5 ("open","<script></script>
6 window.open
7 ('index.aspx?sid="+sid+"','_blank','status=yes,scrollbars=yes,resizable=yes');
8 script>");
9 

 这样我们就可以把SessionID传递到新打开的窗口index.aspx中,我们在index.aspx中就可以根据这个SessionID

重新构造一个Session。

 2:在index.aspx中我们可以这样来重新构造session:

 1 using System.Web.SessionState;
 2 
 3 public partial class index : System.Web.UI.Page
 4 {
 5     protected void Page_Load(object sender, EventArgs e)
 6     {
 7         string sid = Request.QueryString["sid"].ToString();
 8         SessionIDTest ss = new SessionIDTest(sid);
 9         ss.CreateSessionID(Context);
10     }
11 }
12 
13 public class SessionIDTest : SessionIDManager
14 {
15     private string sid;
16     public SessionIDTest(string sid)
17     {
18         this.sid = sid;
19     }
20     public override string CreateSessionID(HttpContext context)
21     {
22         return sid;
23     }
24 
25 }

 其中我们用到了SessionState命名空间下的SessionIDManager类,我们通过重写它的CreateSessionID

这个虚方法来重新或得Session。CreateSessionID这个虚方法需要我们重写,它返回的就是一个新的Session

的SessionID,而这个SessionID就是从登陆界面default.aspx中传递过来的,所以我们可以认为新构造的

Session和登陆窗口的Session是一个Session。

 

 二:在php中的解决办法:

1:在php中解决这个sesssion丢失,用的也是同样的办法,并且还更简单,只需两个函数就搞定了,

session_start();
$sid=session_id();

session_id这个函数就是取得登陆界面的SessionID。

 

 2:然后我们就可以想在.net中一样,根据这个SessionID构造一个Session:

$sid=$_GET['sid'];

session_id($sid); 

session_start();

 Session_id这个函数就是重新构造Session。


 

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!