<?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>Kevin Pajak - Interactive Web Developer/Designer - BLOG &#187; code</title>
	<atom:link href="http://www.kevinpajak.com/blog/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kevinpajak.com/blog</link>
	<description>Creative visionary, digital entrepreneur.</description>
	<lastBuildDate>Tue, 13 Apr 2010 02:01:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to delete entire folders/sub-folders (with all files contained within) in PHP</title>
		<link>http://www.kevinpajak.com/blog/2010/03/31/how-to-delete-entire-folderssub-folders-with-all-files-contained-within-in-php/</link>
		<comments>http://www.kevinpajak.com/blog/2010/03/31/how-to-delete-entire-folderssub-folders-with-all-files-contained-within-in-php/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 08:40:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Dev/Design]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.kevinpajak.com/blog/?p=395</guid>
		<description><![CDATA[Thanks to PeejAvery @CodeGuru.com for turning me on to this one (with the original code here). Here&#8217;s the code: function rrd($directory, $empty=FALSE){ if(substr($directory,-1) == &#8216;/&#8217;){$directory = substr($directory,0,-1);} if(!file_exists($directory) &#124;&#124; !is_dir($directory)){return FALSE;} elseif(!is_readable($directory)){return FALSE;} else{ $handle = opendir($directory); while (FALSE !== ($item = readdir($handle))){ if($item != &#8216;.&#8217; &#38;&#38; $item != &#8216;..&#8217;){ $path = $directory.&#8217;/&#8217;.$item; if(is_dir($path)){rrd($path);} else{unlink($path);} [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to PeejAvery @<a href="http://www.codeguru.com/forum/archive/index.php/t-366823.html" target="_blank"><b>CodeGuru.com</b></a> for turning me on to this one (with the <a href="http://lixlpixel.org/recursive_function/php/recursive_directory_delete/" target="_blank">original code here</a>).</p>
<p>Here&#8217;s the code:<span id="more-395"></span></p>
<p>function rrd($directory, $empty=FALSE){<br />
if(substr($directory,-1) == &#8216;/&#8217;){$directory = substr($directory,0,-1);}<br />
if(!file_exists($directory) || !is_dir($directory)){return FALSE;}<br />
elseif(!is_readable($directory)){return FALSE;}<br />
else{<br />
$handle = opendir($directory);<br />
while (FALSE !== ($item = readdir($handle))){<br />
if($item != &#8216;.&#8217; &amp;&amp; $item != &#8216;..&#8217;){<br />
$path = $directory.&#8217;/&#8217;.$item;<br />
if(is_dir($path)){rrd($path);}<br />
else{unlink($path);}<br />
}<br />
}<br />
closedir($handle);<br />
if($empty == FALSE){if(!rmdir($directory)){return FALSE;}}<br />
return TRUE;<br />
}<br />
}</p>
<p>(Just make sure you add a direct call to the PHP function, or you&#8217;ll get an error. Here&#8217;s how I did that:</p>
<p>Put <em>rrd($username, $empty=FALSE);</em> just beneath the function <img src='http://www.kevinpajak.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Happy coding everybody.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinpajak.com/blog/2010/03/31/how-to-delete-entire-folderssub-folders-with-all-files-contained-within-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to replace a backslash character in a string</title>
		<link>http://www.kevinpajak.com/blog/2010/02/12/how-to-replace-a-backslash-character-in-a-string/</link>
		<comments>http://www.kevinpajak.com/blog/2010/02/12/how-to-replace-a-backslash-character-in-a-string/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 08:36:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Dev/Design]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kevinpajak.com/blog/?p=363</guid>
		<description><![CDATA[Ah, so often it&#8217;s the little things eh&#8230; PROBLEM: I needed to find a way to &#8220;re-process&#8221; text a user enters for a live chat client I&#8217;m working on (in which I prevented MySQL database injections via the mysqli_real_escape_string function). The problem of course is that the text the user enters nicely protects you from [...]]]></description>
			<content:encoded><![CDATA[<p>Ah, so often it&#8217;s the little things eh&#8230;</p>
<p>PROBLEM: I needed to find a way to &#8220;re-process&#8221; text a user enters for a live chat client I&#8217;m working on (in which I prevented MySQL database injections via the <em>mysqli_real_escape_string</em> function). The problem of course is that the text the user enters nicely protects you from a MySQL database injection, BUT the text then comes back full of backslash (\) characters (not too cool looking huh). So, to remove these, you can use the following code (here, I use the PHP <em>str_replace</em> function:</p>
<p>// Convert escaped characters back to regular text<br />
$string = str_replace(&quot;\\&quot;, &quot;&quot;, $string);</p>
<p>Thanks to Geoff Muldoon for this one!:<br />
<A HREF="http://www.bigresource.com/Tracker/Track-php-OcMaOirM/" target="_blank"><B>How to replace a backslash character in a string?</B></A></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinpajak.com/blog/2010/02/12/how-to-replace-a-backslash-character-in-a-string/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>mysqli_real_escape requires TWO parameters!</title>
		<link>http://www.kevinpajak.com/blog/2010/02/11/mysqli_real_escape-requires-two-parameters/</link>
		<comments>http://www.kevinpajak.com/blog/2010/02/11/mysqli_real_escape-requires-two-parameters/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 08:27:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Dev/Design]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.kevinpajak.com/blog/?p=356</guid>
		<description><![CDATA[Props to this really useful post on PHPFreaks.com: http://www.phpfreaks.com/forums/index.php?topic=219045.0 I was having some issues with a mysqli_real_escape_string function that I had converted from mysql to mysqli I believe that the mere mysql version of this tag can function with just a $value, but the mysqli version requires TWO PARAMETERS: both the $connection variable, and the [...]]]></description>
			<content:encoded><![CDATA[<p>Props to this really useful post on PHPFreaks.com:<br />
<A HREF="http://www.phpfreaks.com/forums/index.php?topic=219045.0" target="_blank">http://www.phpfreaks.com/forums/index.php?topic=219045.0</A></p>
<p>I was having some issues with a mysqli_real_escape_string function that I had converted from mysql to <em>mysqli</em> I believe that the mere mysql version of this tag can function with just a <b>$value</b>, but the mysqli version requires TWO PARAMETERS: both the $connection variable, and the $string (whatever variable your particular function uses. The one I have here goes through all posts in a foreach statement and escapes the values of all posts using an array (to prevent MySQL injections).</p>
<p>Ex:<br />
  //This stops SQL Injection in POST vars<br />
  foreach ($_POST as $key =&gt; $value) {<br />
    $_POST[$key] = mysqli_real_escape_string($connection, $value);<br />
  }</p>
<p>$connection = whichever variable you would normally use to reference connection to your MySQL database(s).</p>
<p><A HREF="http://www.phpfreaks.com/forums/index.php?topic=219045.0" target="_blank"><B>THIS POST</B></A> solved my problem! (Thanks a lot Bendude14!)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinpajak.com/blog/2010/02/11/mysqli_real_escape-requires-two-parameters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to log out a user who closes the browser window</title>
		<link>http://www.kevinpajak.com/blog/2010/02/10/how-to-log-out-a-user-who-closes-the-browser-window/</link>
		<comments>http://www.kevinpajak.com/blog/2010/02/10/how-to-log-out-a-user-who-closes-the-browser-window/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 04:43:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Daily Journal]]></category>
		<category><![CDATA[Web Dev/Design]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[apps]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.kevinpajak.com/blog/?p=339</guid>
		<description><![CDATA[I would like to thank Brazilian software developer Daniel Melo for posting this code which helped me to solve a problem which was very pesky indeed, and in need of a solution. The problem occurred during a user&#8217;s logged-in session for a chat application I was building. This was the issue: the app is basically [...]]]></description>
			<content:encoded><![CDATA[<p>I would like to thank Brazilian software developer Daniel Melo for posting this code which helped me to solve a problem which was very pesky indeed, and in need of a solution. The problem occurred during a user&#8217;s logged-in session for a chat application I was building. This was the issue: the app is basically a live-chat client, which also shows all current users online, by name. If a logged-in user clicked the &#8220;Logout&#8221; link, all was good, no problem, his/her name was removed from the list of currently logged-on users. However, if the person just suddenly decided to end the chat session by just closing the browser and hitting &#8216;X&#8217; (as I myself might do!), the user was still showing as currently online, and NOT logged out (obviously this is an annoying issue). <span id="more-339"></span>After searching far and wide for this on the Internet, I came across the brilliant code I found at the link below on stackoverflow.com:</p>
<p><A HREF="http://stackoverflow.com/questions/1371757/how-to-kill-a-application-session-when-a-browser-window-is-closed" target="_blank"><B>&#8220;How to kill an application session when a browser window is closed?&#8221;</B></A></p>
<p>This code uses jQuery/Ajax in an absolutely brilliant and straightforward way. Basically what the code does is automatically load the contents of your &#8220;logoff.php&#8221; page, using the JavaScript <b>window.onbeforeunload</b> function (which is something that executes upon a user&#8217;s exiting the browser, refreshing the page, or submitting a form). Probably the most brilliant thing about this script is that it still uses the <em>onbeforeunload</em> function, BUT (and this was why it was of so much particular use to me for my particular case), let&#8217;s say that your web app page is relying on a form processor page that is on an external page, and not on the same page. If the user in this kind of case enters in their chat text, and then hits the submit button (but still is currently ON the page and actively participating in the current chat session), the typical <em>onbeforeunload</em> function if called, would execute (so basically, in this case, this would amount to the user being logged off every time he/she hit the &#8220;Post Message&#8221; button, and not JUST when the user closed the browser window).</p>
<p>So what this very clever script does is execute the contents of the script on your &#8220;logoff.php&#8221; page <b>onbeforeunload</b>, BUT <u>excepts the following cases:</u> (so it won&#8217;t get called if the user does the following things, and will only log the user out when he/she closes the browser window):</p>
<p>- Hitting the Submit button;<br />
- Clicking a link.</p>
<p>[The "magical" thing I will add too, after thinking about it, is that your 'logout.php' web page does not even have to be currently loaded on the browser - after all, the browser is being closed, right? This script executes your code all at once - like I said, fantastic! One other slightly unfortunate thing I found too in my extensive testing was that: this script works fine in Firefox and IE (and worked in Google Chrome, <em>after</em> not working the first time, insanely enough, but did not work in Opera or Safari :S). My sort of work-around for this was to at least add in code to my log-in page that sets the user's status to offline right away, then online once they are logged on and enter the chatroom].</p>
<p>Anyway, hope what I said here makes sense and is not too muddled an explanation, and&#8230; here is the code!:</p>
<div style="background: #EAE3E3; padding: 10px; border: 1px solid #000;">
/*<br />
* autoLogoff.js<br />
*<br />
* Every valid navigation (form submit, click on links) should<br />
* set this variable to true.<br />
*<br />
* If it is left to false the page will try to invalidate the<br />
* session via an AJAX call<br />
*/<br />
var validNavigation = false;</p>
<p>/*<br />
* Invokes the servlet /endSession to invalidate the session.<br />
* No HTML output is returned<br />
*/<br />
function endSession() {<br />
   $.get(&quot;&lt;whatever url will end your session&gt;&quot;);<br />
}</p>
<p>function wireUpEvents() {</p>
<p>  /*<br />
  * For a list of events that triggers onbeforeunload on IE<br />
  * check http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx<br />
  */<br />
  window.onbeforeunload = function() {<br />
      if (!validNavigation) {<br />
         endSession();<br />
      }<br />
  }</p>
<p>  // Attach the event click for all links in the page<br />
  $(&quot;a&quot;).bind(&quot;click&quot;, function() {<br />
     validNavigation = true;<br />
  });</p>
<p>  // Attach the event submit for all forms in the page<br />
  $(&quot;form&quot;).bind(&quot;submit&quot;, function() {<br />
     validNavigation = true;<br />
  });</p>
<p>}</p>
<p>// Wire up the events as soon as the DOM tree is ready<br />
$(document).ready(function() {<br />
    wireUpEvents();  <br />
});
</div>
<p>From there, you just need to save this text in a file called &#8216;autoLogoff.js&#8217;, and call it toward the top of your page with the following code:</p>
<p>&lt;script type=&quot;text/javascript&quot; src=&quot;js/autoLogoff.js&quot;&gt;&lt;/script&gt;</p>
<p>I am making this post because this information was REALLY hard to find when I was searching for it, in the hopes that it can help you out with a similar project!</p>
<p>Again, here is the original posting for more and complete information:</p>
<p><A HREF="http://stackoverflow.com/questions/1371757/how-to-kill-a-application-session-when-a-browser-window-is-closed" target="_blank"><B>&#8220;How to kill an application session when a browser window is closed?&#8221;</B></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinpajak.com/blog/2010/02/10/how-to-log-out-a-user-who-closes-the-browser-window/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clearing contents of $_POST array values (to avoid re-submitting form data upon page refresh)</title>
		<link>http://www.kevinpajak.com/blog/2010/02/02/clearing-contents-of-_post-array-values-to-avoid-re-submitting-form-data-upon-page-refresh/</link>
		<comments>http://www.kevinpajak.com/blog/2010/02/02/clearing-contents-of-_post-array-values-to-avoid-re-submitting-form-data-upon-page-refresh/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 02:43:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Daily Journal]]></category>
		<category><![CDATA[Web Dev/Design]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kevinpajak.com/blog/?p=327</guid>
		<description><![CDATA[After trying SEVERAL different possible methods &#038; suggestions, I finally was able to achieve this by pointing my form&#8217;s PHP processing page to another page, rather than the same page itself (I really wanted to keep everything &#8211; form as well as form-handling all on the same page, but I found that I had to [...]]]></description>
			<content:encoded><![CDATA[<p>After trying SEVERAL different possible methods &#038; suggestions, I finally was able to achieve this by pointing my form&#8217;s PHP processing page to another page, rather than the same page itself (I really wanted to keep everything &#8211; form as well as form-handling all on the same page, but I found that I had to do this in order to prevent the user from re-submitting form data [through the contents of the $_POST array] that does not clear upon a user&#8217;s hitting the refresh button on the browser). And following that, I had my form&#8217;s processing page script then re-direct back to the original page where the form was located:</p>
<p>    // redirect to the same page without the POST data<br />
    header(&#8220;Location: original-page-with-form.php&#8221;);<br />
    die;</p>
<p>Here are some of the pages I consulted in my research:</p>
<p><A HREF="http://www.thefutureoftheweb.com/blog/get-redirect-after-post" target="_blank">http://www.thefutureoftheweb.com/blog/get-redirect-after-post</A><br />
<A HREF="http://www.issociate.de/board/post/171234/Clear_POST_variables_on_page_refresh.html" target="_blank">http://www.issociate.de/board/post/171234/Clear_POST_variables_on_page_refresh.html</A><br />
<A HREF="http://www.mail-archive.com/php-list@yahoogroups.com/msg04635.html" target="_blank">http://www.mail-archive.com/php-list@yahoogroups.com/msg04635.html</A><br />
<A HREF="http://qaix.com/php-web-programming/437-896-clear-post-variables-read.shtml" target="_blank">http://qaix.com/php-web-programming/437-896-clear-post-variables-read.shtml</A></p>
<p>*NOTE: Of course, keep in mind that any header(&#8220;Location: PAGE&#8221;); PHP code must be included BEFORE any contents are outputted on the page, otherwise you will get the lovely &#8220;Headers already sent&#8221; PHP error.</p>
<p>Hope this can help anyone else out there who may be struggling with the same issue <img src='http://www.kevinpajak.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinpajak.com/blog/2010/02/02/clearing-contents-of-_post-array-values-to-avoid-re-submitting-form-data-upon-page-refresh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery / Ajax live refresh</title>
		<link>http://www.kevinpajak.com/blog/2010/01/30/jquery-ajax-live-refresh/</link>
		<comments>http://www.kevinpajak.com/blog/2010/01/30/jquery-ajax-live-refresh/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 04:20:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Daily Journal]]></category>
		<category><![CDATA[apps]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.kevinpajak.com/blog/?p=320</guid>
		<description><![CDATA[Running live, simultaneous beta tests using three different browsers for my wallpost app, with added jQuery/Ajax page refresh functionality. It is kicking some SERIOUS ass already (think: live chat possibilities, with saved record of all dialogues, that can be added to any page, threaded to any topic, etc) Yeah! . [Already, I&#8217;d like to thank [...]]]></description>
			<content:encoded><![CDATA[<p>Running live, simultaneous beta tests using three different browsers for my wallpost app, with added jQuery/Ajax page refresh functionality. It is kicking some SERIOUS ass already (think: live chat possibilities, with saved record of all dialogues, that can be added to any page, threaded to any topic, etc) Yeah! <img src='http://www.kevinpajak.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>[Already, I&#8217;d like to thank programmer Srinivas Tamada for <A HREF="http://www.9lessons.info/2009/07/auto-load-refresh-every-10-seconds-with.html" target="_blank"><B>this rockin&#8217; code!</B></A></p>
<p>And additionally, <A HREF="http://www.brightcherry.co.uk/scribbles/2009/02/26/jquery-auto-refresh-div-every-x-seconds/" target="_blank"><B>very special thanks to Maruf</B></A> for helping resolve an annoying IE issue:</p>
<p>After I modified the code, the stupid forced-caching issue of IE (which was delaying or not showing the updated list of all the latest posts) managed to get resolved. Here&#8217;s my modified code of the function:</p>
<p>&lt;script&gt;</p>
<p>   var refreshId = setInterval(function() {<br />
      $(&quot;#page_refresh&quot;).load(&#8216;show-comments.php?v=&#8217;+ Math.random());<br />
   }, 500);</p>
<p>&lt;/script&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinpajak.com/blog/2010/01/30/jquery-ajax-live-refresh/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Logout script</title>
		<link>http://www.kevinpajak.com/blog/2010/01/27/logout-script/</link>
		<comments>http://www.kevinpajak.com/blog/2010/01/27/logout-script/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 05:23:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Daily Journal]]></category>
		<category><![CDATA[Web Dev/Design]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.kevinpajak.com/blog/?p=288</guid>
		<description><![CDATA[Wrote my own little logout script today (gives self a tiny little "geek pat on the back" :p).]]></description>
			<content:encoded><![CDATA[<p>Wrote my own little logout script today (gives self a tiny little &#8220;geek pat on the back&#8221; :p).</p>
<p><span id="more-288"></span><br />
Short and sweet:</p>
<p><B>*REVISED/IMPROVED SCRIPT:</B></p>
<p>&lt;?php</p>
<p>session_start();</p>
<p>$_SESSION['logged'] = 0;</p>
<p>$_SESSION['username'] = &#8221;;</p>
<p>$_SESSION['first_name'] = &#8221;;</p>
<p> echo &quot;&lt;script type=&#8217;text/javascript&#8217;&gt;<br />
	{<br />
		history.go(-1);<br />
	}<br />
  &lt;/script&gt;&quot;;</p>
<p>?&gt;</p>
<p>Now it will:</p>
<p><b>1)</b> Log the user out,</p>
<p><b>2)</b> Clear/reset current session state variables (just to be double-sure), then,</p>
<p><b>3)</b> Set the URL to the page <em>previous to &#8216;logout.php&#8217;</em> (which will be the page where the user was when he/she clicked &#8216;Logout&#8217;. We&#8217;ll safely assume that this would be a good spot to return the user).</p>
<p>[Code converted for webpage display using <A HREF="http://www.plus2net.com/html_tutorial/tags-page.php" target="_blank"><B>this awesome tool</B></A>.]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinpajak.com/blog/2010/01/27/logout-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
