<?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>Julius Beckmann &#187; PHP</title>
	<atom:link href="http://juliusbeckmann.de/blog/category/php/feed" rel="self" type="application/rss+xml" />
	<link>http://juliusbeckmann.de/blog</link>
	<description>Ich bin nicht verrückt, nur technisch begabt ...</description>
	<lastBuildDate>Wed, 05 Jun 2013 11:02:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP: Uncomment PHP Code</title>
		<link>http://juliusbeckmann.de/blog/php-uncomment-php-code.html</link>
		<comments>http://juliusbeckmann.de/blog/php-uncomment-php-code.html#comments</comments>
		<pubDate>Fri, 18 Nov 2011 09:33:52 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Comment]]></category>
		<category><![CDATA[Gist]]></category>
		<category><![CDATA[GitHub]]></category>
		<category><![CDATA[Script]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=883</guid>
		<description><![CDATA[If you want to uncomment your PHP Code, have a look at my script: https://gist.github.com/1375986 Usage: # php uncomment /path/to/file.php The script will read the content of the file, remove the comment, and write back to the file. So the file will be CHANGED! You have been warned.]]></description>
			<content:encoded><![CDATA[<p><img src="http://juliusbeckmann.de/blog/static/php-logo.jpg" alt="php-logo" title="php-logo" width="100" height="55" class="alignright size-full wp-image-855" /></p>
<p>If you want to uncomment your PHP Code, have a look at my script:</p>
<p><a href="https://gist.github.com/1375986">https://gist.github.com/1375986</a></p>
<p>Usage:</p>
<div class="codesnip-container" ># php uncomment /path/to/file.php</div>
<p>The script will read the content of the file, remove the comment, and write back to the file.<br />
So the file will be CHANGED! You have been warned.</p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-uncomment-php-code.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: The big php variable type casting!</title>
		<link>http://juliusbeckmann.de/blog/php-the-big-php-variable-type-casting.html</link>
		<comments>http://juliusbeckmann.de/blog/php-the-big-php-variable-type-casting.html#comments</comments>
		<pubDate>Sat, 05 Nov 2011 14:25:30 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=879</guid>
		<description><![CDATA[I wrote a small script that does every available cast in PHP on every datattype. Check it out on GistHub: https://gist.github.com/1341572 The output of the script should be the following: $ php casting.php --- Casting all in PHP available Types in every way --- Info: In PHP we have the following data types: boolean, int, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://juliusbeckmann.de/blog/static/php-logo.jpg" alt="php-logo" title="php-logo" width="100" height="55" class="alignright size-full wp-image-855" />I wrote a small script that does every available cast in PHP on every datattype.<br />
Check it out on GistHub:<br />
<a href="https://gist.github.com/1341572">https://gist.github.com/1341572</a></p>
<p>The output of the script should be the following:</p>
<div class="codesnip-container" >$ php casting.php </p>
<p>--- Casting all in PHP available Types in every way ---</p>
<p>Info:<br />
In PHP we have the following data types: boolean, int, float, double, array, object,<br />
while float and double should be the same.<br />
There are other types that can not be created by cast, like resources.<br />
PHP is weak typed and allows to cast everything to everything.<br />
I think this is something every (good) Software Programmer working with PHP should know.<br />
Check out the results of the casts, especially to boolean, so you do not need<br />
if(strlen($s) > 0 || count($a) > 0) ... anymore.</p>
<p>PHP-Version: 5.3.5-1ubuntu7.3</p>
<p>--- casting 'null' ------------<br />
(boolean)null -> (boolean)false<br />
(int)null -> (integer)0<br />
(float)null -> (double)0<br />
(double)null -> (double)0<br />
(string)null -> (string)''<br />
(array)null -> (array)array ()<br />
(object)null -> (object)stdClass</p>
<p>--- casting '0' ------------<br />
(boolean)0 -> (boolean)false<br />
(int)0 -> (integer)0<br />
(float)0 -> (double)0<br />
(double)0 -> (double)0<br />
(string)0 -> (string)'0'<br />
(array)0 -> (array)array (0 => 0,)<br />
(object)0 -> (object)stdClass</p>
<p>--- casting '1' ------------<br />
(boolean)1 -> (boolean)true<br />
(int)1 -> (integer)1<br />
(float)1 -> (double)1<br />
(double)1 -> (double)1<br />
(string)1 -> (string)'1'<br />
(array)1 -> (array)array (0 => 1,)<br />
(object)1 -> (object)stdClass</p>
<p>--- casting '0.0' ------------<br />
(boolean)0.0 -> (boolean)false<br />
(int)0.0 -> (integer)0<br />
(float)0.0 -> (double)0<br />
(double)0.0 -> (double)0<br />
(string)0.0 -> (string)'0'<br />
(array)0.0 -> (array)array (0 => 0,)<br />
(object)0.0 -> (object)stdClass</p>
<p>--- casting '0.1' ------------<br />
(boolean)0.1 -> (boolean)true<br />
(int)0.1 -> (integer)0<br />
(float)0.1 -> (double)0.1<br />
(double)0.1 -> (double)0.1<br />
(string)0.1 -> (string)'0.1'<br />
(array)0.1 -> (array)array (0 => 0.1,)<br />
(object)0.1 -> (object)stdClass</p>
<p>--- casting '1.23' ------------<br />
(boolean)1.23 -> (boolean)true<br />
(int)1.23 -> (integer)1<br />
(float)1.23 -> (double)1.23<br />
(double)1.23 -> (double)1.23<br />
(string)1.23 -> (string)'1.23'<br />
(array)1.23 -> (array)array (0 => 1.23,)<br />
(object)1.23 -> (object)stdClass</p>
<p>--- casting 'true' ------------<br />
(boolean)true -> (boolean)true<br />
(int)true -> (integer)1<br />
(float)true -> (double)1<br />
(double)true -> (double)1<br />
(string)true -> (string)'1'<br />
(array)true -> (array)array (0 => true,)<br />
(object)true -> (object)stdClass</p>
<p>--- casting 'false' ------------<br />
(boolean)false -> (boolean)false<br />
(int)false -> (integer)0<br />
(float)false -> (double)0<br />
(double)false -> (double)0<br />
(string)false -> (string)''<br />
(array)false -> (array)array (0 => false,)<br />
(object)false -> (object)stdClass</p>
<p>--- casting '""' ------------<br />
(boolean)"" -> (boolean)false<br />
(int)"" -> (integer)0<br />
(float)"" -> (double)0<br />
(double)"" -> (double)0<br />
(string)"" -> (string)''<br />
(array)"" -> (array)array (0 => '',)<br />
(object)"" -> (object)stdClass</p>
<p>--- casting '"abc"' ------------<br />
(boolean)"abc" -> (boolean)true<br />
(int)"abc" -> (integer)0<br />
(float)"abc" -> (double)0<br />
(double)"abc" -> (double)0<br />
(string)"abc" -> (string)'abc'<br />
(array)"abc" -> (array)array (0 => 'abc',)<br />
(object)"abc" -> (object)stdClass</p>
<p>--- casting 'array()' ------------<br />
(boolean)array() -> (boolean)false<br />
(int)array() -> (integer)0<br />
(float)array() -> (double)0<br />
(double)array() -> (double)0<br />
(string)array() -> (string)'Array'<br />
(array)array() -> (array)array ()<br />
(object)array() -> (object)stdClass</p>
<p>--- casting 'array(1)' ------------<br />
(boolean)array(1) -> (boolean)true<br />
(int)array(1) -> (integer)1<br />
(float)array(1) -> (double)1<br />
(double)array(1) -> (double)1<br />
(string)array(1) -> (string)'Array'<br />
(array)array(1) -> (array)array (0 => 1,)<br />
(object)array(1) -> (object)stdClass</p>
<p>--- casting 'array(1, 2)' ------------<br />
(boolean)array(1, 2) -> (boolean)true<br />
(int)array(1, 2) -> (integer)1<br />
(float)array(1, 2) -> (double)1<br />
(double)array(1, 2) -> (double)1<br />
(string)array(1, 2) -> (string)'Array'<br />
(array)array(1, 2) -> (array)array (0 => 1,1 => 2,)<br />
(object)array(1, 2) -> (object)stdClass</p>
<p>--- casting 'fopen("php://stdout", "w")' ------------<br />
(boolean)fopen("php://stdout", "w") -> (boolean)true<br />
(int)fopen("php://stdout", "w") -> (integer)6<br />
(float)fopen("php://stdout", "w") -> (double)7<br />
(double)fopen("php://stdout", "w") -> (double)8<br />
(string)fopen("php://stdout", "w") -> (string)'Resource id #9'<br />
(array)fopen("php://stdout", "w") -> (array)array (0 => NULL,)<br />
(object)fopen("php://stdout", "w") -> (object)stdClass</p>
<p>--- casting 'new A()' ------------<br />
(boolean)new A() -> (boolean)true<br />
PHP Notice:  Object of class A could not be converted to int in casting.php(67) : eval()'d code on line 1<br />
(int)new A() -> (integer)1<br />
PHP Notice:  Object of class A could not be converted to double in casting.php(67) : eval()'d code on line 1<br />
(float)new A() -> (double)1<br />
PHP Notice:  Object of class A could not be converted to double in casting.php(67) : eval()'d code on line 1<br />
(double)new A() -> (double)1<br />
(string)new A() -> (string)'A'<br />
(array)new A() -> (array)array ()<br />
(object)new A() -> (object)A</p>
<p>--- PROBLEMS ------------<br />
Some constructs in PHP need a parameter type to work correnctly,<br />
like foreach() and many functions like implode() ....</p>
<p>foreach(array("works") as $a) {}<br />
PHP Warning:  Invalid argument supplied for foreach() in casting.php on line 78<br />
PHP Warning:  Invalid argument supplied for foreach() in casting.php on line 79<br />
PHP Warning:  Invalid argument supplied for foreach() in casting.php on line 80<br />
implode('', array("works"));<br />
PHP Warning:  implode(): Invalid arguments passed in casting.php on line 84<br />
PHP Warning:  implode(): Invalid arguments passed in casting.php on line 85<br />
PHP Warning:  implode(): Invalid arguments passed in casting.php on line 86</p></div>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-the-big-php-variable-type-casting.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Playing with stdClass</title>
		<link>http://juliusbeckmann.de/blog/php-playing-with-stdclass.html</link>
		<comments>http://juliusbeckmann.de/blog/php-playing-with-stdclass.html#comments</comments>
		<pubDate>Mon, 24 Oct 2011 16:15:38 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=875</guid>
		<description><![CDATA[I wrote some code to check what PHP included stdClass is capeable of. You can find the code at gisthub: https://gist.github.com/1309031 stdClass is really just a wrapper around a array that can be casted to a array. Good thing about it is, you can extend it with some failsave __get($key) function.]]></description>
			<content:encoded><![CDATA[<p><img src="http://juliusbeckmann.de/blog/static/php-logo.jpg" alt="php-logo" title="php-logo" width="100" height="55" class="alignright size-full wp-image-855" />I wrote some code to check what PHP included stdClass is capeable of.<br />
You can find the code at gisthub:<br />
<a href="https://gist.github.com/1309031">https://gist.github.com/1309031</a></p>
<p>stdClass is really just a wrapper around a array that can be casted to a array.<br />
Good thing about it is, you can extend it with some failsave __get($key) function.</p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-playing-with-stdclass.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Universal NULL Object</title>
		<link>http://juliusbeckmann.de/blog/php-universal-null-object.html</link>
		<comments>http://juliusbeckmann.de/blog/php-universal-null-object.html#comments</comments>
		<pubDate>Sat, 23 Jul 2011 15:15:17 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Gist]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=869</guid>
		<description><![CDATA[When working with interfaces you sometimes need a object as parameter. Thats why i wrote this primity universal null object that does nothing at all and does not throw any errors: &#60;?php /** * Universal NULL object. * * Will throw no error messages so can be used as universal parameter. * Does overwrite all [...]]]></description>
			<content:encoded><![CDATA[<p>When working with interfaces you sometimes need a object as parameter. Thats why i wrote this primity universal null object that does nothing at all and does not throw any errors:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<span class="co4">/**<br />
* Universal NULL object.<br />
*<br />
* Will throw no error messages so can be used as universal parameter.<br />
* Does overwrite all available magic methods used in PHP5.3.<br />
*<br />
* @author Julius Beckmann<br />
*/</span><br />
<span class="kw2">class</span> NullObject <span class="br0">&#123;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __destruct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __set<span class="br0">&#40;</span><span class="re0">$name</span><span class="sy0">,</span> <span class="re0">$value</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __get<span class="br0">&#40;</span><span class="re0">$name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="kw4">null</span><span class="sy0">;</span> <span class="br0">&#125;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __isset<span class="br0">&#40;</span><span class="re0">$name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="kw4">false</span><span class="sy0">;</span> <span class="br0">&#125;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __unset<span class="br0">&#40;</span><span class="re0">$name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __call<span class="br0">&#40;</span><span class="re0">$name</span><span class="sy0">,</span> <span class="re0">$args</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __toString<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="st_h">''</span><span class="sy0">;</span> <span class="br0">&#125;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __invoke<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">static</span> <span class="kw2">function</span> __callStatic<span class="br0">&#40;</span><span class="re0">$name</span><span class="sy0">,</span> <span class="re0">$args</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>GistHub:<br />
<a href="https://gist.github.com/1101526">https://gist.github.com/1101526</a></p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-universal-null-object.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Doing some cool things with references and destructors</title>
		<link>http://juliusbeckmann.de/blog/php-doing-some-cool-things-with-references-and-destructors.html</link>
		<comments>http://juliusbeckmann.de/blog/php-doing-some-cool-things-with-references-and-destructors.html#comments</comments>
		<pubDate>Sat, 18 Jun 2011 17:56:49 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Destructor]]></category>
		<category><![CDATA[Objects]]></category>
		<category><![CDATA[References]]></category>
		<category><![CDATA[Static Types]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=850</guid>
		<description><![CDATA[Not everybody of you might know, that there are references in PHP too, like in many other languages. I wondered if there is something these references might be really good for and came up with the following concept. References Maybe i should first tell you something about references in PHP: Normally, you do not use [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://juliusbeckmann.de/blog/static/php-logo.jpg" alt="php-logo" title="php-logo" width="100" height="55" class="alignright size-full wp-image-855" /><br />
Not everybody of you might know, that there are references in PHP too, like in many other languages. I wondered if there is something these references might be really good for and came up with the following concept.<br />
<span id="more-850"></span></p>
<h3>References</h3>
<p>Maybe i should first tell you something about references in PHP:<br />
Normally, you do not use any of them in your code. You need to use the "&#038;" Operator to create references.<br />
Maybe take a look at the following code to understand:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="re0">$a</span> <span class="sy0">=</span> <span class="st_h">'test'</span><span class="sy0">;</span><br />
<span class="re0">$b</span> <span class="sy0">=</span> <span class="sy0">&amp;</span> <span class="re0">$a</span><span class="sy0">;</span><br />
<span class="re0">$a</span> <span class="sy0">=</span> <span class="st_h">'abc'</span><span class="sy0">;</span><br />
<span class="kw1">echo</span> <span class="re0">$b</span><span class="sy0">;</span> <span class="co1">// Will output 'abc'</span></div>
</div>
<h3>Object Destructors</h3>
<p>Another important point to know is, when does PHP call the destructor of a object?<br />
PHP does reference counting, that means, if there is no reference pointing at a object, the garbage collector will destroy the object, but before he does so, the __destruct() method of the object will be called.<br />
Some example code for understanding:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">class</span> a <span class="br0">&#123;</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __destruct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">echo</span> <span class="st0">&quot;I go now, bye!&quot;</span><span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="re0">$a</span> <span class="sy0">=</span> <span class="kw2">new</span> a<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<a href="http://www.php.net/unset"><span class="kw3">unset</span></a><span class="br0">&#40;</span><span class="re0">$a</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// Will output &quot;I go now, bye!&quot;</span></div>
</div>
<h3>Concept</h3>
<p>If i combine the two things we know now, how about using references to do some normally impossible things? Like using something to manage references and use our destructor to manipulate the reference that pointed to us before we get destructed?</p>
<p>I realized this with some code you can find here: <a href="https://github.com/JuliusBeckmann/PHP-Reference-Fun">https://github.com/JuliusBeckmann/PHP-Reference-Fun</a></p>
<p>To do so, created the following classes:</p>
<p><strong>ReferenceManager</strong><br />
This static class will collect references, we might use again.</p>
<p><strong>ReferenceType</strong><br />
A abstract class that uses the ReferenceManager to manipulate the outside reference that pointed at us before we got destructed.</p>
<h3>Idea: Write protected objects</h3>
<p><strong>WriteProtected extends ReferenceType</strong><br />
This class uses the ReferenceType destructor call, to recreate itself via the outside reference fetched from ReferenceManager.</p>
<p>Take a look at the source code to fully understand: <a href="https://github.com/JuliusBeckmann/PHP-Reference-Fun/blob/master/ExampleWriteProtected.php">ExampleWriteProtected.php</a></p>
<h3>Idea: Static typed objects</h3>
<p><strong>AbstractStaticType extends ReferenceType</strong><br />
Another thing to know, when our destructor get called, there is no outside reference to out object any more. If there was only, the value it pointed to got overwritten by the new assigned value.<br />
This can be used to manipulate the new value to something we want to have.</p>
<p>Take a look at the source to fully understand: <a href="https://github.com/JuliusBeckmann/PHP-Reference-Fun/blob/master/ExampleStaticTypes.php">ExampleStaticTypes.php</a></p>
<h3>Conclusion</h3>
<p>This whole concept is just a idea proven with some experimental code.<br />
I think there could be several more useful things that can be done with this concept that might help in real life coding hell, but till now, i will just look at it as a neat idea to keep in mind for later.</p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-doing-some-cool-things-with-references-and-destructors.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Whiz: Simple cURL PHP wrapper class</title>
		<link>http://juliusbeckmann.de/blog/whiz-simple-curl-php-wrapper-class.html</link>
		<comments>http://juliusbeckmann.de/blog/whiz-simple-curl-php-wrapper-class.html#comments</comments>
		<pubDate>Wed, 04 May 2011 15:08:51 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[Whiz]]></category>
		<category><![CDATA[BSD]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[GitHub]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=816</guid>
		<description><![CDATA[I worked on a project where i needed to handle a lot with cURL request. At first i tried to use Zend_Http_Client, but it was too limited for my purpose. So i wrote my own cURL handler php class. I tried to keep the class as simple as possible, but it should be universal and [...]]]></description>
			<content:encoded><![CDATA[<p>I worked on a project where i needed to handle a lot with cURL request. At first i tried to use Zend_Http_Client, but it was too limited for my purpose. So i wrote my own cURL handler php class.<br />
<span id="more-816"></span></p>
<p>I tried to keep the class as simple as possible, but it should be universal and powerfull enough for easy handling. Some ideas i had before writing code:</p>
<h3>Thoughts</h3>
<ul>
<li>Methods should be named like the curl_* PHP functions</li>
<li>Using CURL* constants and values directly</li>
<li>External access to the handle</li>
<li>curl_multi_* possible</li>
<li>Straight forward, no extra logic where possible</li>
<li>Using all curl_* functions available</li>
</ul>
<h3>Code</h3>
<p>You can find the code on github;<br />
<a href="https://github.com/JuliusBeckmann/Whiz-Framework/blob/master/Whiz/Http/Client/Curl.php">https://github.com/JuliusBeckmann/Whiz-Framework/blob/master/Whiz/Http/Client/Curl.php</a><br />
License is new BSD.</p>
<h3>Examples</h3>
<p>Instead of explaining the code now, i just give a example using as much methods possible:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<span class="co1">// cURL options can be found here:</span><br />
<span class="co1">// http://php.net/manual/en/function.curl-setopt.php</span></p>
<p><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st_h">'Whiz/Http/Client/Curl.php'</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="co1">// Set cURL options via constructor</span><br />
<span class="re0">$curl</span> <span class="sy0">=</span> <span class="kw2">new</span> Whiz_Http_Client_Curl<span class="br0">&#40;</span><br />
&nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span>CURLOPT_REFERER <span class="sy0">=&gt;</span> <span class="st_h">'http://www.google.com/'</span><span class="br0">&#41;</span><br />
<span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="co1">// Set URL via method (This is just to make things easier)</span><br />
<span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">setUrl</span><span class="br0">&#40;</span><span class="st_h">'http://juliusbeckmann.de/'</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="co1">// $curl-&gt;exec('http://juliusbeckmann.de/'); would be also possible</span></p>
<p><span class="co1">// Set cURL options via method</span><br />
<span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">setOption</span><span class="br0">&#40;</span>CURLOPT_TIMEOUT<span class="sy0">,</span> 10<span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="co1">// Do the request</span><br />
<span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">exec</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">isError</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// Error</span><br />
&nbsp; <a href="http://www.php.net/var_dump"><span class="kw3">var_dump</span></a><span class="br0">&#40;</span><span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">getErrNo</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <a href="http://www.php.net/var_dump"><span class="kw3">var_dump</span></a><span class="br0">&#40;</span><span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">getError</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><span class="kw1">else</span><span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// Success</span><br />
&nbsp; <span class="kw1">echo</span> <span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">getResult</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="co1">// More info about the transfer</span><br />
&nbsp; <span class="co1">// var_dump($curl-&gt;getInfo());</span><br />
&nbsp; <span class="co1">// var_dump($curl-&gt;getHeader());</span><br />
&nbsp; <span class="co1">// var_dump($curl-&gt;getVersion());</span><br />
<span class="br0">&#125;</span></p>
<p><span class="co1">// Close cURL</span><br />
<span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
<h3>Advanced examples</h3>
<p>And some more advanced example code:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st_h">'Whiz/Http/Client/Curl.php'</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="co1">// Creating a &quot;template&quot; class by overwriting internal config</span><br />
<span class="kw2">class</span> My_Curl <span class="kw2">extends</span> Whiz_Http_Client_Curl <span class="br0">&#123;</span><br />
&nbsp; protected <span class="re0">$_config</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; CURLOPT_RETURNTRANSFER <span class="sy0">=&gt;</span> <span class="kw4">true</span><span class="sy0">,</span> <br />
&nbsp; &nbsp; CURLOPT_REFERER <span class="sy0">=&gt;</span> <span class="st_h">'http://www.google.com/'</span><br />
&nbsp; <span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="re0">$curl</span> <span class="sy0">=</span> <span class="kw2">new</span> My_Curl<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">setUrl</span><span class="br0">&#40;</span><span class="st_h">'http://juliusbeckmann.de/'</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="co1">// Fetch configured handle</span><br />
<span class="re0">$ch</span> <span class="sy0">=</span> <span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">getHandle</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="co1">// Fetch a copy of the configured handle</span><br />
<span class="co1">// $ch2 = $curl-&gt;copyHandle();</span></p>
<p><span class="co1">// Do with handle what ever you like</span><br />
<span class="co1">// ...</span><br />
<span class="re0">$result</span> <span class="sy0">=</span> <a href="http://www.php.net/curl_exec"><span class="kw3">curl_exec</span></a><span class="br0">&#40;</span><span class="re0">$ch</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="co1">// Put handle and result back in</span><br />
<span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">setFromHandle</span><span class="br0">&#40;</span><span class="re0">$ch</span><span class="sy0">,</span> <span class="re0">$result</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="co1">// Fetch transfer info </span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">isError</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// Error</span><br />
&nbsp; <a href="http://www.php.net/var_dump"><span class="kw3">var_dump</span></a><span class="br0">&#40;</span><span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">getErrNo</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <a href="http://www.php.net/var_dump"><span class="kw3">var_dump</span></a><span class="br0">&#40;</span><span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">getError</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><span class="kw1">else</span><span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// Success</span><br />
&nbsp; <span class="kw1">echo</span> <span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">getResult</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="co1">// More info about the transfer</span><br />
&nbsp; <span class="co1">// var_dump($curl-&gt;getInfo());</span><br />
&nbsp; <span class="co1">// var_dump($curl-&gt;getHeader());</span><br />
&nbsp; <span class="co1">// var_dump($curl-&gt;getVersion());</span><br />
<span class="br0">&#125;</span></p>
<p><span class="co1">// Close cURL</span><br />
<span class="re0">$curl</span><span class="sy0">-&gt;</span><span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/whiz-simple-curl-php-wrapper-class.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook: Like Buttons uses wrong url</title>
		<link>http://juliusbeckmann.de/blog/facebook-like-buttons-uses-wrong-url.html</link>
		<comments>http://juliusbeckmann.de/blog/facebook-like-buttons-uses-wrong-url.html#comments</comments>
		<pubDate>Mon, 11 Apr 2011 09:20:40 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Hack]]></category>
		<category><![CDATA[Patch]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=776</guid>
		<description><![CDATA[I use the digg-digg Wordpress Plugin on this blog and found a annoying error - the like button does not use the correct url. I found out why and how to correct it. What happened I wrote a new article and watched the preview of it. Digg-Digg showed the buttons on the preview page. I [...]]]></description>
			<content:encoded><![CDATA[<p>I use the <a href="http://wordpress.org/extend/plugins/digg-digg/">digg-digg Wordpress Plugin</a> on this blog and found a annoying error - the like button does not use the correct url. I found out why and how to correct it.<br />
<span id="more-776"></span></p>
<h2>What happened</h2>
<ul>
<li>I wrote a new article and watched the preview of it.</li>
<li>Digg-Digg showed the buttons on the preview page.</li>
<li>I published the article and tried the Like Button.</li>
<li>The Like Button did not use the correct url, in my case: <em>juliusbeckmann.de/article-name.html</em> instead of <em>juliusbeckmann.de/<strong>blog</strong>/article-name.html</em>.</li>
</ul>
<h2>Explained</h2>
<p>While watching the preview, die like button send the preview url to facebook. Now facebook tried to fetch the page, but was redirected because the article was not published yet. Facebook "guessed" a url and came up with "juliusbeckmann.de/" what got cached.<br />
Now i published the articled, used the like button, but facebook does not use the new url, they used the old guessed and cached url from the preview.</p>
<h2>Solution</h2>
<p>Solution is simple: Do not show the facebook like button on preview pages that will not be visibile for facebook.</p>
<h2>Hack for Digg-Digg</h2>
<p>Hacking Digg-Digg is simple. Open the file "digg-digg.php" and find the function "dd_hook_wp_content".<br />
Now replace this code:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">global</span> <span class="re0">$wp_query</span><span class="sy0">;</span><br />
<span class="re0">$post</span> <span class="sy0">=</span> <span class="re0">$wp_query</span><span class="sy0">-&gt;</span><span class="me1">post</span><span class="sy0">;</span> <span class="co1">//get post content</span></div>
</div>
<p>with this code:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">global</span> <span class="re0">$wp_query</span><span class="sy0">;</span><br />
<span class="co1">// Hack - No Digg-Digg on preview pages &nbsp;by JuliusBeckmann.de</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$wp_query</span><span class="sy0">-&gt;</span><span class="me1">is_preview</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">return</span> <span class="re0">$content</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><br />
<span class="re0">$post</span> <span class="sy0">=</span> <span class="re0">$wp_query</span><span class="sy0">-&gt;</span><span class="me1">post</span><span class="sy0">;</span> <span class="co1">//get post content</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/facebook-like-buttons-uses-wrong-url.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: List of common pitfalls</title>
		<link>http://juliusbeckmann.de/blog/php-list-of-common-pitfalls.html</link>
		<comments>http://juliusbeckmann.de/blog/php-list-of-common-pitfalls.html#comments</comments>
		<pubDate>Sun, 03 Apr 2011 14:25:29 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Secure]]></category>
		<category><![CDATA[Tipps]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=530</guid>
		<description><![CDATA[I have seen a lot of PHP code, good and bad ones. Even some exploits for pretty common things that were just careless implemented. In this post i want to share some of these pitfalls so you dont have to make them on your own. $_GET Vulerabilities str_replace() validation &#60;?php include&#40;str_replace&#40;'../','',$_GET&#91;'file'&#93;&#41;&#41;; ?&#62; The str_replace() validation [...]]]></description>
			<content:encoded><![CDATA[<p>I have seen a lot of PHP code, good and bad ones. Even some exploits for pretty common things that were just careless implemented.<br />
In this post i want to share some of these pitfalls so you dont have to make them on your own.<br />
<span id="more-530"></span></p>
<h2>$_GET Vulerabilities</h2>
<h3>str_replace() validation</h3>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<span class="kw1">include</span><span class="br0">&#40;</span><a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st_h">'../'</span><span class="sy0">,</span><span class="st_h">''</span><span class="sy0">,</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'file'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
<p>The str_replace() validation is still vulnerable against a directory change attack.<br />
Simple hack would be:<br />
<strong>index.php?file=....//somefile.php</strong><br />
<em>Explanation:</em><br />
str_replace() does not double replace. If you want to do so use code like this:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<span class="kw1">while</span><span class="br0">&#40;</span><a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'file'</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="st_h">'../'</span><span class="br0">&#41;</span> <span class="sy0">!==</span> <span class="kw4">FALSE</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'file'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st_h">'../'</span><span class="sy0">,</span><span class="st_h">''</span><span class="sy0">,</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'file'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><br />
<span class="co1">// Or</span><br />
<span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'file'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st_h">'\.+/+'</span><span class="sy0">,</span> <span class="st_h">''</span><span class="sy0">,</span> <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'file'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
<p>Another example for wrong str_replace() usage:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<span class="kw1">include</span><span class="br0">&#40;</span><a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st_h">'http://'</span><span class="sy0">,</span><span class="st_h">''</span><span class="sy0">,</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'file'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
<p>It is the same problem like above and easily hackable:<br />
<strong>index.php?file=hhttp://ttp://www.google.com/</strong></p>
<h3>Suffix/Prefix validation</h3>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<span class="kw1">include</span><span class="br0">&#40;</span><span class="st_h">'/path/'</span><span class="sy0">.</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'file'</span><span class="br0">&#93;</span><span class="sy0">.</span><span class="st_h">'.php'</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
<p>Adding a prefix and a suffix is good, but still vulnerable.<br />
Possible hack:<br />
<strong>index.php?file=../../../../etc/passwd%00</strong><br />
<em>Explanation:</em><br />
The "%00" is a nullbyte, so the internal representation of a zero. Everything behind the nullbyte will be ignored.<br />
The the real command will look like this:<br />
<strong>include('/path/../../../../etc/passwd');</strong><br />
On many systems this will include the /etc/passwd!</p>
<h3>SQL validation</h3>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st_h">'SELECT * FROM articles WHERE id = '</span><span class="sy0">.</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'id'</span><span class="br0">&#93;</span><span class="sy0">.</span><span class="st_h">' ;'</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
<p><strong>This is very dangerous!</strong> The $_GET value can be changed in the query and can give controll over the SQL query!<br />
A simple fix could look like this:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st_h">'SELECT * FROM articles WHERE id = '</span><span class="sy0">.</span><span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'id'</span><span class="br0">&#93;</span><span class="sy0">.</span><span class="st_h">' ;'</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
<p>Still not perfect but secure.</p>
<h2>More</h2>
<p>You have some more examples?<br />
Let me know them! Thanks.</p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-list-of-common-pitfalls.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Benchmark Inkrement und Dekrement</title>
		<link>http://juliusbeckmann.de/blog/php-benchmark-inkrement-und-dekrement.html</link>
		<comments>http://juliusbeckmann.de/blog/php-benchmark-inkrement-und-dekrement.html#comments</comments>
		<pubDate>Sun, 03 Apr 2011 14:21:55 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP5]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=311</guid>
		<description><![CDATA[Wir haben alle in der Schule das kleine EinMalEins gelernt, doch selbst bei den simpelsten Anweisungen in Programmiersprachen gibt es noch Wissenlücken über Verwendung und Laufzeitunterschiede. Im nachfolgenden Post wird mittels Benchmark ermittelt welche Art die schnellste ist eine Variable in PHP5 um 1 zu erhöhen (inkerment) oder zu verringern (decrement). Fangen wir erstmal damit [...]]]></description>
			<content:encoded><![CDATA[<p>Wir haben alle in der Schule das kleine EinMalEins gelernt, doch selbst bei den simpelsten Anweisungen in Programmiersprachen gibt es noch Wissenlücken über Verwendung und Laufzeitunterschiede.<br />
Im nachfolgenden Post wird mittels Benchmark ermittelt welche Art die schnellste ist eine Variable in PHP5 um 1 zu erhöhen (inkerment) oder zu verringern (decrement).<br />
<span id="more-311"></span><br />
Fangen wir erstmal damit an aufzulisten was es alles für Möglichkeiten dafür gibt. Ich beziehe mich dazu auf die Möglichkeiten des inkrementieren, das dekrementieren funktioniert analog dazu.</p>
<h2>Möglichkeiten:</h2>
<p>Einfache Neuzuweisung des inkrementierten Werts:</p>
<div class="codesnip-container" >$i = $i + 1;</div>
<p>Der Wert von $i wird genommen, um eins erhöht und dieser Wert dann wieder in die Variable $i geschrieben.</p>
<div class="codesnip-container" >$i = 1 + $i;</div>
<p>Die 1 wird genommen und der Wert von $i dazuaddiert. Dieser Wert wird dann in$ i gespeichert. Achtung beim dekrement, hier entsteht ein Vorzeichenwechsel.</p>
<div class="codesnip-container" >$i += 1;</div>
<p>Ein Kurzschreibweise für $i = $i + 1;</p>
<div class="codesnip-container" >++$i;</div>
<p>Der Variable $i wird eins hinzuaddiert. ++$i wird auch Pre-Inkrement genannt, da es (pre = vor) vor der Verwendung angewendet wird. Erklärung was "pre" genau bedeutet:<br />
$i = 1; echo $array[++$i]; =&gt; Es wird auf den Wert von $array[2] zugegriffen. $i hat danach den Wert 2.</p>
<div class="codesnip-container" >$i++;</div>
<p>Der Variable $i wird eins hinzuaddiert. $i++ wird auch Post-Inkrement genannt, da es (post = nach) nach der Verwendung angewendet wird. Erklärung was "post" genau bedeutet:<br />
$i = 1; echo $array[$i++]; =&gt; Es wird auf den Wert von $array[1] zugegriffen. $i hat danach den Wert 2.</p>
<h2>Benchmark:</h2>
<p>Welche der Möglichkeiten ist die schnellste?<br />
Für den Benchmark habe ich diesen Code verwendet:<br />
--- wird nachgereicht ---</p>
<p>Als Ergebnis habe ich auf meinem Eee mit Atom N280 und Ubuntu 9.04 bekommen:</p>
<div class="codesnip-container" >PHP-Version: 5.2.6-3ubuntu4.1<br />
Iterator Benchmark with 1000000 iterations<br />
------------------------------------<br />
--- Inkrement ---<br />
0.3493 Seconds for ++$i ;<br />
0.3806 Seconds for $i += 1 ;<br />
0.3887 Seconds for $i++ ;<br />
0.4603 Seconds for $i = 1 + $i ;<br />
0.4699 Seconds for $i = $i + 1 ;<br />
--- Dekrement ---<br />
0.3370 Seconds for --$i ;<br />
0.3767 Seconds for $i -= 1 ;<br />
0.4348 Seconds for $i-- ;<br />
0.4741 Seconds for $i = $i - 1 ;</div>
<h2>Ergebnis:</h2>
<p>Man kann deutlich erkennen dass die normalen Wertezuweisungen mit $i + 1 langsamer sind als die Post/Pre oder Kurzsyntax-Varianten.<br />
Bei den beiden langsameren Varianten muss der Computer zusätzliche Lade und Speicheroperationen machen welche bei den schnelleren drei Varianten entfallen können. Dazu gibt es meist eine in- und decrement Funktionen in Machinensprache welche dies nochmal wesentlich beschleunigen kann.</p>
<p>Wenn man mit der Post, Pre oder Kurzvariante Variante arbeiten kann sollte man diese immer bevorzugen. Es gibt auch Optimizer für PHP die teils an solchen Stellen ansetzten um noch etwas mehr Geschwindigkeit zu erreichen.<br />
Mit diesen Ergebnissen im Hinterkopf könnte die nächste Schleife ein bisschen schneller werden.</p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-benchmark-inkrement-und-dekrement.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Everything you need to know about secure password hashing</title>
		<link>http://juliusbeckmann.de/blog/php-everything-you-need-to-know-about-secure-password-hashing.html</link>
		<comments>http://juliusbeckmann.de/blog/php-everything-you-need-to-know-about-secure-password-hashing.html#comments</comments>
		<pubDate>Sun, 03 Apr 2011 14:17:10 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Hashing]]></category>
		<category><![CDATA[MD5]]></category>
		<category><![CDATA[Password]]></category>
		<category><![CDATA[SHA1]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=414</guid>
		<description><![CDATA[On each site you need to register, your password has to be saved. This is typically done in a database. There are several ways to do this and some of them are good practice, other a very bad idea. Lets start with the baaad examples: The user we have is named "Bob", he likes to [...]]]></description>
			<content:encoded><![CDATA[<p>On each site you need to register, your password has to be saved. This is typically done in a database. There are several ways to do this and some of them are good practice, other a very bad idea.<br />
<span id="more-414"></span></p>
<p>Lets start with the baaad examples:</p>
<p>The user we have is named "Bob", he likes to use his password "donuts".</p>
<h2>Force users to use secure passwords</h2>
<p>This is the first point where we have to interact. It does not matter how good our system will encrypt or hash Bobs password, with a simple one like this it does not make sense at all. We need to force Bob to use a more secure password. Good password contain of upper and lowercase letters, numbers and maybe even special chars. But this is not the subject of this article, so we continue with our bad examples.</p>
<p>We forced Bob to use a more secure password like "IlikeD0nuts".</p>
<h2>Plain text passwords</h2>
<p>The simplest way to store and validate Bobs password would be saving it "as is". The only good thing about this method is, we can send him his password back in case he forgot it. (Hint: If you ever use a "Remember password" function and they send your plain password back, the used some kind of this bad technique.)<br />
The bad thing about this is, if a attacker gets read access to the database, he can read out Bobs password. The attacker is now able to login as Bob and do whatever he wants with Bobs account. If Bob has used the password also for his Email account or anything else the attacker can find out, Bob might get in serious trouble.<br />
As you can see, storing plain text passwords is very bad and should NOT be done.</p>
<h2>MD5 / SHA1 passwords</h2>
<p>The next better way to store a password, is NOT to store the password. Yes, do NOT store the password, store something that is equivalent to the password but cant be reconverted to the password again.<br />
This one way encryption method is called "hashing". Most famous and versatile hashing methods are <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> and <a href="http://en.wikipedia.org/wiki/SHA_hash_functions">SHA1</a>. Both can do a one way encryption like this: The MD5 of the string "abc" is: md5('abc') = 900150983cd24fb0d6963f7d28e17f72. The MD5 of "abc" will <strong>always</strong> be 900150983cd24fb0d6963f7d28e17f72. But if only one char changes, the MD5 will also change. md5('abd') = 4911e516e5aa21d327512e0c8b197616. So this function can create a hash out of a string which is "unique" for this string. But there is NO function like this: unmd5('4911e516e5aa21d327512e0c8b197616') = "abd" because it is a one way encryption. Like a one way flight, you cant get back home when you are at your destination.<br />
You can play around a little bit with these function on: <a href="http://php-functions.com/">http://php-functions.com/</a><br />
This feature of a hash can be used to validate a password. So if Bob registers his new account, we do NOT save his password "donuts" in our database, we save md5('donuts') = 6c493f3632cf8c85348ebd89d1e3cafa. A attacker cant see directly what 6c493f3632cf8c85348ebd89d1e3cafa means. If Bob wants to login, we just have to check if md5($password_bob_entered) = 6c493f3632cf8c85348ebd89d1e3cafa. When it is true, Bob must have entered "donuts".<br />
This way of saving password is the first step in the right direction, but still no good practice. The problem is, that a MD5 will always be the same for a specific string, this hole is used by several online pages and programs to reverse a md5. Just search for "md5 cracker" and you will find tons of ways for cracking MD5 or SHA1 hashes. This plus the fatal use of simple passwords like "donuts" (<a href="http://www.google.com/search?q=6c493f3632cf8c85348ebd89d1e3cafa">Google: "donuts"</a>) will make cracking only a matter of time. So once again, storing simple MD5 or SHA1 hashes is NOT secure.</p>
<h2>MD5 / SHA1 hashes with salt</h2>
<p>This technique is similar to the previous, with one difference, store something additionally called "salt". Like salt in a soup this extra string will make cracking password catchier. The procedure goes like this:<br />
Bob registers with his password "donuts". We generate a random(!) salt, like "8ft$U3", and will hash his password like this:  md5("donuts"."8ft$U3") = 9aaa94551048442a835f5ae875687714. Now we save his name, the salt and the salted password hash in our database. The clever ones of you might have noticed, we just made a secure password out of the simple "donuts". This technique is popular today and sometimes called "good practice". But this is dead wrong. In times of multicore CPUs and incredible fast graphic cards, breaking such a salted hash needs, if the password is not too complex, only a few hours. Current highend graphic cards are able to calculate billions of md5 per second, so breaking thousands of hashes is only a matter of time before a attacker can start his raid against all the cracked user account. So do NOT use simple salted hashes.</p>
<h2>MD5 / SHA1 hashes with salt hashed multiple times</h2>
<p>As we learned from the previous technique, cracking hashes is only a matter of time. So what takes cracking hashes longer? It is complexity. We can add these complexity by rehashing the hash again and again till we reached a level where cracking such a hash will take ages.<br />
I wrote this PHP class for demonstration, you can use it wherever you want, but keep the copyright header:</p>
<p><a href="/classes/secure_hash/_secure_hash---secure_hash.class.php.html">PHP Secure Hash Class</a></p>
<p>The class is using everything what makes a hash complex enough to save it from being cracked. Salts are completely generated from special chars. Salts are random. Hashes are rehashed random multiple times. The whole hash is stored in a special format which can be saved easily in a database. The format looks like this: <strong>$method$salt$iterations$hash$</strong>.</p>
<h2>Ideas for even more secure hashes</h2>
<p>I wrote about storing hashes in a database. The common way nowadays for an attacker is to find a SQL injection to read out your database. This is a major hole in security of websites. The whole password verification data is stored inside a database. My idea is to add another vector to this data which is not saved inside the database. Something like a global salt for all password hashes. This way, a attacker needs access to the database <strong>and</strong> to the source of the config files.<br />
This idea is also implemented in my secure_hash php class.</p>
<h2>Pitfalls of password hashing</h2>
<p>There a a few pitfalls you have to care about when using hashes.</p>
<p>First, the charset of the password string is important. You have to understand how a hashing function works. It is not just randomizing a string and producing some chars. A hashing function does NOT work with chars, it works with their byte interpretation. It is also possible to create a <a href="http://php.net/md5_file">MD5 from a file</a>. So if your website is using the LATIN charset, your passwords will be hashed with the LATIN byte interpretation of these chars. If you use UTF8, these hashes can be completely different! A example: the umlaut 'ä' is in LATIN "961fa22f61a56e19f3f5f8867901ac8cf5e6d11f" but in UTF8 is is "89d39ea0600180286a2f794d8b2c4d5b02450ed9". Keep this in mind if you want to change the charset of your site.</p>
<p>Check for system chars! If i say system chars, i mean things like blanks, tabs, newlines and carriage returns. For example the string 'a' will be this SHA1: "89d39ea0600180286a2f794d8b2c4d5b02450ed9", but the string 'a ' will be "9384a39d69d104db5d1db7ccceae2ce0cb6c01c2".<br />
To reduce this risk use the <a href="http://php.net/trim">trim() function</a> to remove this chars from the passwords.</p>
<h2>Other Algorithms than MD5 or SHA1 (Update)</h2>
<p><em>Jani Hartikainen</em> mentioned in his <a href="#comment-856">comment</a>, that there are more algorithms than just md5 and sha1 and some of them are very slow compared to md5.<br />
That is a good point, which has some up and downsides.</p>
<p>Using a uncommon algorithm brings more security, because a attacker might not know the algorithm or have a tool for cracking it. Also is a slower hashing routine a problem for the attacker.</p>
<p>There might be a problem with uncommon algorithms, if you want to move to an other hoster, he might not support your hashing routine.</p>
<p>If you want to use something else then MD5 or SHA1, just have a look at <a href="http://www.php.net/manual/en/mhash.constants.php">MHash</a> or <a href="http://www.php.net/manual/en/function.hash-algos.php">Hash</a> PHP modules.</p>
<p>If you want to use your own hashing method, my <a href="/classes/secure_hash/_secure_hash---secure_hash.class.php.html">PHP Secure Hash Class</a> supports it. Just have a look at the example class "secure_hash_example".</p>
<p>Personally, i would not use something like whirlpool, tiger or haval, because they create dependencies which make the whole system more complex. I would let the simplicty win here.</p>
<p>I hope you learned how to store passwords secure and will make the Internet a safer place.<br />
<br/><br />
<br/></p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-everything-you-need-to-know-about-secure-password-hashing.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
