Home >Web Front-end >JS Tutorial >Javascript restricts web pages to only be accessible in WeChat's built-in browser_javascript tips

Javascript restricts web pages to only be accessible in WeChat's built-in browser_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:31:431820browse

Then the question arises. This web page first involves mobile web front-end development. I gave priority to using HTML5 bootstrap combination to achieve the beautiful effect of the page. The other front-end tasks are solved by JavaScript (here I completely use native JavaScript) The code does not use any framework, because considering that the speed of loading web pages on mobile phones is inherently slow, and many unused functions in the framework will also be loaded with the web page, consuming user traffic).

After all the functions were in place and the customer was very satisfied with the trial, I submitted the code to the official server. However, the other party suddenly mentioned something: the page you created is indeed very beautiful and has good compatibility, but this page can also be accessed using a browser on your computer, so others can easily view the source code of the page. , and then copy the entire page. Is there any way to prevent this? Then the question comes again. This is my first time doing secondary development for WeChat. I have never encountered such a demand. How can I get it done?

I couldn’t think of it for a while, so I put this problem aside for now and did some backend business. When doing a data collection function in the background, the parameter $_SERVER['HTTP_USER_AGENT'] was used in the PHP code. It suddenly occurred to me that when the browser accesses the web page, it will send a UserAgent to the server, which contains some browser and user operations. Basic information about the system. Since WeChat has a built-in browser, will the UserAgent have a unique logo related to WeChat when browsing the web with WeChat (after all, Tencent is such a big company, and WeChat is their core one of the products)? Simply use the code to print its UserAgent to find out. The javascript code is as follows:

Copy code The code is as follows:


I got the result as shown in the picture on my mobile phone:

I really saw something different this time. I believe smart friends have also discovered it. Yes, it is this thing: MicroMessenger/6.0.0.50_r844973.501. The string after the slash is me. The version number of WeChat currently in use should be preceded by a unique logo of WeChat. In fact, I thought it was MicroMessage at first, thinking that the Chinese translation means "micro-message", but after a closer look, I found that it was not the case. Later, I checked the dictionary and found out that the word Messenger means "messenger, messenger". It doesn’t feel strange anymore. This logo should not be found in other browsers, so here is the solution, please see the code:

Copy code The code is as follows:


This code has been tested on Android, iPhone, iPad, and PC. As long as the webpage is not opened within WeChat, the above warning box will pop up first. At this time, the following page is blank, and nothing else It is not loaded. After clicking the OK button in the warning box, the last three lines of code will forcefully close the current page. OK, now that the user’s original intention has been realized, we can simply call it a day.

Wait, can we really call it a day? Are you sure there won't be any other problems? Of course not. In fact, this method will not let you sit back and relax, because forged UserAgents can still bypass this restriction. Generally speaking, you can guard against gentlemen but not villains. Everyone understands. Of course, if any friend has a better way to solve this problem, please let me know in the comments below. Technology lies in mutual sharing and communication, haha.

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