Ich habe ein einfaches Skript, nur zählen, wie viele auf einen Link klicken, wenn der Link geklickt, ein PHP-Skript lesen "counter.txt" -Datei für den Link ID/URL/Label/Count dann plus 1 bei Zählung, schreibe in "counter.txt", dann Redirect.Mein PHP-Skript irgendwann schreiben leere Daten
counter.txt:
Label 1|1|http://google.com|1
Label 2|2|http://google.us|1
Label 3|3|http://google.uk|1
link.php:
<?php
//gets the content from "counter.txt"
$filecontent = file_get_contents('counter.txt');
//separates the contents of counter.txt by "\n" (spaces)
$filelines = explode("\n", $filecontent);
$url_array = null;
$new_file_content = "";
//for each $filelines variable, assign it as $fileline and execute code
foreach ($filelines AS $fileline)
{
//separates the contents of $fileline by "|"
$filelines_exploded = explode("|", $fileline);
//assigns counter.txt label to a variable
$label = $filelines_exploded[0];
//assigns counter.txt id's to a variable
$click_id = $filelines_exploded[1];
//assigns counter.txt url's to a variable
$click_url = $filelines_exploded[2];
//assigns counter.txt click count to a variable
$click_count = $filelines_exploded[3];
if ($_REQUEST["id"] == $click_id)
{
//$url_array contains all of the variables in an array
$url_array = $filelines_exploded;
//string to rebuild the counter.txt file
$new_file_content .= $label . "|" . $click_id . "|" . $click_url . "|" . ((int) $click_count + 1);
}
else
{
$new_file_content .= $fileline;
}
//adds a line break when rebuilding the counter.txt
$new_file_content .= "\n";
}
//file_put_contents to rewrite the file
file_put_contents('counter.txt', trim($new_file_content));
//redirects to the link gathered from the counter.txt file
header("HTTP/1.1 301 Moved Permanently");
header('Location: ' . $url_array[2]);
exit;
?>
Script arbeiten, aber irgendwann, blank counter.txt werden, alle Daten verloren, ich weiß nicht, warum, bitte schauen Sie, was mit meinem Code falsch ist.
Dank/
dies wäre eine Milliarde einfacher, wenn Sie eine db –
verwenden Zuerst versuchen Sie, Datei anstelle von file_get_contents zu verwenden, und Sie erhalten das Array, ohne die explodieren ('\ n', $ var); http://php.net/manual/en/function.file.php – legomolina