RSS the generator
What is RSS? If it is short, it is a format of an exchange of a content, basing on XML. Any Internet - resource containing the updated or filled up content, can zaimet` at itself RSS a tape and then users of this resource will receive a fast and convenient way to receive fresh materials. By the way, show RSS in a legible kind the same can Opera, and IE gives out simply formatted XML a file.
So, a problem : to write simple generator RSS of a tape for a site containing often the filled up content. The decision we write on PHP. And you as thought?:) Recordings of a tape we shall store{keep} in a separate file that it was easier to throw out out-of-date. Who it has suggested « to generate a tape dynamically, choosing the freshest recordings from MySQL »? Not prokatit, taking into account what to address to a database and to do{make} sample to us it is necessary each time at viewing a tape by the user. In our variant we have simple caching, and regeneration RSS occurs only at the moment of addition of new recording, and in general without the reference{manipulation} to base.
The circuit is simple: podgruzhaem the file with a tape, obrezaem old recording if it is too much them, also is finished new. A piece of a code, the explanatory on a course:
$rss_header_file = "rss/header.inc";
$rss_content_file = "rss/content.inc";
$rss_footer_file = "rss/footer.inc";
$rss_document_file = "rss/export.xml";
$rss_temp_file = "rss/tempfile";
$rss_miss_lines = 8; // Quantity{Amount} of lines in one recording
$rss_max_records = 10; // the Maximum quantity of recordings in a tape
// We load contents
$rss_content = file ($rss_content_file);
// If recordings more than it is necessary, we throw out the oldest
if (count ($rss_content)> $rss_miss_lines * $rss_max_records)
$rss_content = array_slice ($rss_content, $rss_miss_lines);
// We add I skin recording
// In corresponding variables the data should contain
// At addition / removal of fields to correct $rss_miss_lines
array_push (
$rss_content,
" <item> \n ",
"<title.>" $author. " </title> \n ",
"<link.>" $link. " </link> \n ",
"<guid.>" $link. " </guid> \n ",
"<description.>" $message. " </description> \n ",
"<pubDate>" .date ("r.") " </pubDate> \n ",
"<author.>" $author. " </author> \n ",
" </item> \n "
);
$fp = fopen ($rss_content_file, "w");
foreach ($rss_content as $rss_content_line) {
if ($rss_content_line! = "\n")
fwrite ($fp, $rss_content_line);
}
fclose ($fp);
// We collect a tape
$rss_document = array_merge (
file ($rss_header_file),
file ($rss_content_file),
file ($rss_footer_file)
);
// We use a temporary file that there were no conflicts of access
$rnd = rand (0, 1000);
$rss_temp_file = $rss_temp_file. $ rnd;
$fp = fopen ($rss_temp_file, "w");
foreach ($rss_document as $rss_document_line)
fwrite ($fp, $rss_document_line);
fclose ($fp);
unlink ($rss_document_file);
rename ($rss_temp_file, $rss_document_file);
All files for a tape are in a folder rss. It is useful to put also there an index file, pereadresujuhhij us on export.xml. In files header.inc and footer.inc contain, accordingly, heading and, khm... zavershitel` tapes. It is possible, certainly, was to register them in a code obviously but then the opportunity to correct them without zalezanija in a code is lost. Yes, also do not forget to format the added text properly, for example, with the help htmlspecialchars (nl2br ()).

|