<?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, 31 Jul 2010 22:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</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>
<pre class="php">
<span class="phpComment">/* Set timeout threshold to 10 minutes <span class="phpOperator">(</span>600 seconds<span class="phpOperator">)</span> */</span>
@<span class="phpFunction">session_start</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span>
$timeout <span class="phpOperator">=</span> 600;
<span class="phpScriptVar">$_SESSION</span><span class="phpOperator">[</span><span class="phpString">"expires_by"</span><span class="phpOperator">]</span> <span class="phpOperator">=</span> <span class="phpFunction">time</span><span class="phpOperator">(</span><span class="phpOperator">)</span> <span class="phpOperator">+</span> $timeout;
</pre>
]]></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:</p>
<pre class="php">
backup_tables<span class="phpOperator">(</span><span class="phpString">'localhost'</span>,<span class="phpString">'username'</span>,<span class="phpString">'password'</span>,<span class="phpString">'blog'</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpComment">// Backup the entire database or just a specific table.
</span><span class="phpFunctionKeyword">function</span> backup_tables<span class="phpOperator">(</span>$host,$user,$pass,$name,$tables <span class="phpOperator">=</span> <span class="phpString">'*'</span><span class="phpOperator">)</span>
<span class="phpOperator">{</span>
	$link <span class="phpOperator">=</span> <span class="phpFunction">mysql_connect</span><span class="phpOperator">(</span>$host,$user,$pass<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">mysql_select_db</span><span class="phpOperator">(</span>$name,$link<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpComment">//get all of the tables
</span><span class="phpKeyword">	if<span class="phpOperator">(</span></span>$tables <span class="phpOperator"><span class="phpOperator">=</span>=</span> <span class="phpString">'*'</span><span class="phpOperator">)</span>
	<span class="phpOperator">{</span>
		$tables <span class="phpOperator">=</span> <span class="phpFunction">array</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span>
		$result <span class="phpOperator">=</span> <span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">'SHOW TABLES'</span><span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpKeyword">	while<span class="phpOperator">(</span></span>$row <span class="phpOperator">=</span> <span class="phpFunction">mysql_fetch_row</span><span class="phpOperator">(</span>$result<span class="phpOperator">)</span><span class="phpOperator">)</span>
		<span class="phpOperator">{</span>
			$tables<span class="phpOperator">[</span><span class="phpOperator">]</span> <span class="phpOperator">=</span> $row<span class="phpOperator">[</span><span class="phpNumber">0</span><span class="phpOperator">]</span><span class="phpText">;</span>
		<span class="phpOperator">}</span>
	<span class="phpOperator">}</span>
<span class="phpKeyword">	else
</span>
	<span class="phpOperator">{</span>
		$tables <span class="phpOperator">=</span> is_<span class="phpFunction">array</span><span class="phpOperator">(</span>$tables<span class="phpOperator">)</span> <span class="phpOperator">?</span> $tables <span class="phpOperator">:</span> <span class="phpFunction">explode</span><span class="phpOperator">(</span><span class="phpString">','</span>,$tables<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpOperator">}</span>
	<span class="phpComment">//This method is completed<span class="phpKeyword"> for </span>each table
</span><span class="phpKeyword">	foreach<span class="phpOperator">(</span></span>$tables<span class="phpKeyword"> as </span>$table<span class="phpOperator">)</span>
	<span class="phpOperator">{</span>
		$result <span class="phpOperator">=</span> <span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">'SELECT * FROM '</span>.$table<span class="phpOperator">)</span><span class="phpText">;</span>
		$num_fields <span class="phpOperator">=</span> <span class="phpFunction">mysql_num_fields</span><span class="phpOperator">(</span>$result<span class="phpOperator">)</span><span class="phpText">;</span>
		$return<span class="phpOperator">.=</span> <span class="phpString">'DROP TABLE '</span>.$table.<span class="phpString">'<span class="phpText">;</span>'</span><span class="phpText">;</span>
		$row2 <span class="phpOperator">=</span> <span class="phpFunction">mysql_fetch_row</span><span class="phpOperator">(</span><span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">'SHOW CREATE TABLE '</span>.$table<span class="phpOperator">)</span><span class="phpOperator">)</span><span class="phpText">;</span>
		$return<span class="phpOperator">.=</span> <span class="phpString">"\n\n"</span>.$row2<span class="phpOperator">[</span><span class="phpNumber">1</span><span class="phpOperator">]</span>.<span class="phpString">"<span class="phpText">;</span>\n\n"</span><span class="phpText">;</span>
	<span class="phpKeyword">	for </span><span class="phpOperator">(</span>$i <span class="phpOperator">=</span> 0; $i <span class="phpOperator">&lt;</span> $num_fields<span class="phpText">;</span> $i<span class="phpOperator"><span class="phpOperator">+</span><span class="phpOperator">+</span></span><span class="phpOperator">)</span>
		<span class="phpOperator">{</span>
		<span class="phpKeyword">	while<span class="phpOperator">(</span></span>$row <span class="phpOperator">=</span> <span class="phpFunction">mysql_fetch_row</span><span class="phpOperator">(</span>$result<span class="phpOperator">)</span><span class="phpOperator">)</span>
			<span class="phpOperator">{</span>
				$return<span class="phpOperator">.=</span> <span class="phpString">'INSERT INTO '</span>.$table.<span class="phpString">' VALUES<span class="phpOperator">(</span>'</span><span class="phpText">;</span>
			<span class="phpKeyword">	for<span class="phpOperator">(</span></span>$j<span class="phpOperator">=</span>0; $j<span class="phpOperator">&lt;</span>$num_fields<span class="phpText">;</span> $j<span class="phpOperator"><span class="phpOperator">+</span><span class="phpOperator">+</span></span><span class="phpOperator">)</span>
				<span class="phpOperator">{</span>
					$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span> <span class="phpOperator">=</span> <span class="phpFunction">addslashes</span><span class="phpOperator">(</span>$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span><span class="phpOperator">)</span><span class="phpText">;</span>
					$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span> <span class="phpOperator">=</span> <span class="phpFunction">ereg_replace</span><span class="phpOperator">(</span><span class="phpString">"\n"</span>,<span class="phpString">"\\n"</span>,$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span><span class="phpOperator">)</span><span class="phpText">;</span>
				<span class="phpKeyword">	if </span><span class="phpOperator">(</span><span class="phpFunction">isset</span><span class="phpOperator">(</span>$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span><span class="phpOperator">)</span><span class="phpOperator">)</span> <span class="phpOperator">{</span> $return<span class="phpOperator">.=</span> <span class="phpString">'<span class="phpString">"'</span>.$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span>.<span class="phpString">'"' ; } else { $return.= '""</span>'</span><span class="phpText">;</span> <span class="phpOperator">}</span>
				<span class="phpKeyword">	if </span><span class="phpOperator">(</span>$j<span class="phpOperator">&lt;</span><span class="phpOperator">(</span>$num_fields-<span class="phpNumber">1</span><span class="phpOperator">)</span><span class="phpOperator">)</span> <span class="phpOperator">{</span> $return<span class="phpOperator">.=</span> <span class="phpString">','</span><span class="phpText">;</span> <span class="phpOperator">}</span>
				<span class="phpOperator">}</span>
				$return<span class="phpOperator">.=</span> <span class="phpString">"<span class="phpOperator">)</span><span class="phpText">;</span>\n"</span><span class="phpText">;</span>
			<span class="phpOperator">}</span>
		<span class="phpOperator">}</span>
		$return<span class="phpOperator">.=</span><span class="phpString">"\n\n\n"</span><span class="phpText">;</span>
	<span class="phpOperator">}</span>
	<span class="phpComment">//Now, we<span class="phpString">'ll save the <span class="phpFunction">file</span>
</span>	$handle <span class="phpOperator">=</span> <span class="phpFunction">fopen</span><span class="phpOperator">(</span>'</span>backup-<span class="phpString">'<span class="phpOperator">.</span><span class="phpFunction">time</span><span class="phpOperator">(</span><span class="phpOperator">)</span>.'</span>-<span class="phpString">'<span class="phpOperator">.</span><span class="phpOperator">(</span><span class="phpFunction">md5</span><span class="phpOperator">(</span><span class="phpFunction">implode</span><span class="phpOperator">(</span>'</span>,<span class="phpString">',$tables<span class="phpOperator">)</span><span class="phpOperator">)</span><span class="phpOperator">)</span>.'</span>.sql<span class="phpString">','</span>w<span class="phpOperator">+</span>&#039;<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">fwrite</span><span class="phpOperator">(</span>$handle,$return<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">fclose</span><span class="phpOperator">(</span>$handle<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/backup-mysql-database-php/feed/</wfw:commentRss>
		<slash:comments>2</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. if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') &#124;&#124; strstr($_SERVER['HTTP_USER_AGENT'],'iPod')) { // Change your URL below header('Location: http://www.domain.com/iphone'); exit(); }]]></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>
<pre class="php">
<span class="phpKeyword">
if<span class="phpOperator">(</span></span><span class="phpFunction">strstr</span><span class="phpOperator">(</span><span class="phpScriptVar">$_SERVER</span><span class="phpOperator">[</span><span class="phpString">'HTTP_USER_AGENT'</span><span class="phpOperator">]</span>,<span class="phpString">'iPhone'</span><span class="phpOperator">)</span> <span class="phpOperator">|</span><span class="phpOperator">|</span> <span class="phpFunction">strstr</span><span class="phpOperator">(</span><span class="phpScriptVar">$_SERVER</span><span class="phpOperator">[</span><span class="phpString">'HTTP_USER_AGENT'</span><span class="phpOperator">]</span>,<span class="phpString">'iPod'</span><span class="phpOperator">)</span><span class="phpOperator">)</span>
<span class="phpOperator">{</span>
<span class="phpComment">// Change your URL below
</span><span class="phpFunction">header</span><span class="phpOperator">(</span><span class="phpString">'Location<span class="phpOperator">:</span> http<span class="phpOperator">:</span><span class="phpComment">//www<span class="phpOperator">.</span>domain<span class="phpOperator">.</span>com/iphone'</span><span class="phpOperator">)</span><span class="phpText">;</span>
</span><span class="phpFunction">exit</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/detecting-iphone-visitors-ph/feed/</wfw:commentRss>
		<slash:comments>1</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>
<pre class="php">
<span class="phpComment">// ChrisCook<span class="phpOperator">.</span>me
</span><span class="phpFunctionKeyword">function</span> ok_HTML<span class="phpOperator">(</span>$string, $length <span class="phpOperator">=</span> null<span class="phpOperator">)</span>
<span class="phpOperator">{</span>
<span class="phpComment">// get rid of the extra space
</span>$string <span class="phpOperator">=</span> <span class="phpFunction">trim</span><span class="phpOperator">(</span>$string<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpComment">// avoid unicode codec issues
</span>$string <span class="phpOperator">=</span> <span class="phpFunction">utf8_decode</span><span class="phpOperator">(</span>$string<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpComment">// convert HTML characters
</span>$string <span class="phpOperator">=</span> <span class="phpFunction">htmlentities</span><span class="phpOperator">(</span>$string, <span class="phpConstant">ENT_NOQUOTES</span><span class="phpOperator">)</span><span class="phpText">;</span>
$string <span class="phpOperator">=</span> <span class="phpFunction">str_replace</span><span class="phpOperator">(</span><span class="phpString">"#"</span>, <span class="phpString">"#"</span>, $string<span class="phpOperator">)</span><span class="phpText">;</span>
$string <span class="phpOperator">=</span> <span class="phpFunction">str_replace</span><span class="phpOperator">(</span><span class="phpString">"%"</span>, <span class="phpString">"%"</span>, $string<span class="phpOperator">)</span><span class="phpText">;</span>
$length <span class="phpOperator">=</span> <span class="phpFunction">intval</span><span class="phpOperator">(</span>$length<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
if<span class="phpOperator">(</span></span>$length <span class="phpOperator">&gt;</span> <span class="phpNumber">0</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>
$string <span class="phpOperator">=</span> <span class="phpFunction">substr</span><span class="phpOperator">(</span>$string, <span class="phpNumber">0</span>, $length<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
<span class="phpKeyword">
return </span>$string;
<span class="phpOperator">}</span>
</pre>
<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;. /* * ajax_click.js * chriscook.me [...]]]></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>
<pre class="html">
/*
* ajax_click.js
*  chriscook.me
*/
function loadurl(dest) {
try {
// Moz supports XMLHttpRequest. IE uses ActiveX.
// browser detction is bad. object detection works for any browser
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
} catch (e) {
// browser doesn&#039;t support ajax. handle however you want
}
// the xmlhttp object triggers an event everytime the status changes
// triggered() function handles the events
xmlhttp.onreadystatechange = triggered;
// open takes in the HTTP method and url.
xmlhttp.open(&quot;GET&quot;, dest);
// send the request. if this is a POST request we would have
// sent post variables: send(&quot;name=aleem gender=male)
// Moz is fine with just send(); but
// IE expects a value here, hence we do send(null);
xmlhttp.send(&quot;null&quot;);
}
function triggered() {
if ((xmlhttp.readyState == 4) (xmlhttp.status == 200)) {
document.getElementById(&quot;ajaxlink&quot;).innerHTML = xmlhttp.responseText;
}
}
</pre>
<p><strong>2)</strong> Next, add the following code in the  section of your HTML file.</p>
<pre class="html"><span class="htmlScriptTag">&lt;script src=<span class="htmlAttributeValue">&quot;ajax_link.js&quot;</span> type=<span class="htmlAttributeValue">&quot;text/javascript&quot;</span>&gt;</span><span class="htmlScriptTag">&lt;/script&gt;</span></pre>
<p><strong>3)</strong> The following code should be placed in the HTML body of a PHP file.</p>
<pre class="html">
<span class="htmlOtherTag">&lt;div id=<span class="htmlAttributeValue">&quot;ajaxlink&quot;</span> onclick=<span class="htmlAttributeValue">&quot;loadurl(&#039;ajax_function.php&#039;)&quot;</span>&gt;</span>Click Here<span class="htmlOtherTag">&lt;/div&gt;</span>
</pre>
<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>
