php如何生成不重复的卡号
就像电话卡似的,全是数字,不能带字母。
最开始想到是:
N位随机数+数据库一个表的自增id+N位随机数
不想用数据库,想到一个唯一的标准是时间。用时间戳跟上几位随机数。
假如这样生成16的卡号:
时间戳10位+6位随机数 1316651745695261
有时需要一个循环生成多个卡号,一个循环里前面的时间戳(基本是一样,除非循环里还有其它费时操作),后面的6位随机数无法保证唯一。想到结合microtime
循环调用microtime十次:
0.80717200 1316675664
0.80720600 1316675664
0.80721300 1316675664
0.80721900 1316675664
0.80722400 1316675664
0.80723000 1316675664
0.80723600 1316675664
0.80724100 1316675664
0.80724600 1316675664
0.80725200 1316675664
我想着是跟上位置在807172这里的数字便于区分开。代码:
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> for($i=0;$imicrotime_float(); echo $time = microtime(); $arr = explode(" ",$time); $micro = $arr[0]*100000000; $micro_str = substr($micro."",0,6); $timestamp = $arr[1]; echo "\t".$timestamp."".$micro_str; echo '<br>'; }
暂时是本地windows下测试不会有重复的,会不会有比较牛X的服务器计算能力猛,微秒也重复了?
另外这样生成的卡号也不好一眼能看出个啥啥的。又不能随意截取其中的几位怕失去了唯一性。不知道有木有好的方法。
比如移动充值卡生成机制?位数少一点的,太多了,人都晕了。。。
------解决方案--------------------
微秒数重复的可能性不大,况且你后面还有随机数,如果你不确定还可以增加usleep(1)等待下。
但如果服务器修改时间你就没法保证微秒数不重复了。
------解决方案--------------------
如果只用 时间戳(可重复)+随机数(可重复) 这样的话,是不能保证100%没冲突的......
建议:时间戳(可能重复)+随机数(可能重复)+数据库id或其它唯一值(唯一) 才能得到不重复的结果。

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
