Home Backend Development PHP Tutorial php怎么实现,E-mail验证激活用户

php怎么实现,E-mail验证激活用户

Jun 13, 2016 am 10:17 AM
active php url

php如何实现,E-mail验证激活用户?
意思是说,如何实现,用E-mail实现,发邮件来激活用户?
我的程序已经用了一个e-mail类了!
用于找回密码!
但是我想注册时,在加个用邮件来验证用户才能激活用户!
大家应该知道很多程序都带有,注册时 程序就会执行自动生成URL地址,发送至用户的邮件上!
用户点击了URL链接,就会激活用户!
但是我想知道如何实现呢?
就是自动生成URL地址那步不会,还有验证时不会,不过有段源码也不知道能不能用!
谁能告诉我自动生成URL地址怎么实现啊?


最好能给个实例代码,或者提供开源的程序带有我所说的功能!

  谢谢..

------解决方案--------------------
实例代码目前手上没有,开源程序有你说这功能的很多,比如uchome,discuz什么的。


其实原理很简单,你说自动生成URL那一步不会,首先URL分为两部分:
第一部分是一个验证地址并且带一个标识用户id的参数,比如:http://www.xxx.com/active.php?uid=1
第二部分其实就是一个用来验证的字符串,比如:authcode=asdad1f323ff43f
合起来就是http://www.xxx.com/active.php?uid=1&authcode=asdad1f323ff43f

第一部分是你来决定的,active.php就是你写的那个做验证脚本,所以你可以知道地址了?
第二部分不就是个随机生成的字符串吗?你可以用:md5(自己的域名+时间戳+验证的用户名)这个公式来生成这个串。当然你可以自己定义如何组合这个串,只要保证他们不是有规律让人一下猜到的,也别重复的就可以。
很简单是吗?


至于何时生成,何时验证,你可以这样设计:
你可以在用户表中,加两个字段:1 (bool isActive)用户是否已激活,默认为false。2 (string authcode)临时激活码。
当用户注册后,或点击激活按钮后,就执行你的一个脚本:
1 生成激活码,就是我上面说的方法。
2 将这个激活码,存储这个激活码到用户表的authcode字段。
3 将你的完整激活地址,用邮件发送到用户的邮箱
4 你的那个脚本,在接收到请求时,将两个参数取下来去用户表中搜索uid=1的用户的authcode是否与数据库中的相等,如果相等,验证通过,并清空update isActive字段为true,authcode字段更新为空。


就是这样,当然,为了避免生成了重复的验证码和别人的猜解,你可以给authcode设置超时时间。这个有很多种做法,你可以给用户表再加个字段是一个时间戳,用当前时间戳+有效的时间戳期限,比如time()+3600,这个就是一个小时的超时时间,验证时,你可以同时再比对下是否在这个时间戳之内。这种东西,你也可以放在缓存或内存中。这样会更好。


你可能还需要做一个开关,也许你今天想让新注册的用户都需要邮件激活,而明天也许你又不想了。用来控制,是否需要邮件激活。首先判断这个值为true的话,所有注册用户的isActive都为false,否则都为true。
------解决方案--------------------
除了考虑发邮件给客户


还要考虑,邮箱收不到的情况。因为不能保证100%的到达率

所以,还要提供一个邮箱,让客户发一邮件到指定邮箱

程序读取邮件列表,如果有来自A邮箱的邮件,并有注册用户用了该邮箱,就验证通过A

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles