javascript - 使用客户端IP作为cookie,识别重复访问并在第一次访问后显示消息

标签 javascript php jquery cookies

首先,提前感谢解决/帮助解决此问题的任何人。抱歉,如果这是重复的(我在任何地方都找不到,所以发布了一个新问题)。

所以主要问题是我希望我的网页显示警告消息,随后从用户第二次访问该页面开始,所以我认为使用 cookie 记录 IP 将是最独特的做法,请提出建议如果有更好的东西可以使用。

浏览到现在,没有找到在 cookie 中记录 IP 的方法。此外,我发现的解决方案有些相似,但它们是用 PHP 编写的,我并不擅长。

与 jQuery 相比,我更喜欢使用 JavaScript,但感谢所有帮助。

最佳答案

首先,我给出了识别重复访问的不同方法,并在第一次访问后显示消息。

我建议不要使用 IP 地址,因为 NAT 路由器后面可能有多台机器共享一个 IP 地址,还会有移动设备总是更改 IP 地址的问题,因为它们也会连接到不同的网络.

有多种方法可以做到这一点:

我建议使用选项 3选项 4,这样用户计算机上就不会存储任何内容。这样客户端就更加安全了,因为人们可以在他们的浏览器上存储 JavaScript。

选项 1:

您可以使用 HTML5 网络存储来拥有客户端本地存储。

HTML5 Web Storage

Before HTML5, application data had to be stored in cookies, included in every server request. Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.

Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.

选项 2:

您可以使用客户端 cookie,您可以使用 JavaScript 设置和删除它:

Cookies are data, stored in small text files, on your computer.

When a web server has sent a web page to a browser, the connection is shut down, and the server forgets everything about the user.

Cookies were invented to solve the problem "how to remember information about the user":

When a user visits a web page, his name can be stored in a cookie. Next time the user visits the page, the cookie "remembers" his name. Cookies are saved in name-value pairs like:

username=John

我实际上在几天前做过一个这样的例子。

COOKIE EXAMPLE

选项 3:

你可以有一个 session ,这是检查你是否有用户登录你的网站/应用程序的好方法:

PHP Sessions

A session is a way to store information (in variables) to be used across multiple pages.

Unlike a cookie, the information is not stored on the users computer.

When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state.

Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser.

So; Session variables hold information about one single user, and are available to all pages in one application.

选项 4:

您可以使用服务器端 cookie(这样它就不会存储在用户机器上),这是识别用户(访客)的好方法:

PHP Cookies

A cookie is often used to identify a user.

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

关于javascript - 使用客户端IP作为cookie,识别重复访问并在第一次访问后显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32754649/

相关文章:

javascript - 如何在 typescript 中使用 Prop 类型作为类型定义?

php - MySQL下拉框查询

php - 如何获取文件夹下的文件名?

javascript - 按类名删除所有跨度元素?

jquery 在父级中添加类

javascript - 我可以在元素关闭之前定位它吗?

javascript - 暂停 for 循环

javascript - 在 JavaScript 中休眠 - Action 之间的延迟

php - preg_replace 用于以冒号结尾的单词

jquery - Ajax自动完成url问题