<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Der Informatikblog &#187; bleed_ch</title>
	<atom:link href="http://www.informatik-blog.net/author/bleed_ch/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.informatik-blog.net</link>
	<description>Informatik &#38; Co.</description>
	<lastBuildDate>Mon, 19 Dec 2011 13:01:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Rekursives löschen</title>
		<link>http://www.informatik-blog.net/2009/02/04/rekursives-loeschen/</link>
		<comments>http://www.informatik-blog.net/2009/02/04/rekursives-loeschen/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 13:21:57 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[löschen]]></category>
		<category><![CDATA[rekursiv]]></category>
		<category><![CDATA[verzeichnis]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=515</guid>
		<description><![CDATA[Viel zu oft wird die Frage &#8220;Wie lösche ich ein Verzeichnis rekursiv?&#8221; mit einem 100-Zeilen Skript beantwortet. Schade eigentlich, denn viele Programmierer haben sich immer noch keine Mühe gemacht die Änderungen von PHP5 wahrzunehmen. Dabei würde sich ein genauerer Blick lohnen, die Antwort auf diese Frage ist seither sehr kurz und einfach geworden: function recursive&#40;$dest&#41; [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Viel zu oft wird die Frage <strong>&#8220;Wie lösche ich ein Verzeichnis rekursiv?&#8221;</strong> mit einem 100-Zeilen Skript beantwortet. Schade eigentlich, denn viele Programmierer haben sich immer noch keine Mühe gemacht die Änderungen von PHP5 wahrzunehmen. <span id="more-515"></span> Dabei würde sich ein genauerer Blick lohnen, die Antwort auf diese Frage ist seither sehr kurz und einfach geworden:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> recursive<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dest</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$list</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_diff</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$list</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dest</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><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> recursive<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">rmdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Wie wir sehen, passt das sehr einfach in 9 Zeilen. Was geschieht denn genau? Zuerst lesen wir das Verzeichnis, das wir beim Funktionsaufruf mitgegeben haben aus (scandir()) und entfernen danach <strong>&#8220;.&#8221; (aktuelles Verzeichnis)</strong> und <strong>&#8220;..&#8221; (übergeordnetes Verzeichnis)</strong>. Das muss sein, denn sonst würden wir bei ersterem in eine Endlosschleife geraten und bei letzterem über das Ziel hinaus schiessen und übergeordnete Dateien löschen. Sobald wir also unsere Liste haben, gehen wir sie durch und prüfen ob es sich um ein Verzeichnis handelt oder nicht. Ist es eins, so wird die gesamte Prozedur nochmal darauf angewandt &#8211; andernfalls wird die Datei einfach gelöscht und das geht dann so lange bis nichts mehr übrig ist.</p>
<p>Verwendet wird es übrigens folgendermassen:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>recursive<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./bilder'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;super, hat geklappt.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;ohje, bestimmt hab ich keine berechtigung dazu.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Schlägt der Funktionsaufruf fehl, erscheinen (je nach tiefe der Verzeichnise) von PHP generierte Fehlermeldungen.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/02/04/rekursives-loeschen/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fremdschlüssel</title>
		<link>http://www.informatik-blog.net/2009/01/26/fremdschluessel/</link>
		<comments>http://www.informatik-blog.net/2009/01/26/fremdschluessel/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 22:17:23 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[beziehung]]></category>
		<category><![CDATA[fremdschlüssel]]></category>
		<category><![CDATA[relation]]></category>
		<category><![CDATA[verbindung]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=316</guid>
		<description><![CDATA[Ein ganz wichtiges Kapitel, vorallem wenn es um die Planung eines etwas grösseren Projektes geht, ist das Thema Fremdschlüssel. Fremdschlüssel verbinden (meist tabellenübergreifend) Einträge. Nehmen wir an, wir haben eine &#8220;Mitglieder&#8221;-Tabelle. Diese Tabelle besitzt die Spalten &#8220;id&#8221; und &#8220;name&#8221; wobei ersteres der Primärschlüssel ist: Nun fügen wir eine weitere Tabelle namens &#8220;Tagebuch&#8221; hinzu. Sie beinhaltet [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Ein ganz wichtiges Kapitel, vorallem wenn es um die Planung eines etwas grösseren Projektes geht, ist das Thema Fremdschlüssel. Fremdschlüssel verbinden (meist tabellenübergreifend) Einträge. <span id="more-316"></span> Nehmen wir an, wir haben eine &#8220;Mitglieder&#8221;-Tabelle. Diese Tabelle besitzt die Spalten &#8220;id&#8221; und &#8220;name&#8221; wobei ersteres der Primärschlüssel ist:</p>
<p><img class="alignnone size-full wp-image-357" title="dbd-eins" src="http://www.informatik-blog.net/wp-content/uploads/2009/01/dbd-eins.png" alt="dbd-eins" width="170" height="180" /></p>
<p>Nun fügen wir eine weitere Tabelle namens &#8220;Tagebuch&#8221; hinzu. Sie beinhaltet die Spalten &#8220;id&#8221;, &#8220;mitglied_id&#8221; und &#8220;eintrag&#8221;. Der Primärschlüssel ist &#8220;id&#8221; und auf &#8220;mitglied_id&#8221; liegt ein Index:</p>
<p><img class="alignnone size-full wp-image-358" title="dbd-zwei" src="http://www.informatik-blog.net/wp-content/uploads/2009/01/dbd-zwei.png" alt="dbd-zwei" width="170" height="180" /></p>
<p>Der Index ist (je nach DBS) optional aber dringend empfohlen, da es sonst (vorallem mit wachsender Anzahl an Daten) zu starken Geschwindigkeitseinbussen kommen kann. Wir befüllen also die erste Tabelle:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> Mitglieder <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">42</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'A. Merkel'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>und dann noch die zweite:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> Tagebuch <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span> mitglied_id<span style="color: #66cc66;">,</span> eintrag<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">42</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'I am beautiful, no matter what they say...'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Damit es nicht zur Verwirrung kommt habe ich unserem Mitglied die &#8220;id&#8221; 42 gegeben. Doch was passiert wenn wir den Eintrag löschen? Nichts. Und genau das ist das Problem: Sobald wir das versuchen würden, müssten wir dafür sorgen dass in allen Untertabellen sämtliche Relationen aktualisiert werden. Da sowas aber je nach Umfang des Projektes sehr kompliziert enden kann, geben die Datenbanksysteme uns eine sehr nützliche Funktionalität:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Tagebuch <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>mitglied_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> Mitglieder<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CASCADE <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> CASCADE;</pre></div></div>

<p>Wir haben soeben erfolgreich einen Fremdschlüssel von &#8220;Tagebuch:mitglied_id&#8221; zu &#8220;Mitglied:id&#8221; definiert. Bildlich würde das ganze so aussehen:</p>
<p><img class="alignnone size-full wp-image-381" title="dbd-relation" src="http://www.informatik-blog.net/wp-content/uploads/2009/01/dbd-relation.png" alt="dbd-relation" width="440" height="180" /></p>
<p>Wenn wir jetzt unserem Mitglied eine neue &#8220;id&#8221; verpassen oder es komplett löschen, geschieht es seinen Tagebucheinträgen gleich. Ausserdem ist es jetzt nicht mehr möglich einen Eintrag mit ungültiger &#8220;mitglied_id&#8221; in das Tagebuch einzufügen.</p>
<p>Es gibt 5 Reaktionen bei Fremdschlüssel:</p>
<p>** <strong>NO ACTION</strong>, Verweigert die Änderung und löst ein Fehler aus.<br />
<strong>RESTRICT</strong>, Verweigert die Änderung und löst ein Fehler aus.<br />
<strong>CASCADE</strong>, Reagiert gleich wie sein Parent.<br />
<strong>SET DEFAULT</strong>, Setzt den Standardwert.<br />
<strong>SET NULL</strong>, Setzt NULL.</p>
<p>** Bei dieser Option ist eine verzögerte Überprüfung möglich um so einer Verletzung der Regel zuvorzukommen.</p>
<p>Information 1: Der Datentyp TEXT gehört nicht zum SQL-Standard. Deshalb wurde VARCHAR ohne Längenangabe verwendet.</p>
<p>Information 2: Das Datenbanksystem MySQL macht keinen Unterschied zwischen &#8220;NO ACTION&#8221; und &#8220;RESTRICT&#8221;.</p>
<p>Information 3: Unter MySQL sind Fremdschlüssel nur mit der Datenbankengine InnoDB möglich. Standard: MyISAM.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/01/26/fremdschluessel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>foreach mit Referenzen</title>
		<link>http://www.informatik-blog.net/2009/01/26/foreach-mit-referenzen/</link>
		<comments>http://www.informatik-blog.net/2009/01/26/foreach-mit-referenzen/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 14:46:48 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[referenz]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=306</guid>
		<description><![CDATA[PHP bietet unter foreach eine Möglichkeit an, die Werte des originalen Arrays direkt anzusprechen. Programmierer die von dieser Möglichkeit nicht bescheid wissen, bauen dann oftmals Konstrukte wie: foreach &#40;$array as $key =&#62; $value&#41; &#123; $array&#91;$key&#93; = funktionsname&#40;$array&#91;$key&#93;&#41;; &#125; Bei diesem Beispiel wird der Wert des Arrays einer Funktion übergeben und danach überschrieben. Dabei ginge es [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>PHP bietet unter foreach eine Möglichkeit an, die Werte des originalen Arrays direkt anzusprechen. <span id="more-306"></span> Programmierer die von dieser Möglichkeit nicht bescheid wissen, bauen dann oftmals Konstrukte wie:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> funktionsname<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Bei diesem Beispiel wird der Wert des Arrays einer Funktion übergeben und danach überschrieben. Dabei ginge es viel einfacher:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> funktionsname<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: #009900;">&#125;</span></pre></div></div>

<p>Hier wird eine Referenz auf &#8220;value&#8221; gesetzt. Eine Referenz ist eine weitere Variable die auf den gleichen Wert im Speicher zeigt. Im Umkehrschluss wird der Speicher auch erst dann wieder freigegeben, sobald keine Variable mehr auf den Wert zeigt.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/01/26/foreach-mit-referenzen/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Formulare deaktivieren</title>
		<link>http://www.informatik-blog.net/2009/01/25/formulare-deaktivieren/</link>
		<comments>http://www.informatik-blog.net/2009/01/25/formulare-deaktivieren/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 23:11:02 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[deaktivieren]]></category>
		<category><![CDATA[formular]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=227</guid>
		<description><![CDATA[Was man öfters bei grossen Seiten wie PayPal o.ä. sieht, ist das deaktivieren von Formularen nach dem Versand. Das ganze wird mit JavaScript realisiert und dient dazu dem herkömmlichen Benutzer Doppelpostings zu verbieten. Ein wirklicher Schutz ist das natürlich nicht denn JavaScript ist deaktivierbar. Es hat aber dennoch seine Existenzberechtigung: function troughForms&#40;sent&#41; &#123; var len [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Was man öfters bei grossen Seiten wie <a href="http://www.paypal.com">PayPal</a> o.ä. sieht, ist das deaktivieren von Formularen nach dem Versand. Das ganze wird mit JavaScript realisiert und dient dazu dem herkömmlichen Benutzer Doppelpostings zu verbieten. <span id="more-227"></span> Ein wirklicher Schutz ist das natürlich nicht denn JavaScript ist deaktivierbar. Es hat aber dennoch seine Existenzberechtigung:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> troughForms<span style="color: #009900;">&#40;</span>sent<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> len <span style="color: #339933;">=</span> document.<span style="color: #660066;">forms</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">forms</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> disableForm<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span> appendEvent<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> disableForm<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> len <span style="color: #339933;">=</span> form.<span style="color: #660066;">elements</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> element <span style="color: #339933;">=</span> form.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		element.<span style="color: #660066;">disabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		element.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> appendEvent<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	form.<span style="color: #660066;">onsubmit</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		troughForms<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> troughForms<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Erstmal tief Luft holen. Das sieht kompliziert aus &#8211; ist es aber nicht. Dreh- und Angelpunkt des ganzen ist troughForms(). Diese Funktion geht das gesamte Dokument durch und entscheidet anhand des Parameters ob er ein onsubmit-Event an das Formular anhängt oder es komplett deaktiviert. Beim ersten mal wird es natürlich dazu benutzt allen Formularen auf der Seite ein Event zu verpassen. Beim Versand passiert dann genau das Gegenteil. Die gleiche Funktion ist also für das Setup und den Gebrauch zuständig.</p>
<p><strong>Die Vorteile an diesem Beispiel sind:</strong><br />
- nichts muss am Markup geändert werden.<br />
- nicht nur der &#8220;Senden&#8221;-Knopf wird deaktiviert sondern das gesamte Formular.<br />
- nicht nur das versandte sondern jedes Formular auf der Seite wird deaktiviert.</p>
<p>Um das ganze zu verwenden, müssen wir den JavaScript-Code (oben) in eine Datei speichern. In unserem Fall wählen wir <strong>deactivate.js</strong>, die wir dann im HTML-Dokument folgendermassen einbinden:
</pre>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;./deactivate.js&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>Geschafft. Übrigens wäre das ganze mit addEventListener('submit', ...) sicher moderner gewesen aber nicht jeder Browser unterstützt es. Und bei eingefügter Browserkompatibilität wäre der Code dann mindestens gleich gross aber nicht gleich schön.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/01/25/formulare-deaktivieren/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Race-Conditions</title>
		<link>http://www.informatik-blog.net/2009/01/23/sql-race-conditions/</link>
		<comments>http://www.informatik-blog.net/2009/01/23/sql-race-conditions/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 11:23:31 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[functional-index]]></category>
		<category><![CDATA[race-condition]]></category>
		<category><![CDATA[unique-constraint]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=162</guid>
		<description><![CDATA[Jeder kennt es, keiner will es: Race-Conditions. Ein Benutzer registriert sich und versendet versehentlich zweimal hintereinander das gleiche Formular. Wer jetzt zuerst zählt: SELECT COUNT&#40;*&#41; AS total FROM tabelle WHERE mail = 'user@example.com'; und danach gleich einfügt: INSERT INTO tabelle &#40;mail&#41; VALUES &#40;'user@example.com'&#41;; läuft Gefahr Opfer einer Race-Condition zu werden. Was ist das und was [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Jeder kennt es, keiner will es: Race-Conditions. Ein Benutzer registriert sich und versendet versehentlich zweimal hintereinander das gleiche Formular. <span id="more-162"></span> Wer jetzt zuerst zählt:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> total <span style="color: #993333; font-weight: bold;">FROM</span> tabelle <span style="color: #993333; font-weight: bold;">WHERE</span> mail <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'user@example.com'</span>;</pre></div></div>

<p>und danach gleich einfügt:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> tabelle <span style="color: #66cc66;">&#40;</span>mail<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'user@example.com'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>läuft Gefahr Opfer einer Race-Condition zu werden.</p>
<p><strong>Was ist das und was passiert genau?</strong></p>
<p>Das ist eigentlich ganz einfach erklärt. Wenn wir zweimal das gleiche Formular versenden, durchläuft der Server zweimal die gleiche Datei. Ist der zeitliche Unterschied zwischen den beiden Aufrufen genug klein, ergibt sich bei beiden bei der ersten Zählung 0 und es wird danach auch bei beiden eingefügt. Da Spalten wie &#8220;mail&#8221; aber üblicherweise einen &#8220;unique-constraint&#8221; oder eben einen &#8220;unique-functional-index&#8221; besitzen, kommt es zum schlimmsten: Der spätere der beiden Aufrufe schlägt fehl und wird mit einem Fehler quittiert: Der Datensatz ist bereits vorhanden.</p>
<p><strong>Aber abgebrochene Aufrufe werden vom Server ja auch abgebrochen?</strong></p>
<p>Mit den Standardeinstellungen schon. Oftmals entscheiden sich Programmierer aber dazu &#8220;ignore_user_abort&#8221; zu benutzen was ja auch seinen guten Grund hat. Beispiel Bilderupload: Dort wird üblicherweise ein SQL-Query versandt und danach die Datei im Dateisystem entsprechend untergebracht. (Das ganze geht auch umgekehrt) Wenn der Benutzer jetzt zwischen einer der beiden Aktionen seinen Aufruf abbricht, haben wir einen losen Datensatz in der Datenbank oder im Dateisystem. Die Integrität wäre verloren. Ein weiteres Beispiel wäre eine Tabelle in der alle Besuche eines Mitglieds bei einem anderen Mitglied gespeichert werden natürlich mit einem &#8220;unique-constraint&#8221; über from_id und dest_id:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> tabelle <span style="color: #993333; font-weight: bold;">SET</span> visited <span style="color: #66cc66;">=</span> NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> from_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AND</span> dest_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>;</pre></div></div>

<p>Wer sich jetzt nur auf die betroffenen Zeilen verlässt und danach gleich einfügt:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> tabelle <span style="color: #66cc66;">&#40;</span>from_id<span style="color: #66cc66;">,</span> dest_id<span style="color: #66cc66;">,</span> visited<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>hat wieder eine wunderschöne und noch viel wahrscheinlich eintreffendere Race-Condition erstellt. Bei diesem Beispiel reicht es sogar aus ein Mitglied, welches noch nie besucht wurde, zweimal schnell hintereinander (Doppelklick) zu besuchen was ja nun wirklich nicht realitätsfremd ist.</p>
<p><strong>Was kann ich dagegen tun?</strong></p>
<p>Das ist jetzt eben der trickreiche Teil des ganzen. Er nennt sich &#8220;INSERT INTO &#8230; SELECT&#8221; mit einem Subquery und sieht aus wie folgt:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> tabelle <span style="color: #66cc66;">&#40;</span>mail<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'user@example.com'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> tabelle <span style="color: #993333; font-weight: bold;">WHERE</span> mail <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'user@example.com'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>und für das zweite Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> tabelle <span style="color: #66cc66;">&#40;</span>from_id<span style="color: #66cc66;">,</span> dest_id<span style="color: #66cc66;">,</span> visited<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> tabelle <span style="color: #993333; font-weight: bold;">WHERE</span> from_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AND</span> dest_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>Das war es auch schon. Wenn wir jetzt noch sauberkeitshalber die betroffenen Zeilen abfragen, können wir eine schön formatierte Meldung über den (Miss-)Erfolg ausgeben.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/01/23/sql-race-conditions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Aufgepasst: E-Mail</title>
		<link>http://www.informatik-blog.net/2009/01/22/aufgepasst-e-mail/</link>
		<comments>http://www.informatik-blog.net/2009/01/22/aufgepasst-e-mail/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 20:31:41 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[e-mail]]></category>
		<category><![CDATA[functional-index]]></category>
		<category><![CDATA[lower]]></category>
		<category><![CDATA[strtolower]]></category>
		<category><![CDATA[trim]]></category>
		<category><![CDATA[unique-constraint]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=97</guid>
		<description><![CDATA[Ein Projekt wird realisiert und dabei wird eines viel zu oft vergessen: Die richtige Handhabung der E-Mailadressen. Üblicherweise erstellt der Initiator des Projektes eine Tabelle mit der Spalte &#8220;mail&#8221;, setzt sie auf unique und gibt sich damit zufrieden. Doch aufgepasst: Je nach Einstellungen des Datenbanksystems oder der Verbindung ist die unique-Spalte case-sensitive. Das bedeutet sie [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Ein Projekt wird realisiert und dabei wird eines viel zu oft vergessen: Die richtige Handhabung der E-Mailadressen. Üblicherweise erstellt der Initiator des Projektes eine Tabelle mit der Spalte &#8220;mail&#8221;, setzt sie auf unique und gibt sich damit zufrieden. <span id="more-97"></span> Doch aufgepasst: Je nach Einstellungen des Datenbanksystems oder der Verbindung ist die unique-Spalte case-sensitive. Das bedeutet sie unterscheidet zwischen Gross- und Kleinschreibung. Dann wird es möglich, sich mit ein- und derselben E-Mailadresse mehrmals zu registrieren. Zum Beispiel:</p>
<p><strong>&#8220;user@example.com&#8221; und &#8220;User@example.com&#8221;</strong></p>
<p>Ausserdem entfernen die meisten MTAs überschüssige Leerzeichen am Anfang und am Ende der E-Mailadresse.  MTAs (Mail Transfer Agent) sind Programme, die nach dem Aufruf von mail() eine E-Mail im Hintergrund versenden. Ein Beispiel für diesen Fehler:</p>
<p><strong>&#8220;user@example.com&#8221; und &#8221; user@example.com&#8221;</strong></p>
<p>Für die Datenbank sind das nicht die gleichen Strings &#8211; für manche MTAs schon. Daraus ergibt sich, vorallem in Kombination beider Fehler, eine imense Anzahl an Möglichkeiten sich mit der gleichen E-Mailadresse zu registrieren.</p>
<p>Nun gibt es grundsätzlich 2 Varianten dem entgegenzutreten. Die erste wäre folgende Funktion:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> address<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mail</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$revised</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mail</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$revised</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Bei diesem (durch PHP gesicherten) Beispiel muss einfach sichergestellt werden, dass die Funktion überall dort Verwendung findet wo eine E-Mailadresse entgegengenommen wird. Das zweite Beispiel wäre ein sogenannter &#8220;functional index&#8221; im DBS. Das sind Indizes die einer gewissen Regel (Funktion) obliegen:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> name_vom_index <span style="color: #993333; font-weight: bold;">ON</span> tabelle<span style="color: #66cc66;">&#40;</span>lower<span style="color: #66cc66;">&#40;</span>trim<span style="color: #66cc66;">&#40;</span>spalte<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Ich bin mir nicht sicher ob MySQL letzteres unterstützt. PostgreSQL tut es zumindest.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/01/22/aufgepasst-e-mail/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Passwort leicht gemacht</title>
		<link>http://www.informatik-blog.net/2009/01/22/passwort-leicht-gemacht/</link>
		<comments>http://www.informatik-blog.net/2009/01/22/passwort-leicht-gemacht/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 17:41:12 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[passwort]]></category>
		<category><![CDATA[zufall]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=81</guid>
		<description><![CDATA[Jeder Programmierer der früher oder später eine Registration erstellen will, wird um eines nicht herum kommen: Ein Passwortgenerator. Die meisten dürften wissen, wie zufällige Zahlen generiert werden &#8211; ganz anders sieht es jedoch bei Buchstaben aus. Hier ein Beispiel mit der Funktion chr(): function password&#40;$length = 10&#41; &#123; &#160; $pass = &#34;&#34;; &#160; for &#40;$i [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Jeder Programmierer der früher oder später eine Registration erstellen will, wird um eines nicht herum kommen: Ein Passwortgenerator. Die meisten dürften wissen, wie zufällige Zahlen generiert werden &#8211; ganz anders sieht es jedoch bei Buchstaben aus. <span id="more-81"></span> Hier ein Beispiel mit der Funktion chr():</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> password<span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<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>
&nbsp;
		<span style="color: #000088;">$rand</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">65</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">97</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">122</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$pass</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Was passiert genau? Beim Funktionsaufruf wird erstmal ein leerer String namens &#8220;pass&#8221; erstellt. Danach wird in einer for-Schleife Buchstaben für Buchstaben an &#8220;pass&#8221; angehängt. Dazu wird eine zufällige Zahl zwischen 65-90 oder 97-122 generiert, die wir danach an chr() übergeben. Die Funktion chr() erwartet den <a href="http://www.asciitable.com/">Indexwert eines ASCII-Zeichens</a>. Ein weiterer Weg um ein Zufallspasswort zu generieren sieht wie folgt aus:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> password<span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$char</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;z&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;A&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Z&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<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>
&nbsp;
		<span style="color: #000088;">$rand</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$char</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$pass</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$char</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rand</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Das zweite Beispiel ist ziemlich ähnlich wie das erste. Zuerst wird ein leerer String namens "pass" erstellt. Danach werden 2 Arrays (a-z, A-Z) zu einem zusammengeführt (array_merge()). In der Schleife wird dann jeweils ein zufälliger Index des neuen grossen Arrays ausgesucht und im nächsten Schritt den Wert von ihm an "pass" angehängt. Verwendet werden unsere Funktionen übrigens folgendermassen:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pwd</span> <span style="color: #339933;">=</span> password<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
In $pwd befindet sich jetzt ein zufälliges Passwort.
Die Standardlänge ist, wie oben ersichtlich, 10.
Mittels Argument können wir das ganze auch selber bestimmen:
*/</span>
<span style="color: #000088;">$pwd</span> <span style="color: #339933;">=</span> password<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Jetzt befindet sich in $pwd ein Passwort mit der Länge 12.</span></pre></div></div>

<p>Welches der Funktionen du verwendest ist geschmackssache und somit letztlich dir überlassen. Es empfiehlt sich aber für das Verständniss mindestens einmal beide auszuprobieren.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/01/22/passwort-leicht-gemacht/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>substr_count() statt strpos()</title>
		<link>http://www.informatik-blog.net/2009/01/22/substr_count-statt-strpos/</link>
		<comments>http://www.informatik-blog.net/2009/01/22/substr_count-statt-strpos/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 15:44:54 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[position]]></category>
		<category><![CDATA[strpos]]></category>
		<category><![CDATA[substr_count]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=44</guid>
		<description><![CDATA[Immer wieder wird in Foren oder im IRC strpos() empfohlen um zu prüfen ob etwas in einem String vorkommt. Sehen wir uns einmal an warum substr_count() besser dazu geeignet ist: $str = &#34;ich bin ein teststring&#34;; &#160; if &#40;strpos&#40;$str, &#34;bin&#34;&#41;&#41; &#123; // liefert die Position zurück, Integer: 4 // Dieser Block wird ausgeführt. &#125; else [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Immer wieder wird in Foren oder im IRC strpos() empfohlen um zu prüfen ob etwas in einem String vorkommt. <span id="more-44"></span> Sehen wir uns einmal an warum substr_count() besser dazu geeignet ist:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ich bin ein teststring&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;bin&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// liefert die Position zurück, Integer: 4</span>
	<span style="color: #666666; font-style: italic;">// Dieser Block wird ausgeführt.</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Funktioniert ganz gut. Doch was passiert wenn wir nach &#8220;ich&#8221; suchen?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ich bin ein teststring&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ich&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// In dem Fall Integer: 0</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Dieser Block wird ausgeführt.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Was ist passiert? Wir sehen im zweiten Beispiel, dass der else-Teil ausgeführt wird obwohl ein Vorkomniss gefunden wurde. Das liegt ganz einfach daran, dass strpos() eine Positionsangabe zurückliefert und wie wir alle wissen ist 0 für den Computer die erste Zahl. Da der Integerwert 0 aber auch für bool(false) steht, springt er in den else-Teil. Wir müssen bei strpos() also nicht nur den Wert sondern auch den Typ prüfen denn diese Funktion kann je nach Ergebniss 0 oder false zurückliefern was nicht das gleiche ist. Korrekt wäre also:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ich bin ein teststring&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ich&quot;</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: #666666; font-style: italic;">// Hier auch Integer: 0</span>
	<span style="color: #666666; font-style: italic;">// Dieser Block wird ausgeführt.</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Fazit: Wenn man einfach nur wissen will ob sich etwas in einem String befindet, sollte man substr_count() benutzen. Wer jedoch die Position braucht oder Gewicht auf (meiner Meinung nach vernachlässigbarer) Performance legt kann auch strpos() nutzen, sollte allerdings nicht vergessen den Typ der Rückgabe zu prüfen.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/01/22/substr_count-statt-strpos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

