WeChat enterprise code development-how to use WeChat attendance Cookies-javascript skills

Source: Internet
Author: User
Using attendance, every time I use the enterprise ID for development: the relationship between user information and web page Sessions. the method in this section calls the interface, which is a little slow. the official website also recommends using Cookies, but how to use Cookies, I have never figured it out. The following small series help you solve the problem. if you need it, you can refer to the previous article to introduce the Baidu Map of attendance for enterprise ID development. Next, we will learn how to use attendance Cookies for enterprise ID development through this article, the specific content is as follows.

Using attendance, every time I use the enterprise ID for development: the relationship between user information and web page Sessions. the method in this section calls the interface, which is a little slow. the official website also recommends using Cookies, but how to use Cookies, I have never figured it out.

I always thought that there are two methods to obtain client data on the server. one is to put the query string on the URL, the other is to put the data in form, and submit the data in post, I have used it before, but it is mainly used on the client. I have never been able to directly submit the data in Cookies to the server. even if there is one, I can read the data in Cookies and put it into a hidden field in form, then post to the server.

Obviously, attendance is actually a URL, and there is no post data process in the process of entering the URL. You can submit the URL only after entering the URL or ajax. In short, it seems that the data in Cookies cannot be directly submitted to the server. It seems that there is a deadlock. So I studied Cookies again and found that Cookies are submitted to the server. However, they are not submitted in the same position as post. of course, I did not find the relevant documents and found them through tests. As long as you set Cookies, Cookies will be submitted every time you enter the URL. Naturally, you can read the Cookies on the server. In this case, we can understand the real implementation principle of the password. Instead of reading the Cookies, placing them in a hidden field, and submitting them to the server through ajax, you can log on without logon.

We can see that the cookie data is sent to the server, and sessionID is also transmitted to the server through Cookies.

How to set Cookies for front-end js reading:

Function setCookie (name, value) {// two parameters: one is the cookie name and the other is the value var Days = 30; // This cookie will be saved for 30 days var exp = new Date (); // new Date ("December 31,999 8"); exp. setTime (exp. getTime () + Days * 24x60*60*1000); document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();} function getCookie (name) {// use the cookie function var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $) "); If (arr! = Null) return unescape (arr [2]); return null ;}

CSharp server Cookies:

Set Cookies

HttpCookie cookie = new HttpCookie("UserCode", username);    cookie.Expires = DateTime.Now.AddDays(10);// (365 * 24 * 3600);      this.Response.AppendCookie(cookie);    HttpCookie cookieDeviceId = new HttpCookie("DeviceId", rt.DeviceId);    cookieDeviceId.Expires = DateTime.Now.AddDays(10);// (365 * 24 * 3600);      this.Response.AppendCookie(cookieDeviceId);

Read Cookies:

HttpCookie ttHttpCookie = this. request. cookies. get ("UserCode"); HttpCookie ttHttpCookieDeviceId = this. request. cookies. get ("DeviceId"); string code = Request. queryString ["code"]; if (ttHttpCookie = null | ttHttpCookieDeviceId = null) {WeiApi (code);} else {string username = ttHttpCookie. value; string DeviceId = ttHttpCookieDeviceId. value; if (string. isNullOrEmpty (username) | string. isNullOrEmpty (DeviceId) {WeiApi (code);} else {new AppException ("Read Cookies UserCode =" + username + ", DeviceId =" + DeviceId); initSession (username, deviceId );}}

The above content introduces the use of attendance Cookies for enterprise code development. For more information, see PHP Chinese website (www.php1.cn )!

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.