Php 不使用数据库生成顺序发票编号
我正在用PHP创建一个发票,我想为发票号生成一个序列号 我现在使用gettimeofday()来生成发票号,但这给了我一个非序列号,看起来像这样:46023913Php 不使用数据库生成顺序发票编号,php,Php,我正在用PHP创建一个发票,我想为发票号生成一个序列号 我现在使用gettimeofday()来生成发票号,但这给了我一个非序列号,看起来像这样:46023913 <?php $new_invoice = gettimeofday(); $new_invoice = $new_invoice[sec]; $new_invoice = $new_invoice - 1509000000; echo $new_invoice; ?> 创建一个文本文件“counter.txt”,其
<?php
$new_invoice = gettimeofday();
$new_invoice = $new_invoice[sec];
$new_invoice = $new_invoice - 1509000000;
echo $new_invoice;
?>
创建一个文本文件“counter.txt”,其中包含一个数字(1509000000) 读取包含文件内容(counter.txt)的文件,然后更新该文件 我已经有一段时间没有在php中工作了,但它是这样的 根据KIKO:锁定文件
<?php
$num = file_get_contents('counter.txt');
echo $num;
$handle = fopen('counter.txt','w+');
if (flock($handle,LOCK_EX)){
$num++;
fwrite($handle,$num);
fclose($handle);
// release lock
flock($handle,LOCK_UN);
} else {
echo "Error locking file!";
}
$num = file_get_contents('counter.txt');
echo $num;
Richardwhitney现在已经包含了文件锁,但是做得不是很好。如果锁已经存在,他的代码将产生错误。那不实际。下面的代码将等待10秒钟,以便解锁文件
// open the file
$handle = fopen("counter.txt","r+");
if ($handle) {
// place an exclusive lock on the file, wait for a maximum of 10 seconds
$tenths = 0;
while (!flock($handle, LOCK_EX)) {
$tenths++;
if ($tenths == 100) die('Could not get a file lock.');
usleep(100000);
}
// get old invoice number
$oldInvoiceNo = fgets($handle);
// create a new sequential invoice number
$newInvoiceNo = $oldInvoiceNo++;
// write the new invoice number to the file
ftruncate($handle, 0);
fwrite($handle, $newInvoiceNo);
// unlock the file
flock($handle, LOCK_UN);
// close the file
fclose($handle);
}
else die('Could not open file for reading and writing.');
锁定文件时,请始终尝试在尽可能短的时间内执行此操作
最好将此代码与其他代码隔离开来,例如在函数中
function getNewInvoiceNo($pathToCounter)
{
// open the file
$handle = fopen($pathToCounter, "r+");
if ($handle) {
// place an exclusive lock on the file, wait for a maximum of 10 seconds
$tenths = 0;
while (!flock($handle, LOCK_EX)) {
$tenths++;
if ($tenths == 100) die('Could not get a file lock.');
usleep(100000);
}
// create a new sequential invoice number
$newInvoiceNo = fgets($handle) + 1;
// write the new invoice number to the file
ftruncate($handle, 0);
fwrite($handle, $newInvoiceNo);
// unlock the file
flock($handle, LOCK_UN);
// close the file
fclose($handle);
}
else die('Could not open file for reading and writing.');
}
这是一个显而易见的解决方案,它将在99.999%的时间内有效。除非它没有。一些用户可能在您编写新发票号之前阅读了
counter.txt
的内容,瞧,您有两张相同编号的发票。你需要锁定文件。如果你加上这个,我会投票支持你的答案。嗨,理查德,谢谢你的回答,但这对我来说只适用一次。如果counter.txt是只读的,我每次得到101(我使用100而不是1509000000)。如果我让它可写,它会工作一次,但数字100会被删除,所以下次我什么也得不到。知道为什么counter.txt的内容会被删除吗?@karimborumand代码目前有点混乱,无法正常工作fopen()
应该返回一个文件句柄,但它不返回,并且$handle
被用作字符串。@KIKOSoftware感谢您的回复。我只是想确保我理解你的代码。如果它起作用的话,想法是取counter.txt中的任何数字,在其中添加1,并将该数字写入counter.txt文件,替换旧的数字。这是正确的吗?@karimborumand-是的,这是正确的。我已经更新了我的代码以提高效率和简单性。我花了一些时间来设置和测试它-它现在应该可以工作了-我更新了我的答案。添加$num++在php7.x中似乎不起作用使用$num=$num+1;instead@richardwhitney感谢您更新代码。但在写入counter.txt文件时,会添加空格,这会使所有内容在第二次运行时无法正常工作。”100(在counter.txt中以100开头)101(第二次超过3个空格)1(第三次超过6个空格,只写1而不是102)“知道为什么会发生这种情况吗?如果在fopen()中使用“w”或“w+”,则不应该这样做that@richardwhitney我把它和你给我的第一个代码一起使用。我认为它不起作用的原因是echo$num;这本书写了两遍。它就像一个符咒,比你给我的其他代码简单得多。无论如何,非常感谢你的帮助。