<?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>3cc Internet &#187; Coding</title>
	<atom:link href="http://www.3cc.org/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3cc.org</link>
	<description>Simplifying sites</description>
	<lastBuildDate>Sat, 31 Dec 2011 15:51:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Facebook Like Buttons, &amp;fb_xd_fragment=, Blank pages and SEO</title>
		<link>http://www.3cc.org/2011/04/facebook-like-buttons-fb_xd_fraglment-blank-pages-and-seo/</link>
		<comments>http://www.3cc.org/2011/04/facebook-like-buttons-fb_xd_fraglment-blank-pages-and-seo/#comments</comments>
		<pubDate>Sun, 17 Apr 2011 17:39:52 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[facebook]]></category>

		<guid isPermaLink="false">http://www.3cc.org/?p=307</guid>
		<description><![CDATA[This is a software development problem that&#8217;s been around for months now and yet still bugs thousands of users. Any of your pages with facebook widgets on can result in URL&#8217;s like www.example.com/page.php?fb_xd_fragment= This has several implications. Firstly &#8211; it can cause blank pages through unwanted interactions with div areas on the page. We can [...]]]></description>
			<content:encoded><![CDATA[<p>This is a <a href="http://www.elinext.com/" class="clear_url">software development</a> problem that&#8217;s been around for months now and yet still bugs thousands of users.</p>
<p>Any of your pages with facebook widgets on can result in URL&#8217;s like</p>
<p>www.example.com/page.php?fb_xd_fragment=</p>
<p>This has several implications. Firstly &#8211; it can cause blank pages through unwanted interactions with div areas on the page. We can solve this by adding the following fix just before the &lt;/body&gt; tag. This came from http://forum.developers.facebook.net/viewtopic.php?id=60571&amp;p=1 (temporary solutions)</p>
<pre class="brush: js">&lt;!-- Correct fb_xd_fragment Bug Start --&gt;
&lt;script&gt;
document.getElementsByTagName('html')[0].style.display='block';
&lt;/script&gt;
&lt;!-- Correct fb_xd_fragment Bug End --&gt;</pre>
<p>The second, more long term issue is that this page will appear in search results alongside the normal page&#8230;resulting in duplicate content. Obviously you could just remove the like button but that&#8217;s not an ideal solution. So you can do a couple of things.</p>
<p>Head to webmastertools (https://www.google.com/webmasters/tools/home?hl=en) and add the fb_xd_fragment= as something that should be ignored on your site.</p>
<div id="attachment_308" class="wp-caption aligncenter" style="width: 610px"><img class="size-large wp-image-308" title="Filtering out fb_xd_fragment in Google Webmaster Tools" src="http://www.3cc.org/wp-content/uploads/2011/04/google_webmaster_tools-600x412.png" alt="Filtering out fb_xd_fragment in Google Webmaster Tools" width="600" height="412" /><p class="wp-caption-text">Filtering out fb_xd_fragment in Google Webmaster Tools</p></div>
<p>Another option is to use .htaccess and 301 redirects to clip out the &amp;fb_xd_fragment=, which is a pain but very easily do-able and removes the requirement to put the display fix on every page. So try this (modified per your site) in your .htaccess.</p>
<p>RewriteCond %{QUERY_STRING} fb_xd_fragment=<br />
RewriteRule ^(.*) http://www.example.com/$1? [R=301]</p>
<p>You could also ignore it and just hope facebook fixes it soon&#8230;yeahh bad choices right?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3cc.org/2011/04/facebook-like-buttons-fb_xd_fraglment-blank-pages-and-seo/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>phpBB 3 Script Integration &#8211; New Threads and Replies from an External Script</title>
		<link>http://www.3cc.org/2010/09/phpbb-3-script-integration-new-threads-and-replies-from-an-external-script/</link>
		<comments>http://www.3cc.org/2010/09/phpbb-3-script-integration-new-threads-and-replies-from-an-external-script/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 11:21:24 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpbb]]></category>

		<guid isPermaLink="false">http://www.3cc.org/?p=217</guid>
		<description><![CDATA[This is the third article I&#8217;ve written for pulling functions in phpBB3 for external use. This one allows you to either create new posts in a forum or reply to a thread. This was created for use with a text system, where users could text in comments which would be added to a thread. Article [...]]]></description>
			<content:encoded><![CDATA[<p>This is the third article I&#8217;ve written for pulling functions in phpBB3 for external use. This one allows you to either create new posts in a forum or reply to a thread. This was created for use with a text system, where users could text in comments which would be added to a thread.</p>
<p><a href="http://www.3cc.org/2010/03/integrating-your-existing-site-into-phpbb3/" target="_blank">Article on phpBB3 Integration</a><br />
<a href="http://www.3cc.org/2010/09/phpbb-3-script-integration-sending-private-messages-pms-from-an-external-script/" target="_blank">Article on sending PM&#8217;s</a></p>
<p>I&#8217;ve put everything needed into one file as I don&#8217;t want to go through and break it up. The previous two posts (linked above) used a seperate phpbb.php file with part of the code in but this just includes everything.</p>
<pre class="brush: php">&lt;?php
// All queries --&gt; support@3cc.org
// This is not setup for new thread posting, and has been config'd to not increment post count as this is for a bot.
// Further changes will be needed to clean up code as this is using external functions instead of clear documentation. --dc
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './phpBB3/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user-&gt;session_begin();
$auth-&gt;acl($user-&gt;data);
$user-&gt;setup();

// post send controller
function sendphpbbpost($pmmessage,$userid,$pmsubject) {

include_once('phpBB3/includes/functions_posting.php');
$my_subject = utf8_normalize_nfc(request_var('$pmsubject', '', true));
$message = utf8_normalize_nfc($pmmessage, '', true);
$uid = $bitfield = $options = '';
$allow_bbcode = $allow_smilies = true;
$allow_urls = true;
generate_text_for_storage($message, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
$data = array(
// General Posting Settings
'forum_id'          =&gt; 7,    // The forum ID in which the post will be placed. (int)
'topic_id'          =&gt; 5,    // Post a new topic or in an existing one? Set to 0 to create a new one, if not, specify your topic ID here instead.
'icon_id'           =&gt; false,    // The Icon ID in which the post will be displayed with on the viewforum, set to false for icon_id. (int)

// Defining Post Options
'enable_bbcode' =&gt; false, // Enable BBcode in this post. (bool)
'enable_smilies'    =&gt; true, // Enabe smilies in this post. (bool)
'enable_urls'       =&gt; false, // Enable self-parsing URL links in this post. (bool)
'enable_sig'        =&gt; true, // Enable the signature of the poster to be displayed in the post. (bool)

// Message Body
'message'           =&gt; $message,     // Your text you wish to have submitted. It should pass through generate_text_for_storage() before this. (string)
'message_md5'   =&gt; md5($message),    // The md5 hash of your message

// Values from generate_text_for_storage()
'bbcode_bitfield'   =&gt; $bitfield,    // Value created from the generate_text_for_storage() function.
'bbcode_uid'        =&gt; $uid,     // Value created from the generate_text_for_storage() function.

// Other Options
'post_edit_locked'  =&gt; 0,
'topic_title'       =&gt; $subject, // Subject/Title of the topic. (string). This is needed for new posts but for our purposes isn't.

// Email Notification Settings
'notify_set'        =&gt; false,        // (bool)
'notify'            =&gt; false,        // (bool)
'post_time'         =&gt; 0,        // Set a specific time, use 0 to let submit_post() take care of getting the proper time (int)
'forum_name'        =&gt; '',       // For identifying the name of the forum in a notification email. (string)

// Indexing
'enable_indexing'   =&gt; true,
'force_approved_state' =&gt; true, // Only runs on 6 onwards...check SUCC
);

//Now send that post...
submit_post('reply', '', '', POST_NORMAL, &amp;$poll, &amp;$data, $update_message = true);

}

$user-&gt;data['user_id'] = 2;    // User id of poster.
$auth-&gt;acl($user-&gt;data); // Re-initiate user after recall
$userid = $user-&gt;data['user_id'];
$pmmessage = 'This is a new reply, change this to whatever you want.';
sendphpbbpost($pmmessage,$userid,$pmsubject);

?&gt;</pre>
<p>Looking through the code above you will see several points that need changing. It&#8217;s currently set to &#8216;reply&#8217; (change this to &#8216;post&#8217; for a new post). Change all details to suit really &#8211; it&#8217;s easy to customise.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3cc.org/2010/09/phpbb-3-script-integration-new-threads-and-replies-from-an-external-script/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>phpBB 3 Script Integration &#8211; Sending Private Messages (PM&#8217;s) From an External Script</title>
		<link>http://www.3cc.org/2010/09/phpbb-3-script-integration-sending-private-messages-pms-from-an-external-script/</link>
		<comments>http://www.3cc.org/2010/09/phpbb-3-script-integration-sending-private-messages-pms-from-an-external-script/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 16:19:52 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpbb]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.3cc.org/?p=214</guid>
		<description><![CDATA[A while back I posted an article about integrating your site to use the phpBB3 user and login system (here). When I wrote that I also used phpBB as a message system for a site; so when a user did something they were sent a private message automatically. This bit of code (use it in [...]]]></description>
			<content:encoded><![CDATA[<p>A while back I posted an article about integrating your site to use the phpBB3 user and login system (<a href="http://www.3cc.org/2010/03/integrating-your-existing-site-into-phpbb3/" target="_blank">here</a>). When I wrote that I also used phpBB as a message system for a site; so when a user did something they were sent a private message automatically.</p>
<p>This bit of code (use it in conjunction with phpbb.php mentioned in the post linked to above) allows you to call a function to send a PM to any user as long as you have their ID.</p>
<pre class="brush: php">&lt;?php
// PM send controller
include_once("phpbb.php");

function sendphpbbpm($pmmessage,$userid,$pmsubject) {
include_once('forum/includes/functions_privmsgs.php');

$message = utf8_normalize_nfc($pmmessage);
$uid = $bitfield = $options = ''; // will be modified by generate_text_for_storage
$allow_bbcode = $allow_smilies = true;
$allow_urls = true;
generate_text_for_storage($message, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
$pm_data = array(
'from_user_id'        =&gt; 2,
'from_user_ip'        =&gt; "127.0.0.1",
'from_username'        =&gt; "admin",
'enable_sig'            =&gt; false,
'enable_bbcode'        =&gt; true,
'enable_smilies'        =&gt; true,
'enable_urls'        =&gt; false,
'icon_id'            =&gt; 0,
'bbcode_bitfield'    =&gt; $bitfield,
'bbcode_uid'         =&gt; $uid,
'message'            =&gt; $message,
'address_list'        =&gt; array('u' =&gt; array($userid =&gt; 'to')),
);

//Now We Have All Data Lets Send The PM!!
submit_pm('post', $pmsubject, $pm_data, false, false);
}

?&gt;</pre>
<p>You&#8217;ll need to modify the code above slightly to fit your needs, for example changing the admin username and user id, and then calling the function but it&#8217;s all pretty simple stuff one you&#8217;ve got all this.</p>
<p>To call the function (send the PM) to a fictional user 11:</p>
<pre class="brush: php">&lt;?php

$pmsubject = 'Please read this PM.';
$pmmessage = 'Thank you for reading this PM!';

$userid = '11';

sendphpbbpm($pmmessage,$userid,$pmsubject);

?&gt;</pre>
<p>Posting threads and replying to threads uses a similar system so I&#8217;ll put up some script for that shortly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3cc.org/2010/09/phpbb-3-script-integration-sending-private-messages-pms-from-an-external-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Benchmarking and testing your PHP script with microtime</title>
		<link>http://www.3cc.org/2010/09/benchmarking-and-testing-your-php-script-with-microtime/</link>
		<comments>http://www.3cc.org/2010/09/benchmarking-and-testing-your-php-script-with-microtime/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 16:53:44 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[microtime]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.3cc.org/?p=210</guid>
		<description><![CDATA[When you&#8217;re building a new website you often code it &#8216;on-the-fly&#8217;. That&#8217;s to say, strapping in new features here and there until it does exactly what you want &#8211; but leaving a mess that needs to be optimised. One of the best ways of testing your site for scalability (other than getting huge traffic straight [...]]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;re building a new website you often code it &#8216;on-the-fly&#8217;. That&#8217;s to say, strapping in new features here and there until it does exactly what you want &#8211; but leaving a mess that needs to be optimised.</p>
<p>One of the best ways of testing your site for scalability (other than getting huge traffic straight away) is to test how long your PHP scripts take to parse. We can do this by comparing the time at the start and end of a script to give us the processing time. The PHP manual shows the following example:</p>
<pre class="brush:php">$time_start = microtime(true);
// Script you want to test here
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";</pre>
<p>But I prefer this for readability (I put this in a common include so I can test the whole site by simply calling some functions&#8230;see below):</p>
<pre class="brush:php">function starttime() {
$r = explode( ' ', microtime() );
$r = $r[1] + $r[0];
return $r;
}

function endtime($starttime) {
$r = explode( ' ', microtime() );
$r = $r[1] + $r[0];
$r = round($r - $starttime,4);
return '&lt;strong&gt;Execution Time&lt;/strong&gt;: '.$r.' seconds&lt;br /&gt;';
}
</pre>
<p>To use this in a script all we&#8217;d need to do is place this at the start and end, and do a simple calculation. For example (using sudo code):</p>
<pre class="brush:php">$start = starttime();
// Script you want to test here
echo endtime($start);
</pre>
<p>This will help you discover bottlenecks in your script &#8211; there are a load of things you can do to optimise your script; and I find that using functions as above don&#8217;t add extra complication to your script.</p>
<p>For a much more comprehensive article and info on optimisation I would highly reccommend heading here: http://phplens.com/lens/php-book/optimizing-debugging-php.php</p>
<p>He has revealed many of the things people thought sped up PHP execution are simply myths dating back to previous PHP versions, making them pointless. This includes things like echo being faster than print and single quoted vars being faster than double quoted ones.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3cc.org/2010/09/benchmarking-and-testing-your-php-script-with-microtime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Google Maps, Geocoding and PHP to find the distance between UK Postcodes</title>
		<link>http://www.3cc.org/2010/08/using-google-maps-geocoding-and-php-to-find-the-distance-between-uk-postcodes/</link>
		<comments>http://www.3cc.org/2010/08/using-google-maps-geocoding-and-php-to-find-the-distance-between-uk-postcodes/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 15:57:11 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[geocoding]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[postcodes]]></category>

		<guid isPermaLink="false">http://www.3cc.org/?p=140</guid>
		<description><![CDATA[If you&#8217;re looking to make any kind of radius checker, delivery calculator etc, you will need to have some method of calculating this distance. Unfortunately for us in the UK, Royal Mail keep a tight grip on postcode data. As a result, the best low-budget way of finding postcodes is by using the Google Maps [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re looking to make any kind of radius checker, delivery calculator etc, you will need to have some method of calculating this distance. Unfortunately for us in the UK, Royal Mail keep a tight grip on postcode data.</p>
<p>As a result, the best low-budget way of finding postcodes is by using the Google Maps api &#8211; which in itself isn&#8217;t 100% accurate (but good enough).</p>
<p>So we can use the following code:</p>
<pre class="brush:php">&lt;?php
// Specify Postcodes to Geocode
$postcode1 = 'BH151DA';
$postcode2 = 'BH213AP';

// Geocode Postcodes &amp; Get Co-ordinates 1st Postcode
$pc1 = 'http://maps.google.com/maps/geo?q='.$postcode1.',+UK&amp;output=csv&amp;sensor=false';
$data1 = @file_get_contents($pc1);
$result1 = explode(",", $data1);
$custlat1 = $result1[2];
$custlong1 = $result1[3];

// Geocode Postcodes &amp; Get Co-ordinates 2nd Postcode
$pc2 = 'http://maps.google.com/maps/geo?q='.$postcode2.',+UK&amp;output=csv&amp;sensor=false';
$data2 = @file_get_contents($pc2);
$result2 = explode(",", $data2);
$custlat2 = $result2[2];
$custlong2 = $result2[3];

// Work out the distance!
$pi80 = M_PI / 180;
$custlat1 *= $pi80;
$custlong1 *= $pi80;
$custlat2 *= $pi80;
$custlong2 *= $pi80;

$r = 6372.797; // mean radius of Earth in km
$dlat = $custlat2 - $custlat1;
$dlng = $custlong2 - $custlong1;
$a = sin($dlat / 2) * sin($dlat / 2) + cos($custlat1) * cos($custlat2) * sin($dlng / 2) * sin($dlng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));

// Distance in KM
$km = round($r * $c, 2);

// Distance in Miles
$miles = round($km * 0.621371192, 2);

echo 'The distance between '.$postcode1.' and '.$postcode2.' is '.$km.'Km ('.$miles.' miles).';

?&gt;
</pre>
<p>You could use $result1[0] and $result2[0] to check codes. If the value is anything other than 200 the postcode is invalid. Also note UK is also searched for to guarantee correct results!</p>
<p>The result is also rounded to make sure we only have 2 decimal places. Make sure your postcodes do not have any spaces in when they go to Google, if you&#8217;re collecting them from a form maybe use:</p>
<pre class="brush:php">function nowhitespace($data) {
return preg_replace('/\s/', '', $data);
}
$postcode1 = nowhitespace($postcode1);</pre>
<p>to remove all spaces before processing, and the following to check it&#8217;s ok after processing:</p>
<pre class="brush:php">if (($result1[0] != 200) || ($result2[0] != 200)) {
echo "&lt;p&gt;Invalid Postcode(s) Entered. Please try again.&lt;/p&gt;";
} else {</pre>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3cc.org/2010/08/using-google-maps-geocoding-and-php-to-find-the-distance-between-uk-postcodes/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Vertical Align and Centering Images in Div tags</title>
		<link>http://www.3cc.org/2010/08/vertical-align-and-centering-images-in-div-tags/</link>
		<comments>http://www.3cc.org/2010/08/vertical-align-and-centering-images-in-div-tags/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 15:26:13 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://www.3cc.org/?p=197</guid>
		<description><![CDATA[Just thought I&#8217;d share my preferred solution for vertical aligning an image within a Div. You can&#8217;t use &#60;p&#62; tags, text align, vertical align or even image align as this messes up the display? I had this problem with thumbnails or varying sizes which needed to be centered in a div of specific size. Using [...]]]></description>
			<content:encoded><![CDATA[<p>Just thought I&#8217;d share my preferred solution for vertical aligning an image within a Div.</p>
<p>You can&#8217;t use &lt;p&gt; tags, text align, vertical align or even image align as this messes up the display? I had this problem with thumbnails or varying sizes which needed to be centered in a div of specific size.</p>
<p>Using the approach suggested <a href="http://www.w3.org/Style/Examples/007/center#vertical" target="_blank">here</a> and classing it as a table cell wasn&#8217;t great as it messed up the display&#8230;so I just set the image as a background which you can center &amp; put a transparent gif over it to link to. Not ideal but it works!</p>
<pre class="brush:html;">&lt;div style="background-image: url('thumbnail.gif');  background-repeat: no-repeat; background-position: center; width: 100px;  height: 100px"&gt;
&lt;a href="page.php"&gt;
&lt;img src="transparent  gif" width="100" height="100" /&gt;
&lt;/a&gt;
&lt;/div&gt;
</pre>
<p>You could also consider using absolute div tags but that also tends to interfere with surrounding design elements! Other solutions welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3cc.org/2010/08/vertical-align-and-centering-images-in-div-tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Resubmitting forms upon page refresh or reload in PHP</title>
		<link>http://www.3cc.org/2010/08/resubmitting-forms-upon-page-refresh-or-reload-in-php/</link>
		<comments>http://www.3cc.org/2010/08/resubmitting-forms-upon-page-refresh-or-reload-in-php/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 19:03:13 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sessions]]></category>

		<guid isPermaLink="false">http://www.3cc.org/?p=191</guid>
		<description><![CDATA[A common problem with forms is the annoying resubmit when the user reloads or refreshes a page. This can result in duplicate database records if not dealt with properly. There are a number of simple solutions to this problem, I&#8217;ll go through a couple below. 1. Use two files (one with the form on and [...]]]></description>
			<content:encoded><![CDATA[<p>A common problem with forms is the annoying resubmit when the user reloads or refreshes a page. This can result in duplicate database records if not dealt with properly.</p>
<p>There are a number of simple solutions to this problem, I&#8217;ll go through a couple below.</p>
<p><strong>1. Use two files (one with the form on and one to process), and redirect back to the first page when the processing is done.</strong></p>
<p>Here&#8217;s some sample code for the form page (form.htm):</p>
<pre class="brush: php">&lt;form name="form1" method="post" action="process.php"&gt;
&lt;input type="text" name="text" id="text"&gt;
&lt;input type="submit" name="submit" id="submit" value="Submit"&gt;
&lt;/form&gt;
</pre>
<p>and here&#8217;s some sample code for the processing page (process.php):</p>
<pre class="brush: php">&lt;?php
if (isset($_POST['submit'])) {
$text = $_POST['text'];
$query = mysql_query("Insert into our database etc");
header("Location: form.htm");
}
?&gt;
</pre>
<p>This uses PHP header to redirect the user back to the form page when the form is processed, or if the user directly visits process.php without the form submission. Bear in mind this is only example code and so is not complete or secure.</p>
<p><strong>2. Use two files (one with the form on and to process and one when finished), redirecting to the second page when finished.</strong></p>
<p>Here is some sample code for the process/form page (form.php):</p>
<pre class="brush: php">&lt;?php
if (isset($_POST['submit'])) {
$text = $_POST['text'];
$query = mysql_query("Insert into our database etc");
}
header("Location: thanks.htm");
?&gt;
&lt;form name="form1" method="post" action="form.php"&gt;
&lt;input type="text" name="text" id="text"&gt;
&lt;input type="submit" name="submit" id="submit" value="Submit"&gt;
&lt;/form&gt;
</pre>
<p>and here&#8217;s the code for the final page (thanks.htm):</p>
<pre class="brush: php">&lt;p&gt;Thanks for filling out our form!&lt;/p&gt;
</pre>
<p>The user can still refresh the final page as much as they want as the processing was done elsewhere.</p>
<p><strong>3. Use one file, doing all the processing and then displaying a thank you message.</strong></p>
<p>This uses a $_GET to tell the script we&#8217;ve finished and display a message (form.php):</p>
<pre class="brush: php">&lt;?php
if ($_GET['success']) {
echo "Your text was saved successfully!";
} elseif (isset($_POST['submit'])) {
$text = $_POST['text'];
$query = mysql_query("Insert into our database etc");
header("Location: form.php?success=1");
}
?&gt;
&lt;form name="form1" method="post" action="form.php"&gt;
&lt;input type="text" name="text" id="text"&gt;
&lt;input type="submit" name="submit" id="submit" value="Submit"&gt;
&lt;/form&gt;
</pre>
<p>I suppose you could change the echo to a die if you didn&#8217;t want to show the form, or add in a final else like this:</p>
<pre class="brush: php">&lt;?php
if ($_GET['success']) {
echo "Your text was saved successfully!";
} elseif (isset($_POST['submit'])) {
$text = $_POST['text'];
$query = mysql_query("Insert into our database etc");
header("Location: form.php?success=1");
} else {
?&gt;
&lt;form name="form1" method="post" action="form.php"&gt;
&lt;input type="text" name="text" id="text"&gt;
&lt;input type="submit" name="submit" id="submit" value="Submit"&gt;
&lt;/form&gt;
&lt;?php } ?&gt;
</pre>
<p>so that the form would not be displayed when the Success text was.</p>
<p><strong>4. Involve sessions<br />
</strong></p>
<p>You could also involve sessions in your form by storing the POST variables in a session before processing, eg:</p>
<pre class="brush: php">$_SESSION['postdata'] = $_POST;
</pre>
<p>and clearing the session once the processing had been done, eg:</p>
<pre class="brush: php">unset($_SESSION['postdata'])
</pre>
<p>You could also enter a timestamp with each posted form and store this in a database, checking before processing for identical timestamps, it depends on the nature of your application and your personal preference.</p>
<p>Any comments/improvements welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3cc.org/2010/08/resubmitting-forms-upon-page-refresh-or-reload-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple PHP Login Systems and PHP Header &#8211; Break them with cURL</title>
		<link>http://www.3cc.org/2010/08/simple-php-login-systems-and-php-header-break-them-with-curl/</link>
		<comments>http://www.3cc.org/2010/08/simple-php-login-systems-and-php-header-break-them-with-curl/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 21:37:32 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://3cc.org.s92871.gridserver.com/?p=120</guid>
		<description><![CDATA[Most of the simple, free PHP scripts out there for user login systems have several large flaws in them, and I&#8217;ll admit that my earliest sites weren&#8217;t safe from what I&#8217;m going through today. cURL is a valuable tool for any PHP developer and has extremely useful applications, one of which is logging into remote [...]]]></description>
			<content:encoded><![CDATA[<p>Most of the simple, free PHP scripts out there for user login systems have several large flaws in them, and I&#8217;ll admit that my earliest sites weren&#8217;t safe from what I&#8217;m going through today.</p>
<p>cURL is a valuable tool for any PHP developer and has extremely useful applications, one of which is logging into remote sites to post forms. This illustrates a flaw with many sites in that they user header redirects (as shown below) in their login systems to keep people away from pages they shouldn&#8217;t see, and log them in and out.</p>
<pre class="brush: php">if ($_SESSION['logged'] != 1) {
header("Location: login.php");
}</pre>
<p>That very simple bit of code checks to see whether a session has been found where logged = 1. If it doesn&#8217;t find one then it will redirect the user to login.php without showing them the page.</p>
<p>The problem here is that header location ONLY works when it&#8217;s the first thing on a page, otherwise you&#8217;ll see an error and then the rest of the page we shouldn&#8217;t be seeing &#8211; which is what happens with a cURL request. This means the header location redirect doesn&#8217;t work and anyone can grab your &#8220;protected&#8221; page easily and quickly without logging in.</p>
<p>To solve this quickly (without changing your basic login script at all) make sure you have failsafes! The most simple would be to add a die statement as below so that if your redirect fails, they won&#8217;t get to see your private page.</p>
<pre class="brush: php">if ($_SESSION['logged'] != 1) {
header("Location: login.php") or die("Please &lt;a href='login.php'&gt;login&lt;/a&gt; before accessing this page.");
}</pre>
<p>Very simple stuff, but there&#8217;s lots of sites without it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3cc.org/2010/08/simple-php-login-systems-and-php-header-break-them-with-curl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Register_Globals and Session Side Effects in PHP</title>
		<link>http://www.3cc.org/2010/07/register_globals-and-session-side-effects-in-php/</link>
		<comments>http://www.3cc.org/2010/07/register_globals-and-session-side-effects-in-php/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 16:11:14 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[register_globals]]></category>
		<category><![CDATA[sessions]]></category>

		<guid isPermaLink="false">http://www.3cc.org/backyard/?p=108</guid>
		<description><![CDATA[Wrote a little login script (as part of a larger project) which uses PHP to store sessions with a user&#8217;s data in and got the following error. Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as [...]]]></description>
			<content:encoded><![CDATA[<p>Wrote a little login script (as part of a larger project) which uses PHP to store sessions with a user&#8217;s data in and got the following error.</p>
<p><em>Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0</em></p>
<p>Oh, well that&#8217;s no good. Looked through the code, and found I had spelt one variable wrong.</p>
<pre class="brush: php">$_SESSION['user_id'] = $user;</pre>
<p>but $user did not exist, it should have been</p>
<pre class="brush: php">$_SESSION['user_id'] = $user_id;</pre>
<p>So the error/warning appeared because I was referencing a null (or undefined) variable into a session; something which would require register_globals to work.</p>
<p>Register globals would allow you to call file.php?foo=bar; and the script would create a variable $foo with a value of bar automatically &#8211; which is not very secure as variables can be pushed into a script (perhaps bypassing form validation etc). Personally I would never use register_globals (set it to off in PHP.ini or your .htaccess) as mistakes like the one I made would go unnoticed &amp; could be abused.</p>
<p>I googled the error after I found that mistake to confirm that was the problem and was amazed to see people simply turning the warnings off rather than fixing the problem&#8230;talk about short cuts!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3cc.org/2010/07/register_globals-and-session-side-effects-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validating email addresses in PHP with Preg filters, DNS, MX Servers and other checks</title>
		<link>http://www.3cc.org/2010/04/validating-email-addresses-in-php-with-preg-filters-dns-mx-servers-and-other-checks/</link>
		<comments>http://www.3cc.org/2010/04/validating-email-addresses-in-php-with-preg-filters-dns-mx-servers-and-other-checks/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 10:27:17 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[preg]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.3cc.org/backyard/?p=46</guid>
		<description><![CDATA[There are many tutorials online that show users how to validate an email address, but most do it wrong. This means many websites will reject valid addresses such as customer/department=shipping@example.com or abc!def!xyz%yyy@example.com (yes, they are valid!) with the following expression: "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$" If you&#8217;re here through search then you&#8217;ve probably already seen a load of these! [...]]]></description>
			<content:encoded><![CDATA[<p>There are many tutorials online that show users how to validate an email address, but most do it wrong. This means many websites will reject valid addresses such as customer/department=shipping@example.com or abc!def!xyz%yyy@example.com (yes, they are valid!) with the following expression:</p>
<pre class="brush: php">"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"
</pre>
<p>If you&#8217;re here through search then you&#8217;ve probably already seen a load of these!</p>
<p>The code I&#8217;m going to provide:</p>
<ol>
<li>Allows international domains, and special characters in the email address</li>
<li>Checks for domain existance</li>
<li>Checks for mx records</li>
</ol>
<p>So&#8230;to the code. This was put together from a number of sources, then simply based off an article over at <a href="http://www.linuxjournal.com/article/9585">LinuxJournal</a>.</p>
<pre class="brush: php">function ValidateEmail($email) {
// Set test to pass
$valid = true;
// Find the last @ in the email
$findats = strrpos($email, "@");
// Check to see if any @'s were found
if (is_bool($findats) &amp;&amp; !$findats) {
$valid = false;
}
else {
// Phew, it's still ok, continue...
// Let's split that domain up.
$domain = substr($email, $findats+1);
$local = substr($email, 0, $findats);
// Find the local and domain lengths
$locallength = strlen($local);
$domainlength = strlen($domain);
// Check local (first part)
if ($locallength &lt; 1 || $locallength &gt; 64) {
$valid = false;
}
// Better check the domain too
elseif ($domainlength &lt; 1 || $domainlength &gt; 256) {
$valid = false;
}
// Can't be having dots at the start or end
elseif ($local[0] == '.' || $local[$locallength-1] == '.') {
$valid = false;
}
// Don't want 2 (or more) dots in the email
elseif ((preg_match('/\\.\\./', $local)) || (preg_match('/\\.\\./', $domain))) {
$valid = false;
}
// Make sure the domain has valid chars
elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
$valid = false;
}
// Make sure the local has valid chars, make sure it's quoted right
elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&amp;`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) {
$valid = false;
}
}
// Whoa, made it this far? Check for domain existance!
elseif (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
$valid = false;
}
}
if ($valid) {
echo $email.' is valid!';
}
else {
echo $email.' is not valid!';
}
}
</pre>
<p>You&#8217;d call this with:</p>
<pre class="brush: php">ValidateEmail("test@3cc.org");
</pre>
<p>Fancy trying it? <a title="Validate Email Demo" href="http://www.3cc.org/projects/demos/validateemail.php" target="_blank">Click here to demo.</a></p>
<p>Or if you want this code with all the spaces, <a title="Validate Email text" href="http://www.3cc.org/projects/demos/validateemail.txt" target="_blank">click here to view the txt</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.3cc.org/2010/04/validating-email-addresses-in-php-with-preg-filters-dns-mx-servers-and-other-checks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

