<?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; PHP</title>
	<atom:link href="http://www.informatik-blog.net/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.informatik-blog.net</link>
	<description>Informatik &#38; Co.</description>
	<lastBuildDate>Mon, 28 Jun 2010 19:43:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mehrere IP-Adressen Bundles erstellen</title>
		<link>http://www.informatik-blog.net/2009/12/16/mehrere-ip-adressen-bundles-erstellen/</link>
		<comments>http://www.informatik-blog.net/2009/12/16/mehrere-ip-adressen-bundles-erstellen/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 14:53:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[Bundle]]></category>
		<category><![CDATA[Sortieren]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=619</guid>
		<description><![CDATA[

Nunja, zugegeben der Titel sagt nicht viel, jedoch habe ich mich gestern gefragt wie ich ein Array mit mehreren Einträgen bundlen kann.
Zweck war, mehrere Domains inkl. IPs zu sortieren und bei gleichen IPs farbig auszugeben.
Das Ergebniss ist eigentlich relativ einfach wie unten genanntes Beispiel zeigt:



&#60;?php
error_reporting(E_ALL);
set_time_limit(0);

echo '
&#60;form method=&#34;post&#34; action=&#34;&#34;&#62;
&#60;textarea name=&#34;urls&#34;&#62;&#60;/textarea&#62;&#60;br&#62;
&#60;input name=&#34;submit&#34; type=&#34;submit&#34; value=&#34;Abfragen&#34; /&#62;
&#60;/form&#62;
';

if (isset($_POST['submit'])) {
 [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Nunja, zugegeben der Titel sagt nicht viel, jedoch habe ich mich gestern gefragt wie ich ein Array mit mehreren Einträgen bundlen kann.</p>
<p>Zweck war, mehrere Domains inkl. IPs zu sortieren und bei gleichen IPs farbig auszugeben.</p>
<p>Das Ergebniss ist eigentlich relativ einfach wie unten genanntes Beispiel zeigt:</p>
<p><span id="more-619"></span></p>
<pre class="brush: php;">

&lt;?php
error_reporting(E_ALL);
set_time_limit(0);

echo '
&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
&lt;textarea name=&quot;urls&quot;&gt;&lt;/textarea&gt;&lt;br&gt;
&lt;input name=&quot;submit&quot; type=&quot;submit&quot; value=&quot;Abfragen&quot; /&gt;
&lt;/form&gt;
';

if (isset($_POST['submit'])) {
 $eingabe = explode(&quot;\n&quot;, $_POST['urls']);
}

$colorsArray = array(
'#ffebcd',
'#0000ff',
'#8a2be2',
'#a52a2a',
'#deb887',
'#5f9ea0',
'#7fff00',
'#d2691e',
'#ff7f50',
'#6495ed',
'#fff8dc',
'#dc143c',
'#00ffff',
'#ff8c00',
'#9932cc',
'#8b0000',
'#e9967a',
'#8fbc8f',
'#483d8b',
'#2f4f4f',
'#00ced1',
'#9400d3',
'#ff1493',
'#00bfff',
'#1e90ff',
'#b22222',
'#fffaf0',
'#228b22',
'#ff00ff',
'#f8f8ff',
'#ffd700',
'#daa520',
'#008000',
'#adff2f',
'#f0fff0',
'#ff69b4',
'#cd5c5c',
'#4b0082',
'#fffff0',
'#f0e68c',
'#e6e6fa',
'#fff0f5',
'#7cfc00',
'#fffacd',
'#add8e6',
'#f08080',
'#e0ffff',
'#fafad2',
'#90ee90',
'#ffb6c1',
'#ffa07a',
'#20b2aa',
'#87cefa',
'#778899',
'#b0c4de',
'#ffffe0',
'#00ff00',
'#32cd32',
'#faf0e6',
'#ff00ff',
'#800000',
'#66cdaa',
'#0000cd',
'#ba55d3',
'#9370db',
'#3cb371',
'#7b68ee',
'#00fa9a',
'#48d1cc',
'#c71585',
'#191970',
'#f5fffa',
'#ffe4e1',
'#ffe4b5',
'#ffdead',
'#000080',
'#fdf5e6',
'#808000',
'#6b8e23',
'#ffa500',
'#ff4500',
'#da70d6',
'#eee8aa',
'#98fb98',
'#afeeee',
'#db7093',
'#ffefd5',
'#ffdab9',
'#cd853f',
'#ffc0cb',
'#dda0dd',
'#b0e0e6',
'#800080',
'#ff0000',
'#bc8f8f',
'#4169e1',
'#8b4513',
'#fa8072',
'#f4a460',
'#2e8b57',
'#fff5ee',
'#a0522d',
'#87ceeb',
'#6a5acd',
'#708090',
'#fffafa',
'#00ff7f',
'#4682b4',
'#d2b48c',
'#008080',
'#d8bfd8',
'#ff6347',
'#40e0d0',
'#ee82ee',
'#f5deb3',
'#f5f5f5',
'#ffff00',
'#9acd32',
'#ffffff',
'#dcdcdc',
'#d3d3d3',
'#c0c0c0',
'#a9a9a9',
'#808080'
);

echo '&lt;script src=&quot;sorttable.js&quot;&gt;&lt;/script&gt;';

$startcolor = 0;

foreach ($eingabe as $out) {  $domains[] = array(gethostbyname(trim($out))   , $out  ); }

$colors = array();
foreach ($domains as $o) {
 if (!array_key_exists( $o[0] , $colors)) {
 $colors[$o[0]] = $colorsArray[ $startcolor ] ;
 $startcolor++;
 }
}

echo &quot;&lt;table class='sortable'&gt;&lt;tr&gt;&lt;td&gt;Domain&lt;/td&gt;&lt;td&gt;IP&lt;/td&gt;&lt;/tr&gt;&quot;;

$cnt = 0;
foreach ($domains as $ausgabe) {

 echo &quot;&lt;tr style='background-color:&quot;. $colors[ $ausgabe[0] ].&quot;;'&gt;
 &lt;td&gt;&quot;.$ausgabe[1].&quot;&lt;/td&gt;
 &lt;td&gt;&quot;.$ausgabe[0].&quot;&lt;/td&gt;
 &lt;/tr&gt;
 &quot;;
}
echo &quot;&lt;/table&gt;&quot;;

?&gt;
</pre>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/12/16/mehrere-ip-adressen-bundles-erstellen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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;
&#123;
	$list [...]]]></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>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 viel einfacher:

foreach &#40;$array [...]]]></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>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 [...]]]></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 = 0; $i &#60; $length; [...]]]></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 &#123;
&#160;
&#125;

Funktioniert ganz gut. Doch [...]]]></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>
