Recently developing a WeChat official account, one feature allows users to send text messages to the official account, which then returns graphic messages to users. Users click the graphic message to jump to a webpage link that opens in WeChat's built-in browser.
This webpage first involves mobile web frontend development. I chose HTML5 + Bootstrap for the page's aesthetic effect, with other frontend tasks handled by JavaScript (I used pure native JavaScript without any frameworks, considering that mobile webpage loading is already slow, and many unused framework features would load with the page, consuming user data).
After all functions were ready and the client was satisfied with the trial, I submitted the code to the production server. However, the client suddenly mentioned: 'Your page is indeed beautiful and compatible, but this page can also be accessed by desktop browsers. Others can easily view the page source code and copy the entire page. Is there a way to prevent this?' This was my first WeChat secondary development, and I hadn't encountered this requirement. How to solve it?
I couldn't think of it immediately, so I set it aside and worked on backend business. When working on a data collection feature in the backend, the PHP code used the $_SERVER['HTTP_USER_AGENT'] parameter. I suddenly thought: when browsers access webpages, they send a UserAgent to the server containing basic browser and OS information. Since WeChat has a built-in browser, would the UserAgent contain WeChat-specific identifiers when browsing with WeChat? I decided to print its UserAgent to find out. JavaScript code:
<script type="text/javascript">
alert(navigator.userAgent);
</script>
On my phone, I got the following result:

There it was—MicroMessenger/6.0.0.50_r844973.501. The string after the slash is my current WeChat version number, and the part before should be a WeChat-specific identifier. At first I read it as 'MicroMessage,' thinking it meant 'micro message' in Chinese, but on closer inspection, it wasn't. After checking a dictionary, I learned that 'Messenger' means 'messenger, bearer of messages,' which made sense. This identifier shouldn't exist in other browsers, so the solution came. See the code:
<script type="text/javascript">
// Match the browser's UserAgent with regex; if it doesn't contain WeChat's unique identifier, it's another browser
var useragent = navigator.userAgent;
if (useragent.match(/MicroMessenger/i) != 'MicroMessenger') {
// This alert box will block the current page from continuing to load
alert('Access denied: You must use WeChat built-in browser to access this page!');
// The following code forcibly closes the current page
var opened = window.open('about:blank', '_self');
opened.opener = null;
opened.close();
}
</script>
This code was tested on Android, iPhone, iPad, and PC. As long as the page isn't opened within WeChat, the alert box pops up first, and the page behind is blank—nothing has loaded yet. When the user clicks OK on the alert, the last three lines of code forcibly close the current page. This achieves the client's original intent.
Wait, can we really call it done? Are you sure there won't be other issues? Of course not—this method isn't foolproof. Forged UserAgents can still bypass this restriction. It prevents gentlemen but not villains. If anyone has a better solution, please share in the comments—technology is about mutual sharing and exchange.
Original: http://www.jb51.net/article/57260.htm


