0

永続的な日付スタンプをテキストファイルに書き込んでから、ページが表示されるたびに読み戻すことで、永続的な日付スタンプを設定しようとしています。

// set the date, w/in if statements, but left out for brevity
$cldate = date("m/d/Y");
$data = ('clickdate' => '$cldate');  // trying to set a variable/value pair
 - It's throwing an Error on this !
// Open an existing text file that only has the word "locked" in it.
$fd = fopen("path_to_file/linktrackerlock.txt", 'a') or die("Can't open lock file");
// Write (append) the pair to the text file
fwrite($fd, $data); 
// further down …
// Open the text file again to read from it
$rawdata = fopen("path_to_file/linktrackerlock.txt", 'r');
// Read everything in from the file
$cldata = fread($rawdata, filesize("path_to_file/linktrackerlock.txt"));
fclose($rawdata);
// Echo out just the value of the data pair
echo "<div id='Since'>Clicks Since: " . $cldata['clickdate'] . "</div>";
4

2 に答える 2

3
$data = ('clickdate' => '$cldate');

する必要があります:

$data = array('clickdate' => $cldate);

さらに、fwriteステートメントに文字列を渡す必要があるため、配列を作成する必要はありません。

$cldate = date("m/d/Y");
if($fd = fopen("path_to_file/linktrackerlock.txt", 'a')){
    fwrite($fd, $cldate); 
    fclose($fd);
}else{
    die("Can't open lock file");
}
于 2012-12-21T16:03:06.110 に答える
1

コードは根本的に壊れています。配列を作成してから、その配列をファイルに書き出そうとしています。

$data = array('clickdate' => '$cldate');
        ^^^^^---missing

その後、あなたは持っています

fwrite($fd, $data); 

ただし、配列の内容ではArrayなく、ファイルに単語を書き出すだけです。あなたはそれを自分で試すことができます...ただやってecho $data、あなたが何を得るかを見てください。

次の方法で、この全体をはるかに簡単にすることができます。

$now = date("m/d/Y");
file_put_contents('yourfile.txt', $now);

$read_back = file_get_contents('yourfile.txt');

配列の使用を主張する場合は、シリアル化するか、JSONなどの別のエンコード形式を使用する必要があります。

$now = date("m/d/Y");
$arr = array('clickdate' => $now);
$encoded = serialize($arr);

file_put_contents('yourfile.txt', $encoded);

$readback = file_get_contents('yourfile.txt');
$new_arr = unserialize($readback_encoded);
$new_now = $new_arr['clickdate'];
于 2012-12-21T16:08:46.697 に答える