<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		
		<title>foobar.lamp-solutions.de Programmierung</title>
		<link>http://foobar.lamp-solutions.de/</link>
		<description>HowTos zum Thema Programmierung</description>
		<language>de</language>
		<image>
			<title>foobar.lamp-solutions.de Programmierung</title>
			<url>http://foobar.lamp-solutions.de/fileadmin/tt_news_article.gif</url>
			<link>http://foobar.lamp-solutions.de/</link>
			<description>HowTos zum Thema Programmierung</description>
		</image>
		<generator>TYPO3 - get.content.right</generator>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		
		
		
		<lastBuildDate>Wed, 28 Dec 2011 15:10:00 +0100</lastBuildDate>
		
		
		<item>
			<title>Mit PHP XML-Antworten aus SOAP-Requests effektiv Cachen </title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/mit-php-xml-antworten-aus-soap-requests-effektiv-cachen.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/mit-php-xml-antworten-aus-soap-requests-effektiv-cachen.html</guid>
			<description>Ein intelligentes Caching kann die Geschwindigkeit einer Webseite um mehrere Größenordnungen ...</description>
			<content:encoded><![CDATA[Mit PHP XML-Antworten aus SOAP-Requests effektiv Cachen<br /><br />
<h2>1) statisches Dateicaching für XML-Requests</h2>
<br />Webservices auf Basis von SOAP die Informationen über XML-Dokumente übertragen sind eine häufig verwendete Schnittstelle für Webanwednungen.<br /><br />Für den Fall, dass viele, sehr individuelle Daten zwischen zwei Webseiten übertragen werden, kann die Schnittstelle zum Flaschenhals für die Performance werden.<br /><br />Besonders negativ wirkt sich ein solcher Flachenhals aus, wenn die Daten aus den XML Anfragen direkt für die Ausgabe der Seite an Besucher durchgereicht werden. Damit kann sich der Seitenaufbau massiv verzögern.<br /><br />
<h2>2) Der PHP-Code</h2>
<h3>a) wichtige Konstanten definieren</h3>
<div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">//Daten eine Stunde cachen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$cachetime</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">3600</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">//Verzeichnis festlegen und evtl. anlegen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$cache_dir</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;xml_cache&quot;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/opendir"><span style="color: #990000;">openDir</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_dir</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <a href="http://www.php.net/mkdir"><span style="color: #990000;">mkdir</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div><br /><br />Natürlich benötigt die PHP-Anwendung Schreibrechte auf den Ordner in dem wir unsere Daten ablegen wollen.<br /><br />
<h3>b) Hash für Anfrageoptionen bilden.</h3>
Wichtig ist, dass wir Anfragen mit individuellen Parametern individuelle Cache-Dateien bilden. In der Regel werden die Optionen die ein SOAP-Client an einen Server übergibt in Form eines Arrays definiert.<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$options</span><span style="color: #339933;">=</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a>  <span style="color: #009900;">&#40;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">&quot;Art&quot;</span>            <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">&quot;Id&quot;</span>             <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">,</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">&quot;Anzahl&quot;</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">&quot;Preis&quot;</span>         <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3000</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div><br />Wenn wir in unserem Beispiel alle Optionen ganze Zahlen sind, bietet es sich an einen Hashwert für unsere Anfrage direkt aus den Zahlen zu bilden.<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$option</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$hash</span><span style="color: #339933;">.=</span><a href="http://www.php.net/intval"><span style="color: #990000;">intval</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$option</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div><br />Falls auch Zeichenketten als Optionen übergeben werden, bietet es sich an den Hashwert zusätzlich mit sha1 zu codieren<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$option</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$hash</span><span style="color: #339933;">.=</span><span style="color: #000088;">$option</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$hash</span><span style="color: #339933;">=</span><a href="http://www.php.net/sha1"><span style="color: #990000;">sha1</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div><br /><br />anschließend geben wir unserer Cache-Datei einen Namen:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$file</span><span style="color: #339933;">=</span><span style="color: #000088;">$cache_dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$hash</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.xml&quot;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div>
<h3>c)Prüfen ob die Daten aus dem Cache, oder Soap abgefragt werden sollen</h3>
Es gibt drei Fälle in denen wir die Informationen über SOAP neu einlesen müssen
<ol><li>Falls noch kein Cache existiert</li><li>Falls die Cache-Zeit abgelaufen ist</li><li>Falls wir unserem System (zum Beispiel zur Fehlersuche) einen entsprechenden get-Parameter mitgeben</li></ol>
<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">//prüfen ob cache-Datei existiert </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$filetime</span><span style="color: #339933;">=</span><a href="http://www.php.net/filemtime"><span style="color: #990000;">filemtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$toggle</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">//Regeln um vorhandenen Cache neu anzulegen </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #000088;">$filetime</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$cachetime</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">//Datei älter als einen Tag </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$toggle</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;no_soap_cache&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">//no_cache aktiviert </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$toggle</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div>
<h3>d) Daten auswerten</h3>
Sobald wir bestimmt haben, ob wir die Daten aus dem Cache oder über SOAP abrufen, können wir uns ans Auswerten der Informationen machen.<br /><br />Auf die Details der eventuell durchgeführten SOAP abfrage wollen wir hierbei an dieser Stelle nicht eingehen:
<div class="indent"><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$toggle</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #000088;">$xml</span><span style="color: #339933;">=</span><a href="http://www.php.net/simplexml_load_file"><span style="color: #990000;">simplexml_load_file</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">//Erzeuge Soap-Client </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapClient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;WSDL-Dokuement&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  try<span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$soap</span><span style="color: #339933;">=</span><span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$gateway</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$xml</span><span style="color: #339933;">=</span><a href="http://www.php.net/simplexml_load_string"><span style="color: #990000;">simplexml_load_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$soap</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">any</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">//Cache schreiben </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <a href="http://www.php.net/unlink"><span style="color: #990000;">unlink</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$fileh</span><span style="color: #339933;">=</span><a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">   <a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileh</span><span style="color: #339933;">,</span><a href="http://www.php.net/utf8_encode"><span style="color: #990000;">utf8_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">   <a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div><br />Selbst komplexe Anfragen dauern damit nur noch wenige Millisekunden. Alleine die eingesparten Latenzzeiten können, wenn mehrere Abfragen hintereinander durchgeführt werden, den Unterschied für ein positives oder negatives Surferlebnis der Besucher ausmachen.<br /><br /></div>]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Fri, 09 Sep 2011 15:51:00 +0200</pubDate>
			
		</item>
		
		<item>
			<title>Mehrere CPU-Kerne mit einem php-Script ausnutzen</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/mehrere-cpu-kerne-mit-einem-php-script-ausnutzen.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/mehrere-cpu-kerne-mit-einem-php-script-ausnutzen.html</guid>
			<description>PHP-cli Scripte können durchaus auf mehreren CPU-Kernen skalieren. Ein einfaches Beispiel zeigt,...</description>
			<content:encoded><![CDATA[Moderne Server haben oft eine Vielzahl an CPU-Kernen zur Verfügung. Bei Webanwendungen braucht sich der Programmierer in der Regel nicht besonders um eine Verteilung der Rechenlast auf die CPU-Kerne zu kümmern. Webserver sind meist dann besonders ausgelastet, wenn viele gleichzeitige Anfragen an das System gestellt werden. Der Webserver und der Betriebssystemkern kümmern sich dann selbständig um eine Verteilung der Anfragen auf die CPU-Ressourcen.<br /><br />Anders sieht es aus, wenn Programm selbst sehr ausführliche Arbeiten zu erledigen hat. Das können zum Beispiel cronjobs sein, die Logfiles auswerten, oder in regelmäßigen Abständen die Datenbank aufräumen sollen, oder Daten von externen Seiten sammeln sollen. Solange nicht die Festplatten die entscheidende Performacebremse sind, kann es sinnvoll sein diese Aufgabe auf mehrere CPU-Kerne zu verteilen.<br /><br />PHP bietet keine umfassende Thread-Unterstützung wie zum Beispiel c oder c++.<br />Mit Hilfe der Funktion <span style="font-weight: bold;">pcntl_fork</span> lassen sich aber die meisten Anforderungen, wenn auch zum Teil etwas umständlich, umsetzten.<br /><br />Gute Einführungen in die Möglichkeiten der Funktion bieten die Seiten.
<link https://sites.google.com/a/van-steenbeek.net/archive/php_pcntl_fork - external-link-new-window "Öffnet externen Link in neuem Fenster">https://sites.google.com/a/van-steenbeek.net/archive/php_pcntl_fork</link><br /><link http://php.net/manual/de/function.pcntl-fork.php - external-link-new-window "Öffnet externen Link in neuem Fenster">http://php.net/manual/de/function.pcntl-fork.php</link><br /><br />Da uns keine sinnvolle rechenintensive Aufgabe eingefallen ist, die sich mit wenigen Zeilen php umsetzten lässt, haben wie eine sehr einfache Funktion gebaut, die nichts anderes macht, als 100.000 mal die Fakultät der Zahl 100 zu berechnen.<br /><br />Folgendes Beispielscript verteilt die Rechenoperationen auf zwei Kerne:<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">Berechnte Fakultät </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">*/</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">function</span> getFaculty <span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$int</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$intFaculty</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$int</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span><span style="color: #000088;">$int</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$intFaculty</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$int</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$intFaculty</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">Verursache CPU-Last </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">*/</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">function</span> do_stupid<span style="color: #009900;">&#40;</span><span style="color: #000088;">$anfang</span><span style="color: #339933;">,</span><span style="color: #000088;">$ende</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #000088;">$anfang</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$ende</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        getFaculty<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$pid</span> <span style="color: #339933;">=</span> pcntl_fork<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Konnte nicht verzweigen'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$anfang</span><span style="color: #339933;">=</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    do_stupid<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$ende</span><span style="color: #339933;">=</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$zeit</span><span style="color: #339933;">=</span><span style="color: #000088;">$ende</span><span style="color: #339933;">-</span><span style="color: #000088;">$anfang</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Vater: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$zeit</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$anfang</span><span style="color: #339933;">=</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    do_stupid<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000000</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$ende</span><span style="color: #339933;">=</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$zeit</span><span style="color: #339933;">=</span><span style="color: #000088;">$ende</span><span style="color: #339933;">-</span><span style="color: #000088;">$anfang</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Kind: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$zeit</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$anfang</span><span style="color: #339933;">=</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">do_stupid<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$ende</span><span style="color: #339933;">=</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$zeit</span><span style="color: #339933;">=</span><span style="color: #000088;">$ende</span><span style="color: #339933;">-</span><span style="color: #000088;">$anfang</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Allein: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$zeit</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></pre></div></div><br /><br />Die Ausgabe zeigt, dass die Laufzeit eines einzelnen Prozesses genau halb so lange ist wie wenn die gleiche Anzahl von Anfragen nur auf einem Kern ausgeführt wird:
Kind: 5<br />Vater: 5<br />Allein: 10<br />Allein: 10<br /><br />Der letzte Codeblock wird im Vater und im Kindprozess ausgeführt, da der Kindprozess ja ein Klon&nbsp; des ursprünglichen Prozesses ist.<br /><br />Ein Screenshot des Programms htop zeigt gut, wie zwei CPU-Kerne ausgelastet werden.]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Mon, 25 Apr 2011 17:05:00 +0200</pubDate>
			
		</item>
		
		<item>
			<title>Joomla! Probleme mit open_basedir beheben</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/joomla-probleme-mit-open-basedir-beheben.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/joomla-probleme-mit-open-basedir-beheben.html</guid>
			<description>Joomla!-Module bei Konflikten mit open_basedir installieren und konfigurieren.</description>
			<content:encoded><![CDATA[Joomla! bietet vielerlei Plugins, Themes und anderweitige Extensions für so ziemlich alles. Doch oft passiert es, dass der Benutzer durch unliebsame Fehler im Backend gehindert wird diese zu installieren. Vor allem bei vielen Hostern, bei denen man keinerlei Zugriff auf Servereinstellungen hat, kann das einen schon einmal in den Wahnsinn treiben.&nbsp; Im folgenden wollen wir daher eine kleine Sammlung zur Verfügung stellen, welche zumindest die häufigsten Fehler beseitigt.<br /><br />
<h2>1) Fehler in der configurations.php</h2>
<br />In der configurations.php werden alle wichtigen Einstellungen wie Datenbank-, oder FTP-daten hinterlegt. Unter anderen gibt es dort auch folgende Einträge:<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="vim" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">var <span style="color: #000000;">$</span>log_path = <span style="color: #C5A22D;">'/var/www/ihre-domain.tld/htdocs/logs'</span>;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">var <span style="color: #000000;">$</span>tmp_path = <span style="color: #C5A22D;">'/var/www/ihre-domain.tld/htdocs/tmp'</span>;</div></li></ol></pre></div></div><br /><br />Diese beschreiben zwei wichtige Pfade der Installation. Stimmen diese nicht (mehr), zum Beispiel nach einem Domainumzug, kann es dazu führen, dass keinerlei Erweiterungen installiert werden können, weil der angegebene Pfad zum Temp-Verzeichniss nicht mehr stimmt. Als erstes sollten daher diese Einträge überprüft werden. Wichtig ist zudem, dass der absolute Pfad angegeben wird. Sollte dieser nicht bekannt sein, bedient man sich einfach folgendem PHP-Schnipsel:<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Derzeitiges Verzeichnis</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">echo</span> <a href="http://www.php.net/getcwd"><span style="color: #990000;">getcwd</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></pre></div></div><br /><br />Natürlich sollte man nicht das /tmp bzw /logs hinter den Einträgen dann vergessen.<br /><br /><br />
<h2>2) JFolder::create: Path not in open_basedir paths </h2>
<br />Die folgende Fehlermeldung konnten wir mehrfach beobachten:<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="vim" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">JFolder<span style="color: #000000;">::</span>create<span style="color: #000000;">:</span> Path not <span style="color: #804040;">in</span> open_basedir paths </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">Unable <span style="color: #668080;">to</span> create destination</div></li></ol></pre></div></div><br /><br />Diese besagt an sich lediglich, dass ein Problem mit den open_basedir Restriktionen vorliegt. Man sollte daher überprüfen ob alle nötigen Pfade wirklich frei gegeben sind. Auch hier kann man sich einem kleinen PHP-Schnipsel bedienen, vor allem wenn man sich auf einem Shared Host befindet, kann man dem Support damit mitteilen, welcher Pfad weiterhin freizugeben ist:<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Derzeitiges Verzeichnis</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">echo</span> <a href="http://www.php.net/ini_get"><span style="color: #990000;">ini_get</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'open_basedir'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></pre></div></div><br /><br />Sollte hier schon alles in Ordnung sein, muss man ein wenig schummeln. Wir konnten ebenfalls den Fehler beobachten, dass trotz korrekter Einstellung einige Installationen von Joomla! den obigen Fehler meldeten. Dabei behalfen wir uns folgender Anpassung:<br /><br />Zuerst öffnen wir folgende Datei:<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="vim" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #668080;">vi</span> <span style="color: #000000;">/</span>var<span style="color: #000000;">/</span>www<span style="color: #000000;">/</span>ihre<span style="color: #000000;">-</span>domain<span style="color: #000000;">.</span>tld<span style="color: #000000;">/</span>htdocs<span style="color: #000000;">/</span>libraries<span style="color: #000000;">/</span>joomla<span style="color: #000000;">/</span>filesystem<span style="color: #000000;">/</span>folder<span style="color: #000000;">.</span>php</div></li></ol></pre></div></div><br /><br />In diese Datei suchen wir folgenden Eintrag:<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$obd</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/ini_get"><span style="color: #990000;">ini_get</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'open_basedir'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div><br /><br />Diesen kommentieren wir für die Installation der Extension einfach aus indem wir ein &quot;#&quot; davor setzen:<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="vim" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">#<span style="color: #000000;">$</span>obd = ini_get<span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'open_basedir'</span><span style="color: #000000;">&#41;</span>;</div></li></ol></pre></div></div><br /><br />Was wir dadurch bewirken ist, dass die Abfrage des derzeitigen open_basedir's umgangen wird. Joomla! bekommt nun mitgeteilt, dass es keine Restriktionen gäbe und führt nicht zu falschen Fehlermeldungen. Die Installation sollte nun ohne Probleme funktionieren. Man sollte es jedoch später wieder aktivieren, da unter Umständen auch installierte Module darauf zugreifen und sonst nicht mehr funktionieren könnten. <br /><br />Dazu zu erwähnen ist außerdem, dass der Code, der seitens Joomla! open_basedir überprüft komplett richtig ist. Wieso es dabei zum Teil zu Fehlern kommt, scheint ein reiner Bug in einer eingebundenen Extension&nbsp;zu sein.<br /><br />
<h2>3) Schlusswort</h2>
Diese Möglichkeiten bieten nur einen Ansatz der bei uns in 99% der Fälle zum Erfolg führten. Die Möglichkeiten werden insofern neue Erkenntnisse hinzukommen auf jeden Fall erweitert. Zudem sollte man stets die aktuelle Version von Joomla! verwenden (derzeitig 1.6) um eventuelle Bugs in alten Versionen auszuschließen. Ein weiterer Aspekt für ein Update auf die aktuelle Version ist zudem, dass auch Sicherheitslücken geschlossen werden auf die ansonsten gezielt Angriffe vorgenommen werden können. ]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Wed, 19 Jan 2011 15:56:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title>Flash-Diagramme mit PHP erzeugen</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/flash-diagramme-mit-php-erzeugen.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/flash-diagramme-mit-php-erzeugen.html</guid>
			<description>Diagramme erleichtern statistische Darstellungen ungemein und fast jede Administration benötigt...</description>
			<content:encoded><![CDATA[Jeder Entwickler muss mal Projekte entwickeln, in dem ein Adminbereich eingesetzt wird und dort auch Statistiken dargestellt werden. Die meisten Entwickler reduzieren sich darauf, Statistiken tabellarisch darzustellen, aus gutem Grund. Denn zum einen sind diese Schleifen einfacher und schneller umgesetzt und man kann eine Vielzahl an Details mit Tabellen darstellen.
Heutzutage sind für den Anwender allerdings grafische Diagramme mittlerweilen Standard, weshalb in einigen Administrationen mehr oder weniger aufwendig mittels GDLib und meistens fertigen Klassen und/oder Frameworks grafische Diagramme mittels zuvor generierte Datenarrays oberhalb der Tabellen dargestellt werden. Solche Vorgehensweisen, mittels der GDLib oder Imagick grafische Diagramme dynamisch zu erzeugen und darzustellen, kosten auf einem Server gewaltig Resourcen und setzt man sogar viele solcher dynamischen grafischen Diagramme ohne Caching  dar, kann das auch schon mal gewaltige Ausmaße annehmen. Wieso also das Ganze nicht klientseitig vornehmen?
Eine hervorragende und auch simple Alternative ist der <link http://teethgrinder.co.uk/open-flash-chart-2/ _blank external-link-new-window "Öffnet externen Link in neuem Fenster">OpenFlashChart-2</link> (kurz OFC2), das OpenSorce ist und mittels einem JSON-Objekt dynamische, benutzerfreundliche und sogar animierte grafische Diagramme mittels Adobe Flashplayer darstellt und auch vorgefertigte PHP-Klassen hierzu bereitstellt.
Die Daten für die jeweilige Chart werden erst mittels asnychroner Anfrage vom OFC2 angefordert, wenn das Flashchart im Quelltext aufgerufen wird, was noch den zweiten Effekt bringt, dass die Arbeit der PHP-Skripte für die Darstellung erst bei reale Ansicht beginnt und nicht schon, wenn die Seite geparsd wird. In diesem Artikel möchte ich anhand eines kleinen Beispiels eine solche Flashchart vorführen. Der Lesbarkeit wegen erzeuge ich das Datenarray manuell, was natürlich in einem realem Projekt durch Datenbankdaten erzeugt werden kann.
Für dieses Beispiel verwende ich OFC in der Version 2 von Lug Wyrm Charmer, welches man sich als ZIP <link http://sourceforge.net/projects/openflashchart/files/open-flash-chart/ _blank external-link-new-window "Öffnet externen Link in neuem Fenster">hier herunterladen</link> kann. Aus diesem ZIP-File, in dem auch diverse Demos, Beispiele und für verschiedene Scriptsprachen vorhandene Frameworks vorhanden sind, benötigt man lediglich alles aus dem Ordner php-ofc-library (mit dem php5-ofc-library habe ich persönlich keine einzge Demo zum laufen gebracht, weshalb ich die alten PHP-Libraries verwende). Diesen Ordner kopiert man in seinem Projekt dort hin, wo bestenfalls zentral alle Fremdbibliotheken gelagert sind.
Weiterhin muss man die open-flash-chart.swf sowie /js/swfobject.js in sein Dokumentroot des Projektes kopieren. Mittels dem Javascript-Objekt swfobject in der swfobject.js werden die Browserweiche für die Flashplayer sowie alle benötigten Javascript-Funktionen für die Erstellung von Objekt-Tags im HTML-Code zur Verfügung gestellt, weshalb diese auch im Head der Seite eingebunden werden muss:
<div id="tx_codehighlight_pi1"><div class="code"><pre class="html4strict" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;swfobject.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">....</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span></div></li></ol></pre></div></div>
<h3></h3>
<h3>Das Datenscript für die JSON-Diagramm-Daten</h3>
Um Daten darzustellen, erzeugen wir eine PHP-Datei, die unsere Diagrammdaten dynamischen beschafft und mittels der OFC2-PHP-Bibliothek die benötigte JSON-Ausgabe erzeugt inklusive aller Angaben, wie unser Diagramm aussehen soll. In dieser Datei definieren wir mit dem Aufruf der entsprechenden Diagramm-Klasse, was für ein Diagramm wir darstellen wollen (Kuchen-, Balken- oder Liniendiagramm), welche Labels wir auf der X- sowie Y- Achse vergeben wollen und wie die Daten ausgegeben werden (mit Labels, Spitzen mit Darstellungspunkten, usw.). Die OFC2-PHP-Klasse erzeugt bei Erfolg am Ende eine saubere JSON-Ausgabe, die man mit echo lediglich zurück gibt und von dem Flash-Diagramm für die Darstellung verwendet wird.
Zu Beginn des Scriptes sollte man das gesamte Framework-Verzeichnis in den Include-Path aufnehmen, um nicht alle benötigten Scripte manuell inkludieren zu müssen. Im Anschluss inkludieren wir die open-flash-chart Klasse, setzen die X- sowie Y- Achse, den Titel und einige Eigenschaften des Diagramms:
<div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.php.net/set_include_path"><span style="color: #990000;">set_include_path</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/get_include_path"><span style="color: #990000;">get_include_path</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">':'</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__FILE__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/php-ofc-library'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'open-flash-chart.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Die X-Achse erzeugen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> x_axis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$x</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_3d</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$x</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_colour</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#b1b1b1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$x</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_grid_colour</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#ffffff'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Y-Achse</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> y_axis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$y</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_range</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$y</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_colour</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#b1b1b1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$y</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_grid_colour</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#ffffff'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Open-Flash-Chart-JSON einstellen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> title<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'OFC-Beispiel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$title</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_style</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'{font-size:11px; color:#b1b1b1;}'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$chart</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> open_flash_chart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_title</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_bg_colour</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#ffffff'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_x_axis</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_y_axis</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Ein einfaches Balkendiagramm mit Animierung erzeugen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$container</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">'Label 1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">'Label 2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">9</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$container</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tip</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">// Hier erzeugen wir je Balkenwert einen Balken</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> bar_value<span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$bar</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_colour</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#ff0000'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$bar</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_tooltip</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Das Balkendiagramm erzeugen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> bar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$bar</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_values</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$bar</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_on_show</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> bar_on_show<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'grow-up'</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.7</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// OFC das Balkendiagramm hinzufügen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_element</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toPrettyString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div>
Natürlich kann man noch diverse Dinge mehr formatieren, so z.B. der X und Y - Achse Labels geben, die Ansicht und Schritte zwischen Zahlbereichen einstellen und auch farblich anpassen. 
Auch ist man natürlich nicht auf Balkendiagramme beschränkt und kann andere Diagramm-Formen erzeugen, die in der <link http://teethgrinder.co.uk/open-flash-chart-2/tutorial.php _blank external-link-new-window "Öffnet externen Link in neuem Fenster">Dokumentation / Tutorials</link> mit diversen Beispielen erläutert werden.
Entscheidend ist, dass man die JSON-Ausgabe zuvor zum Beispiel unter <link http://www.jsonlint.com/>http://www.jsonlint.com/</link> auf Validität prüft, denn ist die Ausgabe nicht 100% JSON-Valide, erscheint lediglich eine nichts aussagende Fehlermeldung dort, wo das Diagramm dargestellt werden soll. 
<h3>Die HTML-Seite, wo das Diagramm dargestellt werden soll</h3>
Um das Diagramm nun an einer Stelle im Dokument anzuzeigen, setzt man folgenden HTML-Code an deren Platz:
<div id="tx_codehighlight_pi1"><div class="code"><pre class="html4strict" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">swfobject.embedSWF(</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  '/open-flash-chart.swf',</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  'divId',</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  '460',</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  '160',</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  '9.0.0',</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  'expressInstall.swf',</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  { 'data-file':'/statistik.php' }</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">);</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;divId&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span></div></li></ol></pre></div></div>
Mit dem Javascript-Objekt erzeugen wir klientseitig ein OBJECT-Tag innerhalb des DIV, welche mit der ID &quot;divId&quot; deklariert ist. Dazu wird das OFC2-Script open-flash-chart.swf im ersten Parameter aufgerufen (natürlich inklusive richtigen Pfad, falls das Scipt nicht im gleichen Verzeichnis wie die HTML-Datei liegt), im zweiten Parameter teilen wir die ID des Containers mit, wo die Chart als Objekt eingebettet wird, im dritten und vierten Parameter geben wir die Breite und Höhe des Diagramms an, im 5. Parameter teilen wir der eingebauten Browserweiche im SWFObject mit, dass wir mindestens die Version 9.0.0 benötigen, mit dem 6. Parameter teilen wir dem Objekt mit, dass wenn die benötigte Playerversion nicht vorhanden ist, möglichst eine Express-Installation vorgenommen wird und im letztem 7. Parameter geben wir im JSON-Format mindestens das Datenfile mit data-file abgehend von dem Dokumentroot an, welches unsere Diagramm-OFC-Daten als JSON ausgibt.
Sofern alle Pfade und Dateinamen in dem Objektaufruf korrekt sind, sollte beim Aufruf die HTML-Datei das Diagramm animitert (Balken fahren je nach Daten langsam hoch) anzeigen. Selbstverständlich muss ein Webserver im Hintergrund laufen, da das PHP-Script welches die JSON-Ausgabe für das Diagramm erzeugt durch PHP geparst werden muss.
<h3>Fazit</h3>
Mittels OFC2 lassen sich sehr schnell und unkompliziert beeindruckende, animiterte Diagramme erzeugen, die zudem auch die Performance der Seite zumindest beim Laden nicht beeinflusst. Darüber hinaus überzeugen genau solche auch sehr benutzerfreundlichen Diagramme jeden Anwender, da es Rohdaten visuell viel eindeutiger und auffälliger darstellt.
Die <link http://teethgrinder.co.uk/open-flash-chart-2/tutorial.php _blank external-link-new-window "Öffnet externen Link in neuem Fenster">Dokumentation</link> von OFC2 ist leider zwar nicht sehr ausführlich und auch nur schwerlich zu durchschauen, aber mit etwas probieren erzeugt man schnell beeindruckende Diagramme. Sofern man nicht aufwendig gestaltete Diagramme benötigt ist OFC2 die Wahl für statistische Darstellungen.<br /><br />(sj)]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Thu, 13 Jan 2011 13:18:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title>PHP Performance &amp; Security - Teil 4</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/php-performance-security-teil-4.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/php-performance-security-teil-4.html</guid>
			<description>Performance durch richtige Programmierung steigern.</description>
			<content:encoded><![CDATA[Im Anschluss der anderen drei Teile dieser Serie möchte ich hier einige häufig zu findende unperformante Verwendungen mit den passenden performanten Lösungen aufzeigen. Insbesondere aber auch der häufige unnötige Einsatz von den Perl-kompatiblen Funktionen preg_* nimmt sehr viel Performance, welches man sehr häufig auch mit den verschiedenen PHP eigenen str* Funktionen sehr viel performanter benutzen kann.<br /><br />Sehr häufig insbesondere von Anfängern geschriebener Code sieht folgendermaßen aus:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$conf</span><span style="color: #009900;">&#91;</span>key1<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;wert&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$conf</span><span style="color: #009900;">&#91;</span>key2<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;wert&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li></ol></pre></div></div><br /><br />Wo liegt hier nun ein Fehler? Nirgendwo! Es ist schlicht weg nur unsauberer aber auch sehr unperformanter Code, denn der Intepreter vermutet hinter key1 eine Konstante wird aber feststellen, dass es ein String ist und geht nun folgendermaßen vor:<br />- String verkleinern mit strtolower<br />- Suche im Array<br />- Generierung einer E_NOTICE, die möglicherweise - falls mitgeloggt wird - mittels fwrite ins Logfile auch noch geschrieben werden muss<br />- temporär den String erzeugen, um auf den Hashwert zugreifen zu können.<br /><br />Hier ein kurzer Vergleich, wieviel Milisekunden hierdurch je unsaubere Array-Variable vergeudet wird:
<table cellpadding="2" cellspacing="0"><thead><tr><th scope="col">Anzahl Zeichen</th><th scope="col">Konstantenmissbrauch</th><th scope="col">String-Variante</th></tr></thead><tbody><tr><td>3 Zeichen</td><td>15,4 ms</td><td>2,5 ms</td></tr><tr><td>6 Zeichen</td><td>16,6 ms</td><td>2,7 ms</td></tr><tr><td rowspan="1">17 Zeichen</td><td rowspan="1">22,1 ms</td><td rowspan="1">3,1 ms</td></tr></tbody></table>
<br />Dies zeigt eindeutig, dass man gerade bei häufiger Verwendung von assoziativen Arrays zum einen korrekt programmieren (also $conf['key1'] anstelle von $conf[key1]) und bestenfalls kurze Index-Strings verwenden sollte.<br />&nbsp;<br />Einen weiteren Performanceverlust findet man in der folgenden sehr häufig benutzten Schleife:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <a href="http://www.php.net/sizeof"><span style="color: #990000;">sizeOf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$arr</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">// tue was</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div><br /><br />In dieser Schleife wird der initialisierte Parameter $i auf kleiner als Länge des Arrays $arr geprüft. Die PHP-Funktion sizeOf() allerdings ist ebenfalls eine im PHP-Kern definierte Funktion, die ein Array iteriert und die Anzahl gefundener Elemente als Integer zurück gibt. Man benutzt hier also eine PHP-Funktion in einer Schleife, wo jeder Durchlauf der Schleife erneut diese PHP-Funktion ausführt. Sofern man das Array $arr nicht innerhalb der Schleife verändert und tatsächlich das geänderte Array für die Schleife benötigt, steigert man bei einer ausgelagerten Zeile Code die Performance um zig Prozent. Performanter wäre folgender Ansatz:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sizeof"><span style="color: #990000;">sizeOf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$arr</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$length</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">// tu was</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div><br /><br />Bei diesem Vorgehen wird die PHP-Funktion sizeOf() lediglich ein mal aufgerufen und gibt die Anzahl der Array-Elemente als Integer zurück, der dann in der Schleife direkt benutzt wird. <br /><br />Sehr gern möchte man bei bestimmten PHP-Funktionen auch eventuell auftretende Fehler mit einem vorangestelltem @ unterdrücken. Wissen Sie, was der Interpreter daraus erzeugt?<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'datei.txt'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// erzeugt:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$old</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/ini_set"><span style="color: #990000;">ini_set</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error_reporting'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'datei.txt'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.php.net/ini_set"><span style="color: #990000;">ini_set</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error_reporting'</span><span style="color: #339933;">,</span><span style="color: #000088;">$old</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div><br /><br />Verstehen Sie, was dort passiert? Stellen Sie sich vor, dass Sie den @-Operator in einem Skript fünf mal verwenden, erzeugen Sie für den Interpreter somit 15 Zeilen mehr Code, abgesehen von der Mehrarbeit, die im Kern hierdurch passiert. Performante, professionelle Programmierer programmieren in einer Entwicklungsumgebung mit error_reporting(E_STRICT); und merzen sämtlich auftretende Fehler durch Ausnahmen oder Fehlerroutinen aus. Dem sollten Sie sich anschließen und den @-Opterator so nicht mehr benutzen und anstelle hierfür eine passende Fehlerroutine schreiben.<br /><br />Ebenfalls unperformant sind unnötige Zeichenersetzungen und Aufrufe von str_replace, obwohl Sie nicht mal im Code 100%ig wissen, ob dies im String notwendig ist. Zeichenersetzungen (str_replace ist schon wesentlich schneller als preg_replace, aber dennoch teuer) kosten sehr viel Performance und sollten auch nur vorgenommen werden, wenn Sie tatsächlich vonnöten sind. <br /><br />Prüfen Sie also zuvor, ob überhaupt Ersetzungen notwendig sind:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ein Text&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/strpos"><span style="color: #990000;">strpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Ein'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ein'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Kein'</span><span style="color: #339933;">,</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div><br /><br />Ebensolche gilt insbesondere bei der Verwendung von preg_replace(), welches erheblichen Performanceverlust bedeutet. Setzen Sie preg_replace() nur ein, wenn es absolut notwendig ist und verwenden Sie stattdessen lieber die PHP-Funktionen.<br /><br />Ein paar Beispiele:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^stringelchen_/i'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'String'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// viel schneller:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/strncasecmp"><span style="color: #990000;">strncasecmp</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'stringelchen_'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'String'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[abc]/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Ein langer Text'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// schneller</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/strpbrk"><span style="color: #990000;">strpbrk</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'abc'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Ein langer Text'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/string/i'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Text'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// schneller</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/stripos"><span style="color: #990000;">stripos</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Text'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'string'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\n/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// viel schneller</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div><br /><br />(sj)]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Wed, 03 Nov 2010 17:48:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title>Angepasste PHP-Version unter Ubuntu selbst kompilieren</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/angepasste-php-version-unter-ubuntu-selbst-kompilieren.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/angepasste-php-version-unter-ubuntu-selbst-kompilieren.html</guid>
			<description>PHP5.3-cli mit eAccelerator,  angepassten SOMAXCONN, MySQL und pcntl unter Ubuntu 10.4 kompilieren.</description>
			<content:encoded><![CDATA[In fasst allen Fällen fährt ein Administrator am besten, wenn er alle Programme fertig aus den Paketquellen der verwendeten Linux Distribution holt. Die Pakete der Distribution arbeiten in der Regel Fehlerfrei mit anderen Programmen aus der gleichen Quelle zusammen und werden zuverlässig mit Sicherheitsupdates versorgt.<br /><br />Einige Anforderungen lassen sich mit den fertigen Paketen aber nicht lösen. So ist zum Beispiel die Anzahl der maximalen Socket-Verbindungen die PHP unterstützt fest in das Programm einkompiliert. Bei vielen gängigen Distributionen sind dies wie beim aktuellen Ubuntu 10.04 maximal 128 Verbindungen. Wer mehr benötigt, muss PHP neu aus den Quellen übersetzen.<br /><br />Das ist mit wenigen Schritten möglich:<br /><br />
<h2>1)Maximale Sockets erhöhen:</h2>
<div id="tx_codehighlight_pi1"><div class="code"><pre class="bash" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #c20cb9; font-weight: bold;">vi</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>include<span style="color: #000000; font-weight: bold;">/</span>bits<span style="color: #000000; font-weight: bold;">/</span>socket.h</div></li></ol></pre></div></div><br /><br />Die Zeile <br /><i>#define SOMAXCONN&nbsp;&nbsp; 128</i><br /><br />ändern in<br /><i>#define SOMAXCONN&nbsp;&nbsp; 2048</i><br /><br />
<h2>2)PHP-Quellcode herunterladen</h2>
<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="bash" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>opt</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #000000; font-weight: bold;">/</span>de.php.net<span style="color: #000000; font-weight: bold;">/</span>get<span style="color: #000000; font-weight: bold;">/</span>php-5.3.3.tar.bz2<span style="color: #000000; font-weight: bold;">/</span>from<span style="color: #000000; font-weight: bold;">/</span>this<span style="color: #000000; font-weight: bold;">/</span>mirror \</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #660033;">-O</span> php-5.3.3.tar.bz2</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #c20cb9; font-weight: bold;">tar</span> xfv php-5.3.3.tar.bz2</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">php-5.3.3</div></li></ol></pre></div></div><br /><br />
<h2>3)Benötigete Pakete installieren</h2>
<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="bash" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libxml2-dev libmysqlclient-dev build-essential php5-dev</div></li></ol></pre></div></div><br /><br />php Compilieren<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="bash" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--prefix</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>php5 <span style="color: #660033;">--with-config-file-path</span>=<span style="color: #000000; font-weight: bold;">/</span>etc \</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>cli <span style="color: #660033;">--enable-sockets</span> <span style="color: #660033;">--enable-pcntl</span> <span style="color: #660033;">--with-mysql</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mysql</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #c20cb9; font-weight: bold;">make</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></div></li></ol></pre></div></div><br /><br />Viele php-Anwendungen lassen sich durch opt-Code Optimierer stark beschleunigen. Auf diese Möglichkeit wollen wir auch bei unserer selbst kompilieren Variante nicht verzichten. <br /><br />
<h2>4)eAccelerator installieren</h2>
<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="bash" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>opt</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #c20cb9; font-weight: bold;">wget</span> bart.eaccelerator.net<span style="color: #000000; font-weight: bold;">/</span>source<span style="color: #000000; font-weight: bold;">/</span>0.9.6.1<span style="color: #000000; font-weight: bold;">/</span>eaccelerator-0.9.6.1.tar.bz2</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #c20cb9; font-weight: bold;">tar</span> xfv eaccelerator-0.9.6.1.tar.bz2</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>eAccelerator</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">phpize</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--enable-eaccelerator</span>=shared <span style="color: #660033;">--with-php-config</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>php-config</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #c20cb9; font-weight: bold;">make</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></div></li></ol></pre></div></div><br /><br />
<h2>5)PHP configurieren</h2>
<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="bash" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #c20cb9; font-weight: bold;">vi</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>cli<span style="color: #000000; font-weight: bold;">/</span>php.ini</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #007800;">extension</span>=<span style="color: #ff0000;">&quot;eaccelerator.so&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.shm_size=<span style="color: #ff0000;">&quot;16&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.cache_dir=<span style="color: #ff0000;">&quot;/data/cache&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.enable=<span style="color: #ff0000;">&quot;1&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.optimizer=<span style="color: #ff0000;">&quot;1&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.check_mtime=<span style="color: #ff0000;">&quot;1&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.debug=<span style="color: #ff0000;">&quot;0&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.shm_max=<span style="color: #ff0000;">&quot;0&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.shm_ttl=<span style="color: #ff0000;">&quot;0&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.shm_prune_period=<span style="color: #ff0000;">&quot;0&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.shm_only=<span style="color: #ff0000;">&quot;0&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.compress=<span style="color: #ff0000;">&quot;1&quot;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">eaccelerator.compress_level=<span style="color: #ff0000;">&quot;9&quot;</span></div></li></ol></pre></div></div>
<h2>Ergebnis testen</h2>
<br />Die Konfiguration lässt sich leicht testen, indem man ein PHP-Script mit dem frisch kompilierten php startet.<br /><br />Ein einfaches echo SOMAXCONN; sollte jetzt 2048 ausliefern.<br /><br />Zusätzlich wollen wir noch testen ob der eAccelerator auch läuft.<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #666666; font-style: italic;">#test.php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$time_start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">1000000</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$k</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><a href="http://www.php.net/asin"><span style="color: #990000;">asin</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/rand"><span style="color: #990000;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$j</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><a href="http://www.php.net/asin"><span style="color: #990000;">asin</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/rand"><span style="color: #990000;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;">&#125;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <a href="http://www.php.net/asort"><span style="color: #990000;">asort</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <a href="http://www.php.net/asort"><span style="color: #990000;">asort</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <a href="http://www.php.net/array_multisort"><span style="color: #990000;">array_multisort</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #339933;">,</span> <span style="color: #000088;">$j</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <a href="http://www.php.net/array_multisort"><span style="color: #990000;">array_multisort</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span><span style="color: #339933;">,</span> <span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$time_end</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$time_end</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$time_start</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Did nothing in <span style="color: #006699; font-weight: bold;">$time</span> seconds<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></pre></div></div><br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="bash" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>php test.php</div></li></ol></pre></div></div><br />liefert:<br />Did nothing in 11.94739484787 seconds<br /><br />Im Vergleich mit dem php5 aus den Paketen kommt das Ergebnis<br />Did nothing in 13.61189198494 seconds<br /><br />Selbst dieses einfache Script ist durch den eAccelerator um ca. 14% schneller. Ob der eAcceraltor wirklich läuft sieht man auch, wenn im Cache-Verzeichnis (in unserem Fall /data/cache wirklich Daten abgelegt werden.<br /><br />]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Fri, 29 Oct 2010 15:02:00 +0200</pubDate>
			
		</item>
		
		<item>
			<title>PHP Perfomance &amp; Security: Teil 3</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/php-perfomance-security-teil-3-grundsaetzliche-security-funktionen.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/php-perfomance-security-teil-3-grundsaetzliche-security-funktionen.html</guid>
			<description>Grundsätzliche Security-Funktionen</description>
			<content:encoded><![CDATA[In den ersten beiden Teilen zu PHP Perfomance &amp; Security behandelte ich grundsätzliche Vorgaben, die man grundsätzlich bei der Programmierung in PHP umsetzen sollte, um möglichst performant und sicherheitsbewußt zu programmieren. Dabei wurde der Sicherheitsaspekt lediglich kurz angerissen und in diesem Artikel geht es ausschließlich um die Sicherheit beim Programmieren mit PHP. Denn ob eine Anwendung sicher oder nicht ist, unterliegt dem Programmierer und nicht der ausführbaren Skriptsprache!<br /><br />Auch wenn es eigentlich in jedem Tutorial zu lesen ist, möchte ich es ebenfalls wiederholen, dass sämtliche Werte die man in Skripten einliest, zu prüfen sind. Vergessen wird oft, dass nicht nur Werte aus der globalen $_GET und $_POST oder gar $_REQUEST zu prüfen gehören, sondern ebenfalls Werte aus $_SERVER, $_COOKIE, $_SESSION und allen anderen Variablen, die irgendwo hergeholt werden und für die jeweilige Funktion oder Klasse unbekannt sein könnten.<br /><br />Um höchst sicherheitsbewußt zu programmieren, sollte man bestenfalls nach dem Whitelabel-Prinzip prüfen, zumindest aber nach dem Blacklabel-Prinzip. Mit relativ simpler Methodik kann man eine solche Prüfung zentral für alle Skripte umsetzen. Hierzu bietet PHP seit Version 5.2 eine Extension namens ext/filter, mittels den man Variablen im Vorfeld filtern kann. Ausserdem kann und sollte man auch eigene Validatoren einsetzen, die z.B. Formularwerte validiert und entsprechende Rückmeldung zurückgibt. Hierzu nun einige Beispiele.<br /><br />Filtern mit ext/filter<br />Mittels der Extension ext/filter kann man den Inhalt von Variablen auf Datentypen sowie aber auch E-Mail-Adressen und URLs im Vorfeld prüfen. ext/filter stellt hierfür bereits einige vordefinierte Filter zur Verfügung, um Variablen zu prüfen. Übergibt man beispielsweise in einem Skript eine ID (z.B. eine ArtikelID), wird diese voraussichtlich stets ein Integerwert sein oder aber auch eine Zusammensetzung von ID und einem String. Selbstverständlich kann man dies auch mittels RegEx prüfen, was allerdings sehr unperformant ist (insbesondere die Perlkompatiblen Prüfmethoden wie preg_match, etc.). ext/filter stellt hierfür einen performanten und simplen Filter zur Verfügung, der auf alphanumerische Zeichen prüft und dabei die hohen und/oder tieferen ASCII-Codes herausfiltert:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Tiefen ASCII-Zeichensatz herausfiltern</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span>FILTER_SANITIZE_STRING<span style="color: #339933;">,</span>FILTER_FLAG_STRIP_LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Hohen ASCII-Zeichensatz herausfiltern</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span>FILTER_SANITIZE_STRING<span style="color: #339933;">,</span>FILTER_FLAG_STRIP_HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Nur alphanumerische Zeichen aus dem ASCII-Zeichensatz zulassen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span>FILTER_SANITIZE_STRING<span style="color: #339933;">,</span>FILTER_FLAG_STRIP_LOW </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339933;">|</span> FILTER_FLAG_STRIP_HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Zeichenlänge des Strings von 3 bis 12 Zeichen festlegen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">'min_range'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">'max_range'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">,</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #0000ff;">'flags'</span> <span style="color: #339933;">=&gt;</span> FILTER_FLAG_STRIP_LOW <span style="color: #339933;">|</span> FILTER_FLAG_STRIP_HIGH</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span>FILTER_SANITIZE_STRING<span style="color: #339933;">,</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div><br /><br />Mehr Informationen zu ext/filter findet Ihr in dem PHP-Manual unter http://www.php.net/manual/de/book.filter.php<br /><br />Prüfen mittels White- oder mindestens Blacklist<br />Um Variablen mittels einer Blacklist oder noch besser einer Whitelist zu validieren, ist die beste Methode eine eigene Validierungsklasse hierfür zu programmieren, die entsprechende Listen inkludiert oder als feste Klassenvariablen (Arrays) beinhaltet und mittels der Methoden die übergebenen Variablen prüft. Eine solche Klasse könnte folgendermaßen aussehen:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">class</span> validate</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009933; font-style: italic;">/**</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * Prüft den übergebenen String an einer definierten</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * übergebenen Whitelist</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * @param string $str</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * @param array $whitelist   in Form array[erlaubt,erlaubt2,...]</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * @access public</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * @static</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * @return boolean</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> whiteString<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #000088;">$whitelist</span><span style="color: #009900;">&#41;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">return</span> <a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #000088;">$whitelist</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009933; font-style: italic;">/**</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * Prüft den übergebenen String an einer definierten</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * übergebenen Blacklist</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * @param string $str</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * @param array $blacklist    in Form array['blacklist','blacklist2',...]</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * @access public</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * @static</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   * @return boolean</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">   */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> blackString<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #000088;">$blacklist</span><span style="color: #009900;">&#41;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">return</span> <a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #000088;">$blacklist</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Whitelist Aufruf der Klasse innerhalb eines scriptes</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$whitelist</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pruefenderWert'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'auch-erlaubt'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>validate<span style="color: #339933;">::</span><span style="color: #004000;">whiteString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pruefenderWert'</span><span style="color: #339933;">,</span><span style="color: #000088;">$whitelist</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Alles ok, valide.&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;String ist nicht valide&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">// Blacklist Aufruf der Klasse innerhalb eines Scriptes</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$blacklist</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pruefenderWert'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'verboten2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>validate<span style="color: #339933;">::</span><span style="color: #004000;">blackString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pruefenderWert'</span><span style="color: #339933;">,</span><span style="color: #000088;">$blacklist</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Der Wert ist blacklistet&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Ok, Wert ist nicht blacklistet.&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li></ol></pre></div></div><br /><br />Das Zend-Framework bietet eine Reihe an fertigen Validatoren für die unterschiedlichsten Werte, darunter auch E-Mail-Adressen, IPs sowie natürlich die Standard-Validatoren für Strings, Integer, Floats, etc. Im PEAR-Framework existiert ebenfalls ein Package namens Validate, welches auch Sub-Packages für verschiedene Länder-Validatoren (PLZ, etc) anbietet. Leider geht die Entwicklung da nicht ganz so fix voran, allerdings lassen sich mit den Validator-Klassen der Frameworks sehr schnell auch eigene Validatoren erzeugen.<br /><br />Alle Eingaben mittels Filtern prüfen<br />Mit dem Wissen, wie man zuverlässig Werte filtern und validieren kann, sollte man nun bereits bei der Initialisierung des Systems eine globale Funktion zur Verfügung stellen, die man in jedem PHP-Skript aufrufen und Werte von &quot;außen&quot; gefiltert holen kann. Eine solche Funktion könnte folgendermaßen aussehen:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;">/**</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;"> * Globale Funktion, die in jedem Skript aufgerufen werden sollte, um Werte von</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;"> * Aussen gefiltert  ins Skope zu holen. Existiert in keinem der Globalen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;"> * der Parameter, wird dieser ungeprüft zurückgegeben.</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;"> *</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;"> * @param mixed $str         Zu prüfender Parameter</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;"> * @param string $type       Auf welchen Type soll geprüft werden</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;"> * @param array $options   Mögliche Optionen als Array übergeben</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;"> * @return mixed</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009933; font-style: italic;"> */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">function</span> getParam<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #000088;">$type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">// $value existierte in den Globalen nicht, also Wert zurückgeben</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">// Nach übergebenen Typ filtern/prüfen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'integer'</span><span style="color: #339933;">:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span>FILTER_SANITIZE_INT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'string'</span><span style="color: #339933;">:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'htmlentities'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">                     <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'htmlentities'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ?</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">                     FILTER_SANITIZE_SPECIAL_CHARS <span style="color: #339933;">:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">                     FILTER_SANITIZE_STRING<span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'min_range'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/is_int"><span style="color: #990000;">is_int</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'min_range'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #000088;">$opt</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'min_range'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'min_range'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'max_range'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/is_int"><span style="color: #990000;">is_int</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'max_range'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #000088;">$opt</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'max_range'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'max_range'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'alphanumeric'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'alphanumeric'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #000088;">$opt</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flags'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> FILTER_FLAG_STRIP_LOW <span style="color: #339933;">|</span> FILTER_FLAG_STRIP_HIGH<span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'denyLow'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'denyLow'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #000088;">$opt</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flags'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> FILTER_FLAG_STRIP_LOW<span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'denyHigh'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'denyHigh'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #000088;">$opt</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flags'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> FILTER_FLAG_STRIP_HIGH<span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #000088;">$filter</span><span style="color: #339933;">,</span><span style="color: #000088;">$opt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'float'</span><span style="color: #339933;">:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'allowSeparators'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'allowSeparators'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #000088;">$opt</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flags'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> FILTER_FLAG_ALLOW_THOUSEND<span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span>FILTER_SANITIZE_NUMBER_FLOAT<span style="color: #339933;">,</span><span style="color: #000088;">$opt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$allowed</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'allowTags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ?  <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'allowTags'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_bool"><span style="color: #990000;">is_bool</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <a href="http://www.php.net/is_resource"><span style="color: #990000;">is_resource</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <a href="http://www.php.net/is_object"><span style="color: #990000;">is_object</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> </div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #339933;">||</span> <a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ?</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">                   <span style="color: #000088;">$value</span> <span style="color: #339933;">:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">                   <a href="http://www.php.net/strip_tags"><span style="color: #990000;">strip_tags</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #000088;">$allowedTags</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'htmlentities'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">                   <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'htmlentities'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> ? <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div><br /><br />In den Quellcodes sind einige Umbrüche wegen der Lesbarkeit enthalten, dennoch sollte das Skript auch mit den Umbrüchen funktionieren. Selbstverständlich kann man dieses Skript auf White- und/oder Blacklist Validatoren erweitern. Eine Möglichkeit wäre eine globale statische Klasse, die das einlesen der Globalen und sämtliche Prüfungen, Filterungen und Validatoren beinhaltet und über statische Methoden generell vornimmt.<br /><br />(sj)]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Thu, 28 Oct 2010 19:05:00 +0200</pubDate>
			
		</item>
		
		<item>
			<title>Alexa Pagerank über PHP abfragen</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/alexa-pagerank-ueber-php-abfragen.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/alexa-pagerank-ueber-php-abfragen.html</guid>
			<description>Einfache Klasse um Seiteninformationen von alexa.com in eigenen Scripten zu verarbeiten.</description>
			<content:encoded><![CDATA[Die Seite alexa.com bietet die Möglichkeit einige Informationen zu eigenen und fremden Webseiten abzufragen. Am bekanntesten dürfte wohl der Alexa Pagerank sein, der versucht alle Webseiten der Welt in eine Reihenfolge ihrer Besucherzahlen zu bringen.<br />Wie genau der Wert von Alexa ist, lässt sich schwer sagen. Für jeden Webmaster ist es aber ein nützliches Indiz um Seiten zu vergleichen.<br /><br />Im Internet haben wir recht viele Klassen und Scripte gefunden, die es ermöglichen über PHP Seiteninformationen von Alexa zu holen. Viele Ansätze fanden wir aber entweder zu kompliziert oder nicht zuverlässig genug.
Da Alexa vorbildlich eine XML-Datei ausliefert, bietet sich an die Funktionen CURL und simplexml für die Abfrage zu verwenden. Wir haben uns daraus selbst eine kleine Klasse gebaut.<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">class</span> alexa<span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$domain</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">RequestUrl</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'http://data.alexa.com/data?cli=10&amp;dat=s&amp;url='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$domain</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">RequestUrl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> requestPagerank<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$pagerank</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SD</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">POPULARITY</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;TEXT&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pagerank</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> requestSpeed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #000088;">$pagerank</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SD</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SPEED</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;TEXT&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pagerank</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></pre></div></div><br /><br />Ein einfaches Beispiel zeigt, wie sich die Klasse verwenden lässt:<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;class.alexa.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$siteinfo</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> alexa<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;www.lamp-solutions.de&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Pagerank: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$siteinfo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">requestPagerank</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Speed: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$siteinfo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">requestSpeed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></pre></div></div><br /><br />Dieser recht einfache Code ist für uns ausreichend die wichtigsten Informationen abzufragen. Es lässt sich einfach erweitern, um weitere Informationen aus der XML-Datei zu holen. Wie immer freuen wir uns über Anmerkungen und Verbesserungsvorschläge. (te)]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Thu, 14 Oct 2010 15:56:00 +0200</pubDate>
			<enclosure url="http://foobar.lamp-solutions.de/uploads/media/class.alexa.zip" length ="504" type="application/zip" />
		</item>
		
		<item>
			<title>PHP-Performance &amp; Security - Teil 2</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/php-performance-security-teil-2-zentrale-include-datei.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/php-performance-security-teil-2-zentrale-include-datei.html</guid>
			<description>Geschwindigkeit von PHP Scripten durch zentrale Include-Datei optimieren</description>
			<content:encoded><![CDATA[In diesem 2. Teil von PHP-Perfomance &amp; Security geht es darum, die Performance des PHP-Interpretors durch eine zentrale Include-Datei und das inkludieren aller benötigten Skripte zu erhöhen und damit auch den Interpretor zu entlasten.<br /><br />In vielen Anwendungen werden Funktionsdateien mit eine Vielzahl von Funktionen oder gar Klassendateien zentral für die Anwendung benutzt. Bei objektorientierten Anwendungen wird nicht selten sogar ein Autoloader verwendet, der benötigte Klassen automatisch nachlädt und instanziert, und das meistens mit der Voraussetzung, dass im Include-Path die passenden Verzeichnisse benannt werden.<br /><br />In diesem Artikel zeige ich zwei Varianten auf, wie man einmal über eine zentrale Include-Datei, die in jedem PHP-Skript an oberster Stelle schlicht direkt inkludiert wird alle benötigten zentralen Funktionsdateien und/oder Klassendateien mit vollen Pfadangaben auf Wunsch inkludiert und wie man mittels einer eigenen Autoload-Klasse mit direkten Pfadangaben Klassen dynamisch instanzieren kann, ohne die benötigte Klasse vorher direkt zu inkludieren.<br /><br /><b>Die zentrale Include-Datei</b><br />Als Beispiel gehen wir davon aus, dass wir eine ordentliche Verzeichnisstruktur durchgehend verwenden und im Verzeichnis &quot;includes&quot; bzw. Unterordner direkt oberhalb des Document-Roots alle benötigten PHP-Skripte ablegen, die zentrale Funktionen für die gesamte Website zur Verfügung stellt und inkludiert werden sollen. Zusätzlich können via Parameter bestimmte Includes / Instanzierungen auf Wunsch nur geladen werden.<br /><br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* Datei /includes/setup.php */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* Da diese zentrale Include-Datei im Verzeichnis /includes liegt, setzen</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"> * wir auch dieses als INCLUDEDIR, in dem beispielsweise Funktionsdateien </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"> * im Unterverzeichnis /includes/functions abgelegt werden.</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"> */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INCLUDEDIR&quot;</span><span style="color: #339933;">,</span><a href="http://www.php.net/dirname"><span style="color: #990000;">dirname</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* In der Regel benutzt man bei allen Skripten auch eine zentrale </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"> * Konfiguration, die wir hier im gleichen Verzeichnis vermuten und </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"> * inkludieren:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"> */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span>INCLUDEDIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* Im nächsten Abschnitt, inkludieren wir mit direkten Pfadangaben direkt </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"> * alle benötigten Funktionsdateien, sofern diese geladen werden sollen:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"> */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span>INCLUDEDIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/functions/global.functions.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$load</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'template'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span>INCLUDEDIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/functions/template.functions.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$load</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mysql'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span>INCLUDEDIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/classes/mysql.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/class_exists"><span style="color: #990000;">class_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Mysql'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">/* Da ich grundsätzlich MySQL-Connections mittels einer statischen Singleton-</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">   * Klasse vornehme, habe ich dieses Beispiel auch benutzt. Diese Klasse </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">   * instanzieren wir auch (die Klasse ist hier nicht von Belang, nur soviel, </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">   * dass das Objekt lediglich instanziert aber keine Verbindung hergestellt </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">   * wird).</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">   */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql"><span style="color: #990000;">Mysql</span></a><span style="color: #339933;">::</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dsn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* und weiteres, falls notwendig */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li></ol></pre></div></div><br /><br />Die genannte zentrale Konfigurationsdatei enthält die Konfiguration des Systems und könnte folgendermaßen aussehen:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* Datenbank-DSN */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$dsn</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'host'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$dsn</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'dbusername'</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$dsn</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'dbpassword'</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$dsn</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dbname'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'dbname'</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div><br /><br />In den PHP - Skripten des Projektes wird im Anschluss lediglich diese zentrale Include-Datei inkludiert, die alles benötigte bzw. gewünschte des Skriptes dann lädt:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$load</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'template'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$load</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mysql'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'DOCUMENT_ROOT'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/includes/setup.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div><br /><br /><b>Per Autoload Klassen laden</b><br />Zusätzlich kann und sollte man den Autoloader verwenden, um Klassen dynamisch nachladen zu können, sofern man objektorientiert programmiert. Ein passender Autoloader, der ebenfalls in der zentralen Include-Datei /includes/setup.php seinen Platz findet, könnte folgendermaßen aussehen:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* In der Datei /includes/setup.php an folgender Stelle hinzufügen: */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span>INCLUDEDIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">/* Variable prüfen und säubern */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/ucfirst"><span style="color: #990000;">ucfirst</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strip_tags"><span style="color: #990000;">strip_tags</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">/* Ich nehme hier an, dass alle Klassen im Ordner /includes/classes liegen </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">   * und nach dem Schema klassenname.class.php benannt wurde.</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">   */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span>INCLUDEDIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/classes/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span>INCLUDEDIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/classes/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* In dem Fall eines Autoloaders, brauchen wir bei der Instanzierung des </span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"> * MySQL-Singletons keinen Include mehr:</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"> */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$load</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mysql'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/class_exists"><span style="color: #990000;">class_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Mysql'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <a href="http://www.php.net/trigger_error"><span style="color: #990000;">trigger_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Konnte mysql-Singleton nicht laden, da Klasse nicht bekannt'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">E_USER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #009900;">&#125;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql"><span style="color: #990000;">Mysql</span></a><span style="color: #339933;">::</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dsn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div><br /><br />Mittels dieser Methodik muss der Interpreter durch die Angabe eines kompletten Pfades in jedem include (oder include_once, require, require_once) keine Verzeichnisse mehr durchsuchen, sondern versucht direkt die Datei zu laden. Gelingt das dem Interpreter nicht, wird eine Fehlermeldung ausgegeben.<br /><br />Selbstverständlich kann und sollte man auch bei der Namenskonvention der Klassen stets gleich bleiben. Das heißt, dass man Dateien mit Klassen grundsätzlich klassenname.class.php benennt, Dateien mit Funktionen  dateiname.functions.php. Somit weiß man beim include auch immer schon, um was für eine Datei es sich handelt. Klassen sollten zudem immer so benannt werden, wie auch der Name im Dateiname ist und der erste Buchstabe des Namens sollte Groß geschrieben sein. Bespielweise sieht der MySQL-Singleton mit dem Dateinamen mysql.class.php folgendermaßen aus:<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">class</span> <a href="http://www.php.net/mysql"><span style="color: #990000;">Mysql</span></a></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* Klassendefinition */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div>
(sj)]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Wed, 13 Oct 2010 16:45:00 +0200</pubDate>
			
		</item>
		
		<item>
			<title>PHP - Performance &amp; Security - Teil 1</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/php-performance-security-teil-1-php-korrekt-einstellen.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/php-performance-security-teil-1-php-korrekt-einstellen.html</guid>
			<description>PHP Umgebungsvariablen auf optimale Sicherheit und Geschwindigkeit optimieren.</description>
			<content:encoded><![CDATA[Je nach dem, wie PHP auf dem Webserver läuft, kann man PHP auch zu jedem virtuellem Host spezifisch einstellen. Läuft PHP als Apache-Modul kann man mittels php_admin_value in der vHost-Datei spezifische Einstellungen vornehmen. Läuft PHP als CGI-Modul, was Sicherheitstechnisch sicherlich die effektivste Lösung ist aber auch die Performance verschlechtert, kann man PHP mittels der eigenen php.ini jedes virtuellen Hosts einstellen.
Grundsätzlich sollten einige Grund-Einstellungen in PHP vorgenommen werden, sofern dies nicht schon durch den Hoster bereits eingestellt wurde.
<b>open_basedir</b>
Diese Einstellung sollte grundsätzlich für jeden vHost auf das Verzeichnis des vHosts eingestellt sein. Sicher kann open_basedir mittels Funktionen wie system(), exec(), etc. umgangen werden, stellt aber dennoch schonmal eine wesentlich Sicherheitsmaßnahme für &quot;normale&quot; Anwendungen dar. Liegt der DocumentRoot des vHosts unter /var/www/username/htdocs sollte der open_basedir auf /var/www/username gesetzt werden. Diese Einstellungen sollte nur in der php.ini oder im vHost-Block gesetzt werden, obgleich es auch möglich ist, in einer htaccess zu setzen.
<b>upload_tmp_dir</b>
Ist der open_basedir gesetzt, sollte auch dieser Parameter unterhalb des Verzeichnisses gesetzt werden, damit bei möglichen Uploads eines Scriptes auch die notwendigen Rechte vorhanden sind, Dateien in upload_tmp_dir zwischen zu speichern. Diese Einstellungen kann in der php.ini, im vHost-Block oder je nach Erlaubnis in der htaccess oder im Skript selbst gesetzt werden.
<b>disable_functions</b>
Diese Einstellung ist obligatorisch für jeden Hoster und sollte Funktionen wie system, exec, passthru, shell_exec, posix_setuid, pcntl_exec verbieten, so dass diese PHP-Funktionen aus Webscripten nicht aufgerufen werden können. Ohnehin ist es für die Performance besser, ein CLI-Script notfalls einzusetzen und mittels PHP-Funktionen seine Abarbeitungen vorzunehmen, als bequemerweise irgendwelche Shelltools oder andere Anwendungen darüber zu starten. Diese Funktion kann nur in der php.ini global gesetzt werden.
<b>include_path</b>
Die Einstellung include_path gibt den Interpreter die Verzeichnisse vor, wo nach inkludierten Dateien mittels include, require, include_once und require_once gesucht werden soll. Je mehr Verzeichnisse mittels Pfad-Separator (unter Unix : und unter Windows ;) angegeben wurden, desto mehr Verzeichnisse werden vom Interpreter durchsucht. Ich empfehle, diese Einstellung im vHost-Block mittels<br /><br />
<div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">php_value include_path <span style="color: #0000ff;">&quot;/var/www/username&quot;</span></div></li></ol></pre></div></div><br /><br />
lediglich auf den User-Root des vHosts zu setzen und falls man ein zentrales PEAR-Verezichnis anbieten möchte, diesen noch im Anschluss anzusetzen:<br /><br />
<div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">php_value incude_path <span style="color: #0000ff;">&quot;/var/www/username:/usr/share/php5&quot;</span></div></li></ol></pre></div></div><br /><br />
<div>Grundsätzlich sollte man wie so oft und gern vorgenommen den Punkt &quot;.:&quot; nicht als erstes im include_path setzen. Der Punkt deutet auf das Verzeichnisroot des Users und durchsucht quasi bei Angabe von Unterverzeichnissen jedes mal doppelt. Der Interpreter durchsucht jedes mal, wenn ein include, incluce_once, require oder require_once im Script vorkommt alle dort angegebene Verzeichnisse nach der zu inkludieren Datei und das bedeutet verschenkte Performance.
Im nächsten Artikel &quot;PHP Performance &amp; Security - Teil 2 zentrale Include-Datei&quot; gehe ich auf diese Einstellung nochmals genauer ein.
<b>register_globals = Off</b>
Eigentlich nur der Vollständigkeit halber erwähnt, sollte diese Einstellung stets auf Off stehen (was standardmäßig seit PHP-4.2.0 ausgeschaltet ist. Aus Sicherheitsgründen sollte man sowieso wenn überhaupt nur die globalen Variablen direkt ansprechen und selbst diese mit Vorsicht genießen.
<b>mysql_pconnect = Off</b>
Es gibt wohl nur sehr, sehr wenige PHP - Anwendungen, die persistente MySQL-Verbindungen benötigen und programmiert man performant, sollte jede Verbindung nach Abarbeitung seines Codes sowieso die Verbindung wieder schließen. Um also von Grund auf performant zu arbeiten, sollte mysql_pconnect grundsätzlich ausgeschaltet sein.
<div></div>
<div><b>safe_mode</b></div>
<div>Der safe_mode wird oftmals sehr verteufelt. In verschiedenen PHP-Anwendungen, bei denen z.B. keine Uploads vorgenommen werden bzw. von den Skripten direkt keine Datei angelegt oder verändert wird, sollte allerdings tatsächlich darüber nachgedacht werden, den safe_mode im jeweiligen vHost-Block einzuschalten. Dieser Sicherheitsmodus führt bei jeder Aktion eine Zugehörigkeitsprüfung durch, was bedeutet, dass wenn ein Skript auf eine Datei zugreifen möchte, welche nicht dem gleichen Benutzer oder der gleichen Gruppe gehören, abgewiesen wird. Was die Sicherheit einer Anwendung betrifft, ist der Safe-Mode sicherlich die sicherste Einstellung in PHP. Sollten allerdings Uploads möglich sein und verwendet man PHP als Apache-Modul, wird der Safe-Mode hinderlich, denn von Skripten initiierte Uploads gehören dann den Webserver und nicht mehr häufigst den Benutzer der PHP-Skripte, die meistens mittels FTP-User auf den Server geladen werden. Der Safe-Mode verhindert dann den Zugriff durch ein Skript auf die hochgeladene Datei, weshalb diese dann nicht mehr bearbeitbar ist.</div>
<div></div>
<div><b>session.auto_start=Off</b></div>
<div>Sessions werden in PHP-Anwendungen sehr häufig gebraucht und wer sauber programmiert, initiiert die Session mittels session_start() vor jeglicher Ausgabe eines Skriptes. Folglich sollte die automatische Anlage einer Session durch den PHP-Interpreter bei grundsätzlich jedem Aufruf ausgeschaltet werden, womit Performance eingespart werden kann.</div>
<div></div>
<div><b>session.gc_divisor</b></div>
<div>Diese Einstellung legt eine Wahrscheinlichkeit fest, wie oft der Garbage-Collector (PHP's Müllmann) für die gespeicherten Sessions aktiviert wird. Die Einstellung session.gc_probability wirkt sich in Kombination mit dieser Einstellung aus. Der Standardwert von session.gc_divisor ist 100 und der von session.gc_probability = 1 (sprich mit einer Wahrscheinlichkeit von 0,01), was bedeutet, dass der Interpreter bei jedem 1% den Garbage Collector aufruft. Bei 100 Request folglich wird einmal der Garbage Collector aufgerufen, nur sind bei stark frequentierten Seiten 100 PHP-Requests in der Sekunde sicherlich nicht selten und wer möchte schon jede Sekunde den Müllmann beherzigen, was auch wieder erhebliche Performance kostet. Die Einstellung session.gc_divisor kann man getrost auch auf Werten bei 10000 oder 100000 setzen.</div>
<div></div>
<div><b>magic_quotes_gpc=Off</b></div>
<div>Diese PHP-Einstellung maskiert in GET, POST und REQUEST-Variablen automatisch bestimmte, enthaltene Zeichen durch einen Backslash. Wer sich aber lediglich auf dieses Feature verläßt, ist sehr naiv. Jeder PHP-Programmierer sollte jede mögliche Variable, die von Außen kommt, sowie so durch eigene Routinen und Funktionen, bestenfalls sogar lediglich mit Whitelists prüfen und eventuelle Codezeichen maskieren. Dieses Feature verbessert die Sicherheit lediglich auf einem sehr schwachen Niveau und kostet erhebliche Performance. Sofern Sie keine Fremdskripte auf den Server einsetzen und nur eigene programmierte Skripte, sollte dieses Feature ausgeschaltet werden und eine eigene, viel effizientere und sichere Prüfung vorgenommen werden, die sicherlich nicht soviel Performance kosten wird, wie diese Einstellung.
</div>
<div></div>
<div>Durch die häufigste Verwendung von PHP als Apache-Modul sollte man zumindest diese Einstellungen in jedem virtuellen Host korrekt vornehmen und mittels htaccess das überschreiben einiger Einstellungen mit AllowOverride None verbieten. Somit ist zumindest etwas an Sicherheit und Performance gewonnen, abgesehen von verschiedenen Performance-Optimierungen des Webservers, die nochmals einen Schub bringen können. Denken Sie auch daran, dass jede htaccess im Dokumentroot der Apache auslesen und abarbeiten muss, auch wenn nichts darin steht. Schon deshalb sollten diese Einstellungen direkt in der vHost.conf vorgenommen werden und mit so wenig htaccess Dateien wie nur möglich gearbeitet werden.</div>
<div></div>
<div>Im kommenden Artikel beschreibe ich, wie man mittels einer zentralen Include-Datei, die beispielsweise über eine config.php in jeder Skriptdatei mittels direkter Pfadangabe inkludiert wird und jedes zentral benötigte Skript (z.B. Klassen, Funktionsdateien, etc.) standardmäßig mittels voller Pfadangaben einbindet,&nbsp;dem Interpreter die Zusatzarbeit des ständigen Pfaddurchlaus bei der Suche nach inkludierenden Dateien erspart.
(sj)</div></div>]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Thu, 07 Oct 2010 09:38:00 +0200</pubDate>
			
		</item>
		
		<item>
			<title>PHP-Performance &amp; Security - Einleitung</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/php-performance-security-einleitung.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/php-performance-security-einleitung.html</guid>
			<description>PHP ist einer der beliebtesten und weit verbreitesten Skriptsprachen, die für dynamische Websites...</description>
			<content:encoded><![CDATA[<div>PHP ist einer der beliebtesten und weit verbreitesten Skriptsprachen, die für dynamische Websites wie auch Enterprise-Anwendungen genutzt wird. Der Einstieg in die Programmierung mit PHP ist im Gegensatz zu anderen Skriptsprachen wie Perl, ASP, Python oder JSP weitaus einfacher, weshalb sich auch täglich neue Menschen mit der Programmierung und/oder Umsetzung von dynamischen Inhalten mit PHP auseinander setzen.</div>
<div></div>
<div>Allerdings fällt man immer wieder über Skripte, die schlicht unsicher und völlig unperformant umgesetzt wurden, ja sogar ein erhebliches Sicherheitsrisiko darstellen, insbesondere wenn diese als OpenSource der breiten Masse angeboten werden und durch die Unwissenheit der Nutzer viele Hoster wegen solcher Skripte sehr viel Mehraufwand im Supportbereich haben.</div>
<div></div>
<div>Dabei ist es eigentlich gar nicht schwierig performant und sicher mit PHP zu programmieren, wenn man sich an einige Regeln hält und diese Konsequent umsetzt. Aus diesem Grund möchte ich hier eine Artikelserie starten, die verschiedene Aspekte der performanten und sicheren Programmierung mit PHP abhandeln, einige Grundsätze der Programmierung aufzeigen und am Ende eine Checkliste zur Verfügung stellt, in der alle Ratschläge nochmals zusammen gefasst werden.</div>
<div></div>
<div>Beginnen werde ich mit dem grundsätzlichen Aufbau von PHP-Projekten, um die Performance von Beginn an in der Interpretation des Skripte für den PHP-Interpreter zu erhöhen sowie einige Sicherheitsregeln und Funktionen, die in jeder PHP-Anwendung Ihren Platz haben sollten, um zumindest die häufigsten Sicherheitslecks zu umgehen.</div>
<div>
<ol><li>Teil) <link http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/article/php-performance-security-teil-1-php-korrekt-einstellen.html - - "Teil 1 - PHP korrekt einstellen">PHP korrekt einstellen</link></li><li>Teil)&nbsp;<link http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/article/php-performance-security-teil-2-zentrale-include-datei.html - - "Teil 2 zentrale Include-Datei">zentrale Include-Datei</link></li><li>Teil) <link http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/article/php-perfomance-security-teil-3-grundsaetzliche-security-funktionen.html - external-link-new-window "Öffnet externen Link in neuem Fenster">grundsätzliche Security-Funktionen</link></li><li>.... to be continued</li></ol>
(sj)</div>
<div></div>]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Thu, 07 Oct 2010 09:11:00 +0200</pubDate>
			
		</item>
		
		<item>
			<title>Google Pagerank mit PHP abfragen</title>
			<link>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/google-pagerank-mit-php-abfragen.html</link>
            <guid>http://foobar.lamp-solutions.de/howtos/programmierung/php/einzelansicht-php/artikel/google-pagerank-mit-php-abfragen.html</guid>
			<description>Suchmaschinenoptimierung ist eines der wichtigsten Aufgabenfelder bei der Webprogrammierung.
Ein...</description>
			<content:encoded><![CDATA[Suchmaschinenoptimierung ist eines der wichtigsten Aufgabenfelder bei der Webprogrammierung.
Ein Indikator für den Erfolg kann der Pagerank sein den Google für eine Seite veröffentlicht. Dieser Pagerank  lässt sich mit Browserplugins dank einer API leicht abragen.
Die Schnittstelle dafür lässt sich auch über PHP verwenden.
Im Internet haben wir vor einiger Zeit eine fertige Klasse für diesen Zweck gefunden, die bei uns in der Praxis sehr zuverlässig funktioniert.
Eine zip-Datei mit der Klasse findet sich am Ende des Artikels zum Download.
Wenn die Klasse eingebunden ist, reicht es eine einfache Funktion zu definieren
<br /><div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">function</span> get_pagerank<span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #666666; font-style: italic;">// Pruefe Google-Pagerank</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #000088;">$gpr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PageRank<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$gpr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printrank</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$link</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div>
und mit
<div id="tx_codehighlight_pi1"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">get_pagerank<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;www.meinedomain.tld&quot;</span><span style="color: #009900;">&#41;</span></div></li></ol></pre></div></div><br /><br />
aufzurufen.
<b>Wichtig</b>: Google verbietet in den Geschäftsbedingungen den Pagerank im großen Stil vollautomatisch abzufragen. Als Grenzwert kursiert im Internet die Zahl von 1000 Abfragen pro Tag über eine IP-Adresse. 
Es ist auf jeden Fall sinnvoll zwischen einzelnen Abfragen mit sleep() eine Pause einzulegen und ein Limit pro Tag zu definieren. ]]></content:encoded>
			<category>PHP</category>
			
			
			<pubDate>Sat, 18 Sep 2010 21:12:00 +0200</pubDate>
			<enclosure url="http://foobar.lamp-solutions.de/uploads/media/class.pagerank.zip" length ="1802" type="application/zip" />
			<enclosure url="http://foobar.lamp-solutions.de/uploads/media/class.pagerank" length ="4262" type="text/plain" />
		</item>
		
	</channel>
</rss>
