Home >php教程 >php手册 >Solution to the loss of window session opened with window.open in asp.net and php

Solution to the loss of window session opened with window.open in asp.net and php

WBOY
WBOYOriginal
2016-07-09 09:10:181310browse

Hope this is helpful to those who happen to encounter this problem. The following is the source code of asp.net, which can be downloaded.

window.open opens the window Session is lost

Although window.open is very unpleasant, it sometimes has certain uses. Nowadays, many office automation systems (OA),

In order to give the user more space for operation, window.open is used to open a window with only the title bar after the user logs in.

But in the window that opens, the session generated in the login window cannot be found.

1: Solution in asp.net:

1: If the login window is default.aspx, we can write this in the background:

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

In this way, we can pass the SessionID to the newly opened window index.aspx, and we can use this SessionID in index.aspx

Reconstruct a Session.

2: In index.aspx we can reconstruct the session like this:

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 }

Among them, we use the SessionIDManager class under the SessionState namespace, and we override its CreateSessionID

This virtual method is used to retrieve or retrieve the Session. The virtual method CreateSessionID needs to be rewritten, and it returns a new Session

’s SessionID, and this SessionID is passed from the login interface default.aspx, so we can think of the newly constructed

Session and the Session of the login window are one Session.

2: Solution in php:

1: The same method is used to solve this session loss in php, and it is simpler. It only requires two functions.

session_start();
$sid=session_id();

The session_id function is to obtain the SessionID of the login interface.

2: Then we can construct a Session based on this SessionID just like in .net:

$sid=$_GET['sid '];

session_id($sid);

session_start();

The function Session_id is to reconstruct the Session.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn