<?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>The Sapience Society &#187; PHP</title> <atom:link href="http://blog.lococobra.com/tag/php/feed" rel="self" type="application/rss+xml" /><link>http://blog.lococobra.com</link> <description>The advice of a self-proclaimed technological ninjician</description> <lastBuildDate>Thu, 08 Dec 2011 19:41:17 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Dynamically Self-Calling PHP Functions/Methods (For Recursion)</title><link>http://blog.lococobra.com/dynamically-self-calling-php-functionsmethods-for-recursion</link> <comments>http://blog.lococobra.com/dynamically-self-calling-php-functionsmethods-for-recursion#comments</comments> <pubDate>Fri, 31 Dec 2010 06:06:28 +0000</pubDate> <dc:creator>Jeff</dc:creator> <category><![CDATA[Code Magic]]></category> <category><![CDATA[Call]]></category> <category><![CDATA[Calling]]></category> <category><![CDATA[Dynamic]]></category> <category><![CDATA[Function]]></category> <category><![CDATA[Method]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Recursion]]></category> <category><![CDATA[Self]]></category> <guid
isPermaLink="false">http://blog.lococobra.com/?p=414</guid> <description><![CDATA[Okay, I know this is silly, and I doubt anyone will even get into the situation where they'd need to do this. (And if they do find themselves needing it.. I'm guessing they could figure it out on their own) ANYWAYS.. I have a function that calls itself recursively, I was going to rename the [...]]]></description> <content:encoded><![CDATA[<p>Okay, I know this is silly, and I doubt anyone will even get into the situation where they'd need to do this. (And if they do find themselves needing it.. I'm guessing they could figure it out on their own) ANYWAYS..</p><p>I have a function that calls itself recursively, I was going to rename the function which of course meant I would also have to change all self-calls. This caused me to wonder.. Can this be done dynamically?</p><p>Ten minutes of googling later, nothing found. Time to cobble some random ideas together!</p><pre class="brush: php; gutter: true; title: ; notranslate">$self_caller = __FUNCTION__;
$self_caller($args); //For functions
$this-&gt;$self_caller($args); //For methods</pre><p>That's it! I'm not sure I would recommend doing this at all, seems hackish... but all concepts used here are documented on the php.net site: <a
href="http://php.net/manual/en/functions.variable-functions.php" target="_blank">Variable functions</a>, <a
href="http://php.net/manual/en/language.constants.predefined.php">Magic constants</a></p><div
class="shr-publisher-414"></div>]]></content:encoded> <wfw:commentRss>http://blog.lococobra.com/dynamically-self-calling-php-functionsmethods-for-recursion/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Automagically Load Class Files in PHP</title><link>http://blog.lococobra.com/automagically-load-class-files-in-php</link> <comments>http://blog.lococobra.com/automagically-load-class-files-in-php#comments</comments> <pubDate>Fri, 06 Aug 2010 05:59:16 +0000</pubDate> <dc:creator>Jeff</dc:creator> <category><![CDATA[Code Magic]]></category> <category><![CDATA[Classes]]></category> <category><![CDATA[OOP]]></category> <category><![CDATA[PHP]]></category> <guid
isPermaLink="false">http://blog.lococobra.com/?p=366</guid> <description><![CDATA[If you have very much experience with writing medium/large PHP based projects, you've probably run into the following annoyance: You have a bunch of classes that you use ubiquitously, some which you wrote and some which others have written. Each file has a single class in it, but since the classes are from difference sources, [...]]]></description> <content:encoded><![CDATA[<p>If you have very much experience with writing medium/large PHP based projects, you've probably run into the following annoyance:</p><p>You have a bunch of classes that you use ubiquitously, some which you wrote and some which others have written. Each file has a single class in it, but since the classes are from difference sources, some of their files have non-consistent naming conventions. You have to include classes into the files which use them, so you might include all of the classes in a header file, but that will cause unnecessary overhead because even unneeded classes get included in ever file which uses the header at all. The other option is to individually include only the necessary classes into the files that use them, but what a pain in the ass!</p><p>Fortunately there is a third option, available in PHP 5.0+; it's called <a
title="Autoloading Classes" href="http://php.net/manual/en/language.oop5.autoload.php" target="_blank">Autoloading Classes</a>. Basically, as soon as an object of a class is created for which the source file has not yet been included a function called <em>__autoload</em> is called. Using that function, we can then include the appropriate class file just before the object is created. Thus avoiding including any additional classes via a header or having to type out extra includes. So how does this magical function work?<br
/> <span
id="more-366"></span><br
/> Here's a simple example from the PHP site:</p><pre class="brush: php; gutter: false; title: ; notranslate">function __autoload($class_name) {
    require_once $class_name . '.php';
}</pre><p>Not bad, but rather restrictive. All it does is attempts to include a file called whateverYourClassIsCalled.php. If that file doesn't exist.. FATAL ERROR! Here is my version, which (at least to me) seems much more robust:</p><pre class="brush: php; gutter: false; title: ; notranslate">function __autoload($Name)
{
    $seperators = array('.', '-', '', '_');
    $namingConventions = array(
        'class[SEP]'.$Name,
        $Name.'[SEP]class',
        $Name,
    );
    $includePath = array(
        '/includes/',
        '/secondary/path/'
    );
    foreach ($includePath as $path)
        foreach ($seperators as $sep)
            foreach ($namingConventions as $convention)
                if (is_file($file = $_SERVER['DOCUMENT_ROOT'].$path.str_replace('[SEP]', $sep, $convention).'.php'))
                    include_once ($file);
}</pre><p>My function searches for any of the following files inside any pre-specified includes folder.<br
/> <em>class.Example.php<br
/> Example.class.php<br
/> Example.php<br
/> class-Example.php<br
/> Example-class.php<br
/> Example.php<br
/> classExample.php<br
/> Exampleclass.php<br
/> Example.php<br
/> class_Example.php<br
/> Example_class.php<br
/> Example.php</em></p><p>Efficiency may take a small hit (a ten-thousandth of a second on my machine for each additional path) if you specify multiple include folders, but I think the ease of never having to specify your class files is well worth it. Also, it's worth noting that my function is completely case-insensitive, trust me on that one.</p><div
class="shr-publisher-366"></div>]]></content:encoded> <wfw:commentRss>http://blog.lococobra.com/automagically-load-class-files-in-php/feed</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Generate Javascript Bookmarklets Using A Simple PHP Function</title><link>http://blog.lococobra.com/generate-javascript-bookmarklets-using-a-simple-php-function</link> <comments>http://blog.lococobra.com/generate-javascript-bookmarklets-using-a-simple-php-function#comments</comments> <pubDate>Wed, 09 Jun 2010 19:42:52 +0000</pubDate> <dc:creator>Jeff</dc:creator> <category><![CDATA[Code Magic]]></category> <category><![CDATA[base64]]></category> <category><![CDATA[Bookmarklet]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[PHP]]></category> <guid
isPermaLink="false">http://blog.lococobra.com/?p=327</guid> <description><![CDATA[I wrote a pretty simple PHP function for converting Javascript into a bookmarklet. As you'll see, it's nothing too crazy. I did come up with a kind of interesting way of shortening the source using base 64. I don't know if I'm the first person to do this, but I came up with the idea [...]]]></description> <content:encoded><![CDATA[<p>I wrote a pretty simple PHP function for converting Javascript into a bookmarklet. As you'll see, it's nothing too crazy. I did come up with a kind of interesting way of shortening the source using base 64. I don't know if I'm the first person to do this, but I came up with the idea on my own (no plagiarism!). Unfortunately, it's not IE compatible.. so there's a switch to turn that feature off (if you want). <span
id="more-327"></span></p><p>Here's the PHP function:</p><pre class="brush: php; title: ; notranslate">function Bookmarkletize($source, $ieCompatible = false){
	$source = preg_replace('~[\t\r\n]~', '', $source);
	$source1 = 'javascript:'.rawurlencode($source);
	if($ieCompatible) return $source1;
	$source2 = 'javascript:eval(atob(\''.base64_encode($source).'\'))';
	if(strlen($source1) &lt; strlen($source2))
		return $source1;
	else
		return $source2;
}</pre><p>As an example, you can try "Bookmarkletizing" the source for the form hijacker I previously posted about. Here's what that would look like:</p><pre class="brush: php; title: ; notranslate">$source = &lt;&lt;&lt;source
var index=prompt('Form index?', '0');
if(confirm('The action url is:\\n'+document.forms[index].action.substr(0,100)+'\\n\\nDoes that look right?')){
	document.forms[index].action='https://lococobra.com/showreq.php';
	document.forms[index].submit();
}
source;
echo Bookmarkletize($source);</pre><p>(syntax might look funny, it's just because the PHP highlighting wordpress plugin I'm using doesn't support Heredoc string quoting)</p><p>Hope this is useful for someone!</p><div
class="shr-publisher-327"></div>]]></content:encoded> <wfw:commentRss>http://blog.lococobra.com/generate-javascript-bookmarklets-using-a-simple-php-function/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Form Request Hijacker Javascript Bookmarklet</title><link>http://blog.lococobra.com/form-request-hijacker-javascript-bookmarklet</link> <comments>http://blog.lococobra.com/form-request-hijacker-javascript-bookmarklet#comments</comments> <pubDate>Sat, 05 Jun 2010 00:06:09 +0000</pubDate> <dc:creator>Jeff</dc:creator> <category><![CDATA[Code Magic]]></category> <category><![CDATA[cURL]]></category> <category><![CDATA[forms]]></category> <category><![CDATA[hijack]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[PHP]]></category> <guid
isPermaLink="false">http://blog.lococobra.com/?p=322</guid> <description><![CDATA[The idea: Lets say you want to use some service hosted at another site, but there isn't an API or anything.. just a user-accessible form. Well, in order to use that form, you're going to have to use something like cURL to submit some POST variables or something to the form handler. Here's the problem: [...]]]></description> <content:encoded><![CDATA[<p>The idea: Lets say you want to use some service hosted at another site, but there isn't an API or anything.. just a user-accessible form. Well, in order to use that form, you're going to have to use something like cURL to submit some POST variables or something to the form handler.</p><p>Here's the problem: What information is actually getting submitted with the form?</p><p>The only way to form your curl request is if you know every single bit of data that's getting sent to the form. You could do this with a tool like the "Web Developer" add-on for Firefox, but then if there's some onsubmit javascript that affects the form in any way, you don't get those changes.</p><p>My solution: Using javascript, hijack the form and have it submit to a custom URL which will then tell you any variables sent through the headers.<span
id="more-322"></span></p><p>The original javascript:</p><pre>var index=prompt('Form index?', '0');
if(confirm('The action url is:\\n'+document.forms[index].action.substr(0,100)+'\\n\\nDoes that look right?')){
	document.forms[index].action='https://lococobra.com/showreq.php';
	document.forms[index].submit();
}</pre><p>As you (maybe) can see, the code targets the Xth form on the page you're currently on, tells you where the form submits to (so you can submit to that url in cURL), and finally, submits the form to another script located at 'http://trappar.net/showreq.php'.</p><p>Lets make that into a bookmarklet! (Take a look <a
href="http://blog.lococobra.com/generate-javascript-bookmarklets-using-a-simple-php-function">at this</a> to see how I made it)</p><pre>javascript:eval(atob('dmFyIGluZGV4PXByb21wdCgnRm9ybSBpbmRleD8nLCAnMCcpO2lmKGNvbmZpcm0oJ1RoZSBhY3Rpb24gdXJsIGlzOlxuJytkb2N1bWVudC5mb3Jtc1tpbmRleF0uYWN0aW9uLnN1YnN0cigwLDEwMCkrJ1xuXG5Eb2VzIHRoYXQgbG9vayByaWdodD8nKSl7ZG9jdW1lbnQuZm9ybXNbaW5kZXhdLmFjdGlvbj0naHR0cHM6Ly9sb2NvY29icmEuY29tL3Nob3dyZXEucGhwJztkb2N1bWVudC5mb3Jtc1tpbmRleF0uc3VibWl0KCk7fQ=='))</pre><p>Here's that in a link, if that helps: <a
href="javascript:eval(atob('dmFyIGluZGV4PXByb21wdCgnRm9ybSBpbmRleD8nLCAnMCcpO2lmKGNvbmZpcm0oJ1RoZSBhY3Rpb24gdXJsIGlzOlxuJytkb2N1bWVudC5mb3Jtc1tpbmRleF0uYWN0aW9uLnN1YnN0cigwLDEwMCkrJ1xuXG5Eb2VzIHRoYXQgbG9vayByaWdodD8nKSl7ZG9jdW1lbnQuZm9ybXNbaW5kZXhdLmFjdGlvbj0naHR0cHM6Ly9sb2NvY29icmEuY29tL3Nob3dyZXEucGhwJztkb2N1bWVudC5mb3Jtc1tpbmRleF0uc3VibWl0KCk7fQ=='))">Bookmarklet</a></p><p>Now what does that new action URL do? It shows you all of the header variables that were sent through the form. Here's the code for that page (PHP)</p><pre>&lt;?
$vars = array('$_GET', '$_POST', '$_REQUEST', '$_FILES');
foreach($vars as $var)
	eval('if(count('.$var.') &gt; 0) echo \'&lt;h3&gt;'.$var.'&lt;/h3&gt;&lt;pre&gt;\'.var_export('.$var.',true).\'&lt;/pre&gt;\';');
?&gt;</pre><p>Voila! With one click you can take over any form and find out exactly what it's doing.</p><div
class="shr-publisher-322"></div>]]></content:encoded> <wfw:commentRss>http://blog.lococobra.com/form-request-hijacker-javascript-bookmarklet/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>PHP Variable Variables and Reference Arrays in Use</title><link>http://blog.lococobra.com/php-variable-variables-reference-arrays</link> <comments>http://blog.lococobra.com/php-variable-variables-reference-arrays#comments</comments> <pubDate>Wed, 23 Sep 2009 18:37:27 +0000</pubDate> <dc:creator>Jeff</dc:creator> <category><![CDATA[Code Magic]]></category> <category><![CDATA[array]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[reference]]></category> <guid
isPermaLink="false">http://blog.lococobra.com/?p=220</guid> <description><![CDATA[Something I haven't talked about a whole lot on here is that I'm actually a professional PHP developer. It's my job, so usually I don't mix work and online stuff. This is pretty cool though, so I want people to be able to find it if they're trying to figure out how to do it. [...]]]></description> <content:encoded><![CDATA[<p>Something I haven't talked about a whole lot on here is that I'm actually a professional PHP developer. It's my job, so usually I don't mix work and online stuff. This is pretty cool though, so I want people to be able to find it if they're trying to figure out how to do it. Be warned, if you're not a PHP coder the following post will make ZERO sense to you <img
src='http://blog.lococobra.com/wp-includes/images/smilies/icon_razz.gif' alt="icon razz PHP Variable Variables and Reference Arrays in Use" class='wp-smiley' title="PHP Variable Variables and Reference Arrays in Use" /></p><p>Before I get into what a "reference array" is (I made that term up) let me guide you through the discovery and some practical uses of the theory around references. A while ago I was looking for a way to do something in code that didn't seem very possible. I wanted to do this:</p><pre class="brush: php; title: ; notranslate">$var1 = 'Hello World';
echo $&quot;var1&quot;;</pre><p>Problem is, that doesn't work.. instead I found that you can use variable variables (also known as dynamic variables)<br
/> <span
id="more-220"></span></p><h3>Variable Variables</h3><pre class="brush: php; title: ; notranslate">$var1 = 'Hello World';
$var2 = 'var1';
echo $$var2;
Output: Hello World</pre><p>So how does that code work? PHP parses the inside string and evaluates which outside string to call.</p><pre class="brush: php; title: ; notranslate">IF: $var1 = 'Hello World';
$var2 = 'var1';
THEN: $var1 == $$var2</pre><p>But what's the practical use? Why on earth would we call a variable that way? When could there possibly be a situation where you don't already know the name of the variables you're accessing? I'll tell you with an example, it's one of my favorite pieces of code I've ever written. It's short and elegant and would probably be used by 80% of developers if they knew about it.</p><pre class="brush: php; title: ; notranslate">
foreach($_REQUEST as $key=&gt;$elem)
	$$key = $elem;</pre><p>That code takes every $_GET, $_POST, and $_COOKIE variable and converts it like this:</p><pre class="brush: php; title: ; notranslate">IF: $_POST['foo'] = 'bar';
THEN: $foo = 'bar';</pre><p>Form processing just became so much easier! Plus if you want you can add trim and stripslashes to make form processing even easier...</p><pre class="brush: php; title: ; notranslate">foreach($_REQUEST as $key=&gt;$elem)
	$$key = trim(stripslashes($elem));</pre><p>This is all well and good, but there's still a completely different way to do all this.</p><h3>Using References</h3><pre class="brush: php; title: ; notranslate">$var1 = 'Hello World';
$var2 = &amp;$var1;
echo $var2;
OUTPUT: Hello World</pre><p>In that code, $var2 is created as a reference to $var1, meaning the two can be used interchangeably. You can reassign the value of either $var1 or $var2 and both variables will reflect the change. What's interesting is, references can not be used to do the code I wrote earlier! Anyways, one more example before we get to reference arrays.</p><pre class="brush: php; title: ; notranslate">$var1 = 'Hello';
$var2 = ', ';
$var3 = 'World';
$varArr = array(&amp;$var1, &amp;$var2, &amp;$var3);
$var1 = 'Goodbye';
$var2 = ' Cruel ';
foreach($varArr as $item)
	echo $item;
OUTPUT: Goodbye Cruel World</pre><p>What's important here? You can use references to create an array which will always reflect the current information being stored in other variables. This can still be replicated by the double string approach though. Example:</p><pre class="brush: php; title: ; notranslate">$var1 = 'Hello';
$var2 = ', ';
$var3 = 'World';
$varArr = array('var1', 'var2', 'var3');
$var1 = 'Goodbye';
$var2 = ' Cruel ';
foreach($varArr as $item)
	echo $$item;
OUTPUT: Goodbye Cruel World</pre><p> So by now if you're still even reading you're probably thinking "Why even use this stuff? What's the point? Especially what's the point of reference arrays?"</p><h3>Using Reference Arrays</h3><p>Lets say have several arrays filled with similar content, and we need to do some processing to the data in those arrays. You could combine the arrays, but what if you need them to be separate? I'll leave you with this rather simple example. If you have questions please post comments and I'll answer them.</p><pre class="brush: php; title: ; notranslate">
$fruits = array(' appLE', 'pear3', 'banana--');
$vegetables = array('pea', 'broccoli   ');
$processArr = array(&amp;$fruits, &amp;$vegetables);
foreach($processArr as &amp;$array)
	foreach($array as &amp;$item)
	{
		$item = preg_replace('/[^a-z]/i', '', $item);
		$item = ucwords(strtolower($item));
	}
echo '&amp;lt;pre&gt;';
print_r($fruits);
print_r($vegetables);
</pre><div
class="shr-publisher-220"></div>]]></content:encoded> <wfw:commentRss>http://blog.lococobra.com/php-variable-variables-reference-arrays/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
