<?php
class QueryMD5
{
var $GetParameter = array();
var $SecretKey = 'hRz655.5-';
var $Key = 'urlkey';
var $IgnoreSessionID = true;
var $IgnoreOrder = true;
function setIgnoreOrder($bool)
{
$this->IgnoreOrder = (bool) $bool;
}
function setIgnoreSessionID($bool)
{
$this->IgnoreSessionID = (bool) $bool;
}
function QueryMD5($arr)
{
if (is_array($arr)){
$this->GetParameter = $arr;
}else{
parse_str($arr, $tmp);
$this->GetParameter = $tmp;
}
}
function getQuery()
{
$s = $this->joinParameter($this->GetParameter);
$k = $this->calcMD5($s);
$s .= '&'.$this->Key.'='.$k;
return $s;
}
function calcMD5($s)
{
$tmp = md5($this->SecretKey.$s);
return $tmp;
}
function joinParameter($arr)
{
if ($this->IgnoreOrder){
ksort($arr);
}
$tmp = array();
foreach($arr as $k => $v){
$tmp[] = $k.'='.urlencode($v);
}
$s = join('&', $tmp);
return $s;
}
function checkQuery($s)
{
parse_str($s, $tmp);
$key = '';
if (array_key_exists($this->Key, $tmp)){
$key = $tmp[$this->Key];
unset ($tmp[$this->Key]);
}
if ($this->IgnoreSessionID){
unset ($tmp[session_name()]);
}
$s = $this->joinParameter($tmp);
$success = $key == $this->calcMD5($s);
return $success;
}
} /* end class */
session_start();
$para = array(
'id' => 21,
'tid' => 17,
'action' => 'delete',
);
$tmp = new QueryMD5($para);
$q = $tmp -> getQuery();
printf ('<h1>Die originale URL</h1>');
printf ('<a href="?%1$s">klicken ja, aber nicht an den Parametern rumeditieren</a>', $q);
if (!empty($_SERVER['QUERY_STRING'])){
echo '<hr>';
$bool = $tmp->checkQuery($_SERVER['QUERY_STRING']);
if ($bool){
print ('OK, der Querystring ist gültig');
}else{
print ('Fehler, der Querystring ist verändert');
}
}
?>
Dieses PHP Snippet soll in erster Linie als Beispiel und Anregung für eigene Bemühungen dienen.
Gerne darf man es für Projekte aller Art benutzen.
Möchte jemand das Snippet also solches in ähnlicher oder anderer Form veröffentlichen ist ein kleiner Hinweis auf simplecontent.net nicht zuviel verlangt oder Herr Koch?
Für die Abwesenheit von Fehlern kann natürlich keine Gewähr gegeben werden.