Home Backend Development PHP Tutorial 判断客户端是否具备JavaScript和Cookie功能_PHP

判断客户端是否具备JavaScript和Cookie功能_PHP

Jun 01, 2016 pm 12:39 PM
javascript judgment Function client

Cookie

在我的站点上,我想确认访问者是否具备cookies和JavaScript功能。维持会话状态在许多情况下都很复杂,而且也要考虑到表单的验证。在我的站点上,我使用了客户端JavaScript进行表单验证,所以要完成验证工作,用户的浏览器必须要具备JavaScript功能。同时,我还使用了Session变量,这就需要访问者的浏览器支持Cookies功能。为了确认这些前提条件的存在,我使用了一段小巧的代码来检查用户是否具备JavaScript和Cookies功能。如果用户不具备这2个条件,就拒绝他们登录网站。在这篇文章里,也介绍我的站点的登录页面代码。

  开始,使用JavaScript创建一个Cookie。然后,再检查这个cookie是否存在。因为我们使用JavaScript来测试用户是否打开Cookies功能,如果他们不具备JavaScript,但却具备Cookies,我们将会得到他们不具备Cookies功能的结果。这个结果是很好的,因为我要求用户同时具备JavaScript和Cookies功能。为了判断用户是否具备JavaScript功能,我在HTML页面上创建了一个隐藏表单域,并且在页面调用事件(onload)中调用一个JavaScript函数来改变这个隐藏表单域的数值。这样,如果表单域的数值改变了,就表示JavaScript是生效的。

  首先,在HTML页面上放置一个隐藏域,并将它介于 ... 之间。



...





  只要这个域的数值是false的,那么我们就知道了用户的浏览器不支持JavaScript。注意,默认值是false,我们编写的JavaScript函数将改变它为true。所以,如果JavaScript生效,这个JavaScript函数将运行,域的数值将变为true。在HTML页面的Body标记中,放置调用JavaScript函数的代码,从而检查cookies(cc):



  如果函数不执行(这种情况仅仅发生在JavaScript不生效时),cc()函数就不会改变隐藏域的数值。cc()函数的代码相当简单,如下:



  注意:在函数外的JavaScript代码行将在页面调入前执行,所以,也就是在cc()函数前被调用。通常,JavaScript代码块应该放在HTML页面的HEAD一节中。

  这样,如果JavaScript生效而Cookies不生效,用户将得到“需要打开cookies功能”的信息提示。如果JavaScript不生效,就不会显示任何信息,这种情况下,我们不能判断cookies功能是否打开。当表单提交时,检查隐藏域cookieexists的数值。如果等于false,那么表示不支持JavaScript。
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VMware Horizon client freezes or stalls while connecting [Fix] VMware Horizon client freezes or stalls while connecting [Fix] Mar 03, 2024 am 09:37 AM

When connecting to a VDI using the VMWareHorizon client, we may encounter situations where the application freezes during authentication or the connection blocks. This article will explore this issue and provide ways to resolve this situation. When the VMWareHorizon client experiences freezing or connection issues, there are a few things you can do to resolve the issue. Fix VMWareHorizon client freezes or gets stuck while connecting If VMWareHorizon client freezes or fails to connect on Windows 11/10, do the below mentioned solutions: Check network connection Restart Horizon client Check Horizon server status Clear client cache Fix Ho

The difference between vivox100s and x100: performance comparison and function analysis The difference between vivox100s and x100: performance comparison and function analysis Mar 23, 2024 pm 10:27 PM

Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

What exactly is self-media? What are its main features and functions? What exactly is self-media? What are its main features and functions? Mar 21, 2024 pm 08:21 PM

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

What is Discuz? Definition and function introduction of Discuz What is Discuz? Definition and function introduction of Discuz Mar 03, 2024 am 10:33 AM

"Exploring Discuz: Definition, Functions and Code Examples" With the rapid development of the Internet, community forums have become an important platform for people to obtain information and exchange opinions. Among the many community forum systems, Discuz, as a well-known open source forum software in China, is favored by the majority of website developers and administrators. So, what is Discuz? What functions does it have, and how can it help our website? This article will introduce Discuz in detail and attach specific code examples to help readers learn more about it.

PHP Tips: Quickly Implement Return to Previous Page Function PHP Tips: Quickly Implement Return to Previous Page Function Mar 09, 2024 am 08:21 AM

PHP Tips: Quickly implement the function of returning to the previous page. In web development, we often encounter the need to implement the function of returning to the previous page. Such operations can improve the user experience and make it easier for users to navigate between web pages. In PHP, we can achieve this function through some simple code. This article will introduce how to quickly implement the function of returning to the previous page and provide specific PHP code examples. In PHP, we can use $_SERVER['HTTP_REFERER'] to get the URL of the previous page

PHP MQTT Client Development Guide PHP MQTT Client Development Guide Mar 27, 2024 am 09:21 AM

MQTT (MessageQueuingTelemetryTransport) is a lightweight message transmission protocol commonly used for communication between IoT devices. PHP is a commonly used server-side programming language that can be used to develop MQTT clients. This article will introduce how to use PHP to develop an MQTT client and include the following content: Basic concepts of the MQTT protocol Selection and usage examples of the PHPMQTT client library: Using the PHPMQTT client to publish and

What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account? What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account? Mar 21, 2024 pm 04:16 PM

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

How to solve the problem that the Baidu Netdisk webpage cannot start the client? How to solve the problem that the Baidu Netdisk webpage cannot start the client? Mar 13, 2024 pm 05:00 PM

When many friends download files, they will first browse on the web page and then transfer to the client to download. But sometimes users will encounter the problem that the Baidu Netdisk webpage cannot start the client. In response to this problem, the editor has prepared a solution for you to solve the problem that the Baidu Netdisk webpage cannot start the client. Friends in need can refer to it. Solution: 1. Maybe Baidu Netdisk is not the latest version. Manually open the Baidu Netdisk client, click the settings button in the upper right corner, and then click version upgrade. If there is no update, the following prompt will appear. If there is an update, please follow the prompts to update. 2. The detection service program of Baidu Cloud Disk may be disabled. It is possible that we manually or use security software to automatically disable the detection service program of Baidu Cloud Disk. Please check it out

See all articles