使用桌面应用程序时的PHP会话

使用桌面应用程序时的PHP会话

问题描述:

在这个问题中,我问如何从vb.net应用程序发布到php文件:

In this question I asked how to POST to a php file form a vb.net app: POST to webpage in vb.net (win forms, desktop, not ASP.net) So now I've logged in the user user by posting their username and password to the php file, the php file then does security/checks they exist/etc and if both username and password are correct is stores the user ID in a session variable.

现在,如果vb.net应用尝试从需要用户登录的页面上下载数据,它将通过以下操作进行检查:

Now if the vb.net app tries to download data off a page which needs the user to logged in, it checks this by doing:

if (!isset($_SESSION['uid'])) {
    header("Location: index.php");
}

但是,正确登录应用程序后,未设置会话变量.会话如何与这样的vb.net应用程序一起工作?

However after having logged correctly in the app the session variable is not set. How does session work with a vb.net app like this?

用户成功登录后,我应该下载用户ID并将其保留在vb.net应用程序中,然后将其发布到需要身份验证的每个页面上吗?

When the user logs in successfully should I download the user id and keep it in the vb.net app and then post it to each page that requires authentication?

要使您的PHP网站将VB.NET客户端识别为登录用户,您需要发送一个cookie.当您在PHP中使用session_start()时,PHP将在访问者cookie中设置一个随机ID来链接会话.您需要知道的是此ID.更具体地说,在您首次向网站提出请求时,您应该阅读该信息.

To have your PHP website recognize the VB.NET client as a logged on user you need to send a cookie. When you use session_start() in PHP, PHP will set a random ID in the visitors cookie to link the session with. What you need to know is what this ID is. More specifically, on your first request to the website, you should read this out.

在另一个问题中,我看到您正在使用WebClient实例.如果发送了请求,则还有一个名为ResponseHeaders的属性.这是一个集合,其中包含来自Web服务器(在本例中为运行您的网站的Web服务器)的响应标头.这也可能也包含cookie代码.

In your other question I saw you are using a WebClient instance. If you sent a request, there is also a property called ResponseHeaders. This is a collection that contains the response headers from the webserver (in this case the webserver that's running your site). This will likely contain a cookie code too.

例如:

Dim myWebClient As New WebClient

Dim responseArray = myWebClient.UploadData("http://...", "POST", Encoding.ASCII.GetBytes(postData))
Dim MyCookie As String = cl.ResponseHeaders.Item(HttpResponseHeader.SetCookie)

myWebClient.Headers.Add(HttpRequestHeader.Cookie, MyCookie)

在此示例中,您必须处理responseArray,但这是存储cookie并将其发送回的基本原理.您使用此WebClient的相同实例发送的下一个请求将包含您的站点对上一个请求作出响应的cookie.基本上,这意味着PHP网站创建的SessionID将成为成员并发回.

You have to process the responseArray in this example, but this is the basic principle for storing a cookie and sending it back. The next request you send out with the same instance of this WebClient will contain the cookie your site responded with the last request. Basically it means, the SessionID that the PHP site creates will be membered and send back.

我个人会为此编写一个包装类.只需创建一个将登录请求发送到您的特定站点的功能.然后存储cookie,稍后将发送的每个请求都将这个cookie添加到其中.您可以轻松地编写通用"方法,例如字符串GetPage(字符串URL);字符串PostPage(字符串URL,字符串PostData)等

Personally I would write a little wrapper class around this. Just make a function that sends out a login request to your specific site. Then store the cookie, and every request you will send later you add this cookie to it. You could easily write a 'generic' method like string GetPage(string URL); string PostPage(string URL, string PostData) etc.