Riprende questa discussione.
Girovagando ho trovato una funzione
per gestire il cd "Conditional GET"
per evitare eventueli, voluti o non voluti,
attacchi DOS.
Questo è il codice, è gradito qualsiasi intervento
Grazie
Codice PHP:
<?
header("Content-Type: text/xml;charset=ISO-8859-1");
/*
CONNESSIONE AL DB
*/
$connessione = mysql_connect ('host', 'user', 'psw');
$dbase = mysql_select_db ('DB', $connessione) ;
$query1 = " SELECT id,titolo,testo,data FROM tabella ORDER BY data DESC LIMIT 10";
$result1 = mysql_query($query1);
// prima parte dell'xml
$stringa = '<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="0.92">
<channel>
<title>Tuo sito</title>
<link>[url]http://www.sito.it[/url]</link>
<description>Le ultime del sito</description>
<language>it</language>
<image>
<title>Tuo sito</title>
<url>[url]http://www.sito.it/images/sito.gif[/url]</url>
<link>[url]http://www.sito.it/[/url]</link>
<width>88</width>
<height>30</height>
</image>
';
// tutti gli altri item
$a = 0 ;
while ( $row = mysql_fetch_assoc($result1) ){
if ($a=="0") {
$prima_data = $row["data"]; // mi memorizzo la data più recente
$a++;
}
$title = cleanText($row["titolo"]);
$link = cleanText("http://www.sito.it/visualizza.php?id=".$row["id"]);
$description = cleanText($row["testo"]);
$stringa .= "<item>
<title>$title</title>
<link>
$link</link>
<description>$description</description>
</item>
";
}
// chiude il file xml
$stringa .= "
</channel>
</rss>
";
mysql_free_result($result1);
doConditionalGet($prima_data);
print $stringa;
/*
FUNZIONI
*/
// testo HTML compatibile XML
function cleanText($intext) {
return htmlspecialchars(stripslashes($intext));
}
function doConditionalGet($timestamp) {
// A PHP implementation of conditional get, see
// [url]http://fishbowl.pastiche.org/archives/001132.html[/url]
$last_modified = substr(date('r', $timestamp), 0, -5).'GMT';
$etag = '"'.md5($last_modified).'"';
// Send the headers
header("Last-Modified: $last_modified");
header("ETag: $etag");
// See if the client has provided the required headers
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
false;
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) :
false;
if (!$if_modified_since && !$if_none_match) {
return;
}
// At least one of the headers is there - check them
if ($if_none_match && $if_none_match != $etag) {
return; // etag is there but doesn't match
}
if ($if_modified_since && $if_modified_since != $last_modified) {
return; // if-modified-since is there but doesn't match
}
// Nothing has changed since their last request - serve a 304 and exit
header('HTTP/1.0 304 Not Modified');
exit;
}
?>