doupin2013 2010-07-21 06:41
浏览 53
已采纳

C#:如何将PHP会话变量发送到WebBrowser控件?

Okay, I am using the following code to navigate to a page in my WebBrowser control based on information entered in a login form:

private string currentUser = usernameTextbox.Text;
private string currentPasswd = passwordTextbox.Password;

public static byte[] StrToByteArray(string str)
{
    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    return encoding.GetBytes(str);
}

Uri myHomePage = new Uri("userHome.php");
currentBrowser.Navigate(myHomePage, "", appFunctions.StrToByteArray("username=" + currentUser + "&password=" + currentPasswd), "Content-Type: application/x-www-form-urlencoded
");

This code works fine and sends the POST data to my desired variables within my PHP page, and it successfully logs the user in to their personal menu. Doing this also sets a $_SESSION variable on the PHP page to hold the username accross all pages of the site. This also works fine - as long as I am navigating from links within the pages themselves. However, if I try to use the WebBrowser.Navigate() to move to another page, it seems to lose the $_SESSION variable.

With all that being said, what I would like to know is if there is any way to send my C# 'currentUser' variable directly to the PHP $_SESSION variable via the WebBrowser.Navigate() method.

I think that if I can do this my problem will be solved. If not I'll probably have to go through each page of my site and add some variable-checking code to see if the C# form has sent the POST data directly to that page.

Any help will be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • doumian3780 2010-07-21 07:05
    关注

    PHP has two ways to work with sessions: set a cookie, or mangle every URL on the page to include the session identifier.

    It sounds like PHP is doing the latter, when you want it to do the former. Make sure your code can work with cookies.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部