<?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>Chris Cook .me &#187; php</title>
	<atom:link href="http://chriscook.me/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://chriscook.me</link>
	<description>Homepage</description>
	<lastBuildDate>Sat, 23 Oct 2010 22:53:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>PHP: Session Timeouts</title>
		<link>http://chriscook.me/web-development/php-session-timeouts/</link>
		<comments>http://chriscook.me/web-development/php-session-timeouts/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 22:31:50 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[timeout]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=117</guid>
		<description><![CDATA[Defining session timeout thresholds for PHP scripts is a security &#8220;must&#8221;. I recommend that you consider the purpose of your script before applying a session timeout function. For instance, if your site has a secure login and security requirements, it&#8217;s important to include the timeout function. However, it&#8217;s often an inconvenience to end-users. Use the [...]]]></description>
			<content:encoded><![CDATA[<p>Defining session timeout thresholds for PHP scripts is a security &#8220;must&#8221;.  I recommend that you consider the purpose of your script before applying a session timeout function.  For instance, if your site has a secure login and security requirements, it&#8217;s important to include the timeout function. However, it&#8217;s often an inconvenience to end-users.</p>
<p>Use the code below to create a function which can be used to implement a secure timeout threshold.</p>
<p>{code type=php}<br />
/* Set timeout threshold to 10 minutes (600 seconds) */<br />
@session_start();<br />
$timeout = 600;<br />
$_SESSION["expires_by"] = time() + $timeout;<br />
{/code}</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/php-session-timeouts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use PHP to Backup your MySQL Database</title>
		<link>http://chriscook.me/web-development/backup-mysql-database-php/</link>
		<comments>http://chriscook.me/web-development/backup-mysql-database-php/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 20:00:13 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[automated backup]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[cron job]]></category>
		<category><![CDATA[databsae]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=104</guid>
		<description><![CDATA[If you don&#8217;t backup your databases regularly, shame on you. You should! The code below will allow you to generate a backup as often as you&#8217;d like. It only makes sense to run this via a CRON job. This is pretty easy to do and you can set the frequency as you wish. The backup [...]]]></description>
			<content:encoded><![CDATA[<p>If you don&#8217;t backup your databases regularly, shame on you.  You should!</p>
<p>The code below will allow you to generate a backup as often as you&#8217;d like.  It only makes sense to run this via a CRON job.  This is pretty easy to do and you can set the frequency as you wish.</p>
<p>The backup files will be stored in the same directory as this script.  </p>
<p><em><strong>Please leave feedback and let me know if this works for you!</strong></em></p>
<p>Here&#8217;s the PHP code:<br />
{code type=php}<br />
backup_tables(&#8216;localhost&#8217;,'username&#8217;,'password&#8217;,'blog&#8217;);</p>
<p>// Backup the entire database or just a specific table.<br />
function backup_tables($host,$user,$pass,$name,$tables = &#8216;*&#8217;)<br />
{</p>
<p>	$link = mysql_connect($host,$user,$pass);<br />
	mysql_select_db($name,$link);</p>
<p>	//get all of the tables<br />
	if($tables == &#8216;*&#8217;)<br />
	{<br />
		$tables = array();<br />
		$result = mysql_query(&#8216;SHOW TABLES&#8217;);<br />
		while($row = mysql_fetch_row($result))<br />
		{<br />
			$tables[] = $row[0];<br />
		}<br />
	}<br />
	else<br />
	{<br />
		$tables = is_array($tables) ? $tables : explode(&#8216;,&#8217;,$tables);<br />
	}</p>
<p>	//This method is completed for each table<br />
	foreach($tables as $table)<br />
	{<br />
		$result = mysql_query(&#8216;SELECT * FROM &#8216;.$table);<br />
		$num_fields = mysql_num_fields($result);</p>
<p>		$return.= &#8216;DROP TABLE &#8216;.$table.&#8217;;';<br />
		$row2 = mysql_fetch_row(mysql_query(&#8216;SHOW CREATE TABLE &#8216;.$table));<br />
		$return.= &#8220;\n\n&#8221;.$row2[1].&#8221;;\n\n&#8221;;</p>
<p>		for ($i = 0; $i < $num_fields; $i++)<br />
		{<br />
			while($row = mysql_fetch_row($result))<br />
			{<br />
				$return.= &#8216;INSERT INTO &#8216;.$table.&#8217; VALUES(&#8216;;<br />
				for($j=0; $j<$num_fields; $j++)<br />
				{<br />
					$row[$j] = addslashes($row[$j]);<br />
					$row[$j] = ereg_replace(&#8220;\n&#8221;,&#8221;\\n&#8221;,$row[$j]);<br />
					if (isset($row[$j])) { $return.= &#8216;&#8221;&#8216;.$row[$j].&#8217;&#8221;&#8216; ; } else { $return.= &#8216;&#8221;"&#8216;; }<br />
					if ($j<($num_fields-1)) { $return.= &#8216;,&#8217;; }<br />
				}<br />
				$return.= &#8220;);\n&#8221;;<br />
			}<br />
		}<br />
		$return.=&#8221;\n\n\n&#8221;;<br />
	}</p>
<p>	//Now, we&#8217;ll save the file<br />
	$handle = fopen(&#8216;backup-&#8217;.time().&#8217;-&#8217;.(md5(implode(&#8216;,&#8217;,$tables))).&#8217;.sql&#8217;,'w+&#8217;);<br />
	fwrite($handle,$return);<br />
	fclose($handle);<br />
}<br />
{/code}</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/backup-mysql-database-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Detecting iPhone visitors with PHP</title>
		<link>http://chriscook.me/web-development/detecting-iphone-visitors-ph/</link>
		<comments>http://chriscook.me/web-development/detecting-iphone-visitors-ph/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 00:53:59 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[http_user_agent]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone redirect]]></category>
		<category><![CDATA[iphone visitor]]></category>
		<category><![CDATA[ipod]]></category>
		<category><![CDATA[ipod touch]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[visitor detection]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=87</guid>
		<description><![CDATA[More and more internet traffic is generated by the iPhone and iPod touch. The PHP snippet below shows you how to automatically redirect your users to a page which is optimized for the iPhone Safari browser. {code type=php} if(strstr($_SERVER['HTTP_USER_AGENT'],&#8217;iPhone&#8217;) &#124;&#124; strstr($_SERVER['HTTP_USER_AGENT'],&#8217;iPod&#8217;)) { // Change your URL below header(&#8216;Location: http://www.domain.com/iphone&#8217;); exit(); } {/code}]]></description>
			<content:encoded><![CDATA[<p>More and more internet traffic is generated by the <strong>iPhone</strong> and <strong>iPod touch</strong>.</p>
<p>The PHP snippet below shows you how to automatically redirect your users to a page which is optimized for the iPhone Safari browser.</p>
<p>{code type=php}<br />
if(strstr($_SERVER['HTTP_USER_AGENT'],&#8217;iPhone&#8217;) || strstr($_SERVER['HTTP_USER_AGENT'],&#8217;iPod&#8217;))<br />
{<br />
// Change your URL below<br />
header(&#8216;Location: http://www.domain.com/iphone&#8217;);<br />
exit();<br />
}<br />
{/code}</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/detecting-iphone-visitors-ph/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Preventing typical XSS attacks</title>
		<link>http://chriscook.me/web-development/php-preventing-typical-xss-attacks/</link>
		<comments>http://chriscook.me/web-development/php-preventing-typical-xss-attacks/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 17:36:44 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[cross-site scripting]]></category>
		<category><![CDATA[htmlentities]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[prevent xss]]></category>
		<category><![CDATA[transform_HTML]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xss]]></category>
		<category><![CDATA[xss attacks]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=71</guid>
		<description><![CDATA[XSS attacks plague beginner programmers and are a significant vulnerability for commercial web hosts &#38; website operators.  XSS means &#8220;cross-site scripting&#8220;.  These exploits work on the client side. Often, hackers put some type of JavaScript in content that users submit that allow them to steal the data from a cookie.  XSS attacks are pretty difficult [...]]]></description>
			<content:encoded><![CDATA[<p>XSS attacks plague beginner programmers and are a significant vulnerability for commercial web hosts &amp; website operators.  XSS means &#8220;<strong>cross-site scripting</strong>&#8220;.  These exploits work on the client side. Often, hackers put some type of JavaScript in content that users submit that allow them to steal the data from a cookie.  XSS attacks are pretty difficult to prevent.  Hackers have been successful with XSS attacks on most, if not all, of the biggest sites on the net.</p>
<p>To help prevent XSS attacks, it&#8217;s best to restrict and filter the data that you get from a user through your site.  Have you ever wondered why popular bulletin boards, such as vB or phpBB, use custom tag formats like [url] or [b]?  They&#8217;re trying to prevent attacks.</p>
<p>This tutorial is a very basic example of a way to help prevent XSS attacks.  There are other methods &#8212; and more comprehensive methods out there.</p>
<p><strong>okHTML function:</strong><br />
Let&#8217;s start with a simple function that converts any HTML code (or character) into literals.</p>
<p>{code type=php}</p>
<p>// ChrisCook.me<br />
function ok_HTML($string, $length = null)<br />
{<br />
// get rid of the extra space<br />
$string = trim($string);</p>
<p>// avoid unicode codec issues<br />
$string = utf8_decode($string);</p>
<p>// convert HTML characters<br />
$string = htmlentities($string, ENT_NOQUOTES);<br />
$string = str_replace(&#8220;#&#8221;, &#8220;#&#8221;, $string);<br />
$string = str_replace(&#8220;%&#8221;, &#8220;%&#8221;, $string);</p>
<p>$length = intval($length);<br />
if($length &gt; 0) {<br />
$string = substr($string, 0, $length);<br />
}<br />
return $string;<br />
}<br />
{/code}</p>
<p><strong>The Explanation:</strong><br />
One  of the  most important components of that function is the htmlentities() funcion call that converts <strong>&amp;</strong>, <strong>&lt;</strong>, and <strong>&gt;</strong> into <strong>&amp;amp;</strong>, <strong>&amp;lt;</strong>, and <strong>&amp;gt;</strong>. This helps resolve the simple hacks.  We&#8217;re not done yet, though.  All XSS attacks aren&#8217;t basic.  Hackers know programmers have implemented these attacks to they tend to encode their hacks and malicious scripts in UTF-8 or hexadecimal instead of using the normal ASCII text.</p>
<p>To help prevent this, transform_HTML() takes the additional step of converting # and % signs into the correct entities.</p>
<p>In my readings on preventing XSS attacks, many experts recommend that you limit the  string length in case some goober tries to overload your string with a very, very long input in hopes that they&#8217;ll crash the server or your database. You can edit the <strong>$length</strong> parameter to help control this.</p>
<p>That&#8217;s it for today,<br />
<em>Chris</em></p>
<p><strong>Disclaimer: </strong>As always, I want to add my handy-dandy disclaimer.  Please understand that this tutorial is intended to demonstrate a specific function.  Please review the code and add appropriate security measures before using it in a production environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/php-preventing-typical-xss-attacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP/AJAX: Call PHP function by clicking a link</title>
		<link>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/</link>
		<comments>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 22:14:24 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[chriscook.me]]></category>
		<category><![CDATA[execute php function in ajax request]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=46</guid>
		<description><![CDATA[This tutorial demonstrates how to execute an external PHP function by clicking a simple link within HTML.  The method uses AJAX so that the page doesn&#8217;t refresh and doesn&#8217;t require a form submission. 1) Paste the following code into a .js file.  For demonstration purposes, we have named it &#8220;ajax_click.js&#8221;. {code type=html} /* * ajax_click.js [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial demonstrates how to execute an external PHP function by clicking a simple link within HTML.  The method uses AJAX so that the page doesn&#8217;t refresh and doesn&#8217;t require a form submission.</p>
<p><strong>1)</strong> Paste the following code into a .js file.  For demonstration purposes, we have named it &#8220;ajax_click.js&#8221;.</p>
<p>{code type=html}</p>
<p>/*<br />
* ajax_click.js<br />
*  chriscook.me<br />
*/</p>
<p>function loadurl(dest) {</p>
<p>try {<br />
// Moz supports XMLHttpRequest. IE uses ActiveX.<br />
// browser detction is bad. object detection works for any browser<br />
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
} catch (e) {<br />
// browser doesn&#8217;t support ajax. handle however you want<br />
}</p>
<p>// the xmlhttp object triggers an event everytime the status changes<br />
// triggered() function handles the events<br />
xmlhttp.onreadystatechange = triggered;</p>
<p>// open takes in the HTTP method and url.<br />
xmlhttp.open(&#8220;GET&#8221;, dest);</p>
<p>// send the request. if this is a POST request we would have<br />
// sent post variables: send(&#8220;name=aleem gender=male)<br />
// Moz is fine with just send(); but<br />
// IE expects a value here, hence we do send(null);<br />
xmlhttp.send(&#8220;null&#8221;);<br />
}</p>
<p>function triggered() {<br />
if ((xmlhttp.readyState == 4) (xmlhttp.status == 200)) {</p>
<p>document.getElementById(&#8220;ajaxlink&#8221;).innerHTML = xmlhttp.responseText;<br />
}<br />
}<br />
{/code}</p>
<p><strong>2)</strong> Next, add the following code in the  section of your HTML file.</p>
<p>{code type=html}&lt;script src=&#8221;ajax_link.js&#8221; type=&#8221;text/javascript&#8221;&gt;&lt;/script&gt;{/code}</p>
<p><strong>3)</strong> The following code should be placed in the HTML body of a PHP file.</p>
<p>{code type=html}</p>
<p>&lt;div id=&#8221;ajaxlink&#8221; onclick=&#8221;loadurl(&#8216;ajax_function.php&#8217;)&#8221;&gt;Click Here&lt;/div&gt;</p>
<p>{/code}</p>
<p>Replace &#8216;ajax_function.php&#8217; with the correct file you want to execute.  For instance, if you want your users to &#8220;click here&#8221; in order to send themselves a copy of their monthly invoice, you&#8217;d code the mailer function in &#8216;ajax_function.php&#8217;.</p>
<p><strong>4) </strong>That&#8217;s it.  Ensure that you keep the &lt;div&gt; id  as &#8220;ajaxlink&#8221;.  Once you click the link, it will disappear (until the page is refreshed).</p>
<p><strong>Feedback:</strong> I&#8217;d love to hear how you&#8217;ve used this tool.  Please feel free to post a comment on my blog.</p>
<p><strong>Disclaimer: </strong> This tutorial is provided to demonstrate how to perform the function.  Please ensure that you review the code and add security measures before using this in a production environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

