New users ask PHPThink about session and cookie

Source: Internet
Author: User
New users ask PHPThink about session and cookie issues developed by WPF (C #). they want to enable the browser and log on to the website. Refer to this http://blog.csdn.net/htsnoopy/article/details/7094224
I generate a cookie using the c # code, but when I open a browser and jump to a webpage, I find that the value in the session is empty.
After careful research, I found that the website developed by the PHPThink framework generates a fixed sessionid every time, and the sessionid generated by my code every time is random, so I cannot log on because I cannot match the number, what should we do?


Reply to discussion (solution)

Sessionid is generated by the server and can only be read by the client
Otherwise, how can we communicate?

Sessionid is generated by the server and can only be read by the client
Otherwise, how can we communicate? Well, I know that the server is also local. I don't know how to set it to make them consistent.

Sessionid is generated by the server and can only be read by the client
Otherwise, how can we communicate? The server is built with wamp

You are reading on the client!
Didn't the link you provided read cookies?

He also needs to send the cookie he has read back.

You are reading on the client!
Didn't the link you provided read cookies? When I read it, I sent it back. but when I opened the webpage, he re-generated a session. this sessionid is different from what I sent, and it is a fixed id no matter how I got it.

How did you write your php part?
If "no matter how you get her, it's a fixed id", isn't the fixed id you read and sent back?

How did you write your php part?
If "no matter how you get her, it's a fixed id", isn't the fixed id you read and sent back? No. why?

Paste the code!

C # obtain cookie logon

HttpHeader header = new HttpHeader (); header. accept = "text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8"; header. contentType = "application/x-www-form-urlencoded"; header. method = "POST"; header. userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2 ;. net clr 2.0.50727 ;. net clr 3.5.30729 ;. net clr 3.0.30729; Media Center PC 6.0; InfoPath.3 ;. NET4.0C ;. NET4.0E "; header. maxTry = 200; CookieCollection mycookie = HTMLHelper. getCookieCollection (" http://localhost/gyjw/index.php /Index/login_into "," admin = admin & password = 424 ", header); foreach (Cookie cookie in mycookie) // Set the cookie to the cookie {InternetSetCookie ("http: //" + cookie. domain. toString (), cookie. name. toString (), cookie. value. toString () + "; expires = Sun, 22-Feb-2099 00:00:00 GMT");} // System. diagnostics. process. start (" http://localhost/gyjw/index.php /Index/Login.html "); // System. Diagnostics. Process. Start (" http://localhost/gyjw/index.php /Index/login_into "); // System. Diagnostics. Process. Start (" http://localhost/gyjw/index.php "); System. Diagnostics. Process. Start (" http://localhost/gyjw/index.php /User/AddUser.html ");

Httphelper class

Using System; using System. collections. generic; using System. linq; using System. text; using System. net; using System. IO; using System. threading; namespace WpfUI {public class HTMLHelper {////// Obtain the CooKie ///////////////
 Public static CookieContainer GetCooKie (string loginUrl, string postdata, HttpHeader header) {HttpWebRequest request = null; HttpWebResponse response = null; try {CookieContainer cc = new CookieContainer (); request = (HttpWebRequest) webRequest. create (loginUrl); request. method = header. method; request. contentType = header. contentType; byte [] postdatabyte = Encoding. UTF8.GetBytes (postdata); request. contentLength = postdatabyte. length; request. allowAutoRedirect = false; request. cookieContainer = cc; request. keepAlive = true; // submit the request Stream; stream = request. getRequestStream (); stream. write (postdatabyte, 0, postdatabyte. length); stream. close (); // receive response = (HttpWebResponse) request. getResponse (); response. cookies = request. cookieContainer. getCookies (request. requestUri); CookieCollection cook = response. cookies; // Cookie string format string strcrook = request. cookieContainer. getCookieHeader (request. requestUri); return cc;} catch (Exception ex) {throw ex ;}}////// Obtain CookieCollection ///////////////
 Public static CookieCollection GetCookieCollection (string loginUrl, string postdata, HttpHeader header) {HttpWebRequest request = null; HttpWebResponse response = null; try {CookieContainer cc = new CookieContainer (); request = (HttpWebRequest) webRequest. create (loginUrl); request. method = header. method; request. contentType = header. contentType; byte [] postdatabyte = Encoding. UTF8.GetBytes (postdata); request. contentLength = postdatabyte. length; request. allowAutoRedirect = false; request. cookieContainer = cc; request. keepAlive = true; // submit the request Stream; stream = request. getRequestStream (); stream. write (postdatabyte, 0, postdatabyte. length); stream. close (); // receive response = (HttpWebResponse) request. getResponse (); response. cookies = request. cookieContainer. getCookies (request. requestUri); CookieCollection cook = response. cookies; // Cookie string format string strcrook = request. cookieContainer. getCookieHeader (request. requestUri); return cook;} catch (Exception ex) {throw ex ;}}////// Obtain html ///////////////
 Public static string GetHtml (string getUrl, CookieContainer cookieContainer, HttpHeader header) {Thread. sleep (1000); HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; try {httpWebRequest = (HttpWebRequest) HttpWebRequest. create (getUrl); httpWebRequest. cookieContainer = cookieContainer; httpWebRequest. contentType = header. contentType; httpWebRequest. servicePoint. connectio NLimit = header. maxTry; httpWebRequest. referer = getUrl; httpWebRequest. accept = header. accept; httpWebRequest. userAgent = header. userAgent; httpWebRequest. method = "GET"; httpWebResponse = (HttpWebResponse) httpWebRequest. getResponse (); Stream responseStream = httpWebResponse. getResponseStream (); StreamReader streamReader = new StreamReader (responseStream, Encoding. UTF8); string html = streamReade R. readToEnd (); streamReader. close (); responseStream. close (); httpWebRequest. abort (); httpWebResponse. close (); return html;} catch (Exception e) {if (httpWebRequest! = Null) httpWebRequest. Abort (); if (httpWebResponse! = Null) httpWebResponse. close (); return string. empty ;}} public class HttpHeader {public string contentType {get; set;} public string accept {get; set ;} public string userAgent {get; set ;} public string method {get; set;} public int maxTry {get; set ;}}}

Login Page

 Background login
   
 
   
 
 
 
 
 
 
 
   
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.