dongli7870 2016-09-08 07:40
浏览 75
已采纳

PHP读取文件不起作用

I have a problem with my orders. If a person presses F5 too fast, an order can be sent multiple times. I want to write the order_id into a file.

Before sending an order and check if the order_id is already stored and, if it is, dont send the order again.

I get the order_id, than I try to check if it's in the orders.txt file. If found in that file redirect to "mysite". If not stored, write it to orders.txt.

After the order_id is written to orders.txt send the order_id-XXXX.xml to a URL.

The thing is: only the else part works.

If I press F5 multiple times, it writes the order_id to the orders.txt file each time. It is not checking if it's already there. It also sends the order multiple times.

If I remove the inner if (file_exists($filename))else{} part, it works just fine.

Heres my code :

$order_id=  $order->get_order_number();
$filename = $_SERVER['DOCUMENT_ROOT']."/xml/order_id-".$order_id.".xml";
$_cassa_point_url='some_url';

$orders_txt = $_SERVER['DOCUMENT_ROOT']."/xml/orders.txt";

$contents = file_get_contents($orders_txt);
$pattern = preg_quote($order_id, '/');
$pattern = "/^(".$pattern.")/m";

    if( strpos(file_get_contents($orders_txt),$order_id) !== false) {
         header( "refresh:0;url=mysite );
    }else{
        $myfile = file_put_contents($orders_txt, $order_id."
" , FILE_APPEND | LOCK_EX);
        if (file_exists($filename)) {

            $url = $_cassa_point_url . $order_id;
            file_get_contents($url);
            header( "refresh:0;url=mysite);
            return true;            
        } else {
            $url = $_cassa_point_url . $order_id;
            file_get_contents($url);
            header( "refresh:0;url=mysite );
            return true;
        }
    }
  • 写回答

1条回答 默认 最新

  • douchun6221 2016-09-08 07:42
    关注
     header( "refresh:0;url=mysite );
    

    you forgot " in it so change it to

     header( "refresh:0;url=mysite");
    

    you have done it in the same way in else as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题