2

我有一个偏执的客户的项目......他担心如果超过 200 人同时使用该网站,该网站会崩溃。

我的脚本看起来会收集数据吗?还是该文件会给用户错误?

<?php
if($_POST['name'] && $_POST['email']) {
    //file name var
    $fileName = "mycsvfile.csv";

    //grab from form
    $name = $_POST['name'];
    $email = $_POST['email'];

    //date
    date_default_timezone_set("America/Los_Angeles");
    $today = date("F j, Y, g:i a");

    //set the data we need into an array
    $list = array (
        array($today, $name, $email)
    );

    // waiting until file will be locked for writing (1000 milliseconds as timeout)
    if ($fp = fopen($fileName, 'a')) {
      $startTime = microtime();
      do {
        $canWrite = flock($fp, LOCK_EX);
        // If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
        if(!$canWrite) usleep(round(rand(0, 100)*1000));
      } while ((!$canWrite)and((microtime()-$startTime) < 1000));

      //file was locked so now we can store information
      if ($canWrite) {
        foreach ($list as $fields) {
            fputcsv($fp, $fields);
        }
      }
      fclose($fp);
    }

    //Send them somewhere...
    header( 'Location: http://alandfarfaraway' ) ;

}else{
    echo "There was an error with your name and email address.";
}
?>
4

1 回答 1

1

使用 MySQL 等数据库来存储数据。它将处理更高的负载。

http://php.net/manual/en/book.mysql.php

于 2012-03-15T22:16:57.960 回答