<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>PHP and Web Development Blog</title>
	
	<link>http://www.ebrueggeman.com/blog</link>
	<description>Tips and Tricks for Web Developers</description>
	<pubDate>Thu, 19 Feb 2009 06:04:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/PhpAndWebDevelopmentBlog" type="application/rss+xml" /><item>
		<title>Easy Ways to Improve Site Performance</title>
		<link>http://www.ebrueggeman.com/blog/php/easy-ways-to-improve-site-performance/</link>
		<comments>http://www.ebrueggeman.com/blog/php/easy-ways-to-improve-site-performance/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 06:04:39 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Wordpress]]></category>

		<category><![CDATA[web hosting]]></category>

		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=54</guid>
		<description><![CDATA[Often as a site grows, its performance degrades sharply. This can happen for many reasons, and is typical for a site that continually undergoes maintenance. As a project ages, the content often grows as well. More data, more code revisions, more ideas to turn into code for the site, etc.
I&#8217;m exploring this subject because a [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/lJmdHJreG8Clwjf4afEzsYbteB8/0/da"><img src="http://feedads.g.doubleclick.net/~a/lJmdHJreG8Clwjf4afEzsYbteB8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/lJmdHJreG8Clwjf4afEzsYbteB8/1/da"><img src="http://feedads.g.doubleclick.net/~a/lJmdHJreG8Clwjf4afEzsYbteB8/1/di" border="0" ismap="true"></img></a></p><p>Often as a site grows, its performance degrades sharply. This can happen for many reasons, and is typical for a site that continually undergoes maintenance. As a project ages, the content often grows as well. More data, more code revisions, more ideas to turn into code for the site, etc.</p>
<p>I&#8217;m exploring this subject because a site of mine is in need of a performance boost. It was a snappy little site when I first launched it, but I&#8217;ve had many more ideas since then, and I&#8217;ve implemented almost all of them. Now regular browsing through the site is noticeable slower.</p>
<p>I&#8217;ve got a short list of performance enhancing ideas that I&#8217;m going to explore below.</p>
<h3>Separating CSS and JS Files</h3>
<p>For starters, if you have inline CSS or JS, you should separate these into separate files. This is a huge performance win, because a browser can cache mystyles.css or javastuff.js, but can&#8217;t cache it if it&#8217;s included inline in your page source code. Most performance wins happen client side - taking advantage of caching is often more efficient than making slight changes to server side code, though this isn&#8217;t a rule that&#8217;s set in stone; You could have terrible server side code that slows down the site too. My site in question already has the CSS and JS files separated, but it&#8217;s worth mentioning in case you haven&#8217;t done this.</p>
<h3>Minifying CSS and JS Files</h3>
<p>This is something that enterprise many sites do, so I thought I would go ahead and do this. Basically, it means removing spaces,tabs, and line returns wherever possible so that the file size is smaller, but the code executes as normal. When minifying a CSS file, I prefer to leave one CSS selector per line, and then listing each CSS property after it. This reduces file size, but makes the file still human readable/editable.</p>
<p>Here is an example of a fully minified CSS file from MSN - <a href="http://tk2.stc.s-msn.com/br/hp/11/en-us/css/hp_1.css" target="_blank">http://tk2.stc.s-msn.com/br/hp/11/en-us/css/hp_1.css</a> - I think this is overdoing it, and doing it this way is probably done through an automated script, as this would be next to impossible for a developer to edit in a development environment.</p>
<p>For complex sites, this is going to be a better win than for simple sites. I saw about a 20% decrease in file size. For very, very simple sites (read very), using an external CSS file can slow things down because of the server disk access time of retrieving the external CSS file. The original Google page, for example, only has a few lines of CSS, and therefore uses inline CSS. For anything more complicated, include it in an external file.</p>
<h3>Reducing Image Use</h3>
<p>Many blogs and news oriented sites use too many images. This slows things down for first time readers, who don&#8217;t have the images cached. A good tool to examine load times of everything on your site is Firebug, a plugin for Firefox. You can get the latest version of Firebug here. The image below is a screen capture of one of the features in Firebug that lets you examine exactly how long everything took to load. If your site is nearing half a megabyte (~500kb), it&#8217;s time to put less on your pages, or use smaller images.</p>
<p style="text-align: center;"><a href="http://www.ebrueggeman.com/blog/wp-content/uploads/2009/02/firebug-example.png"><img class="size-medium wp-image-56 aligncenter" title="firebug-example" src="http://www.ebrueggeman.com/blog/wp-content/uploads/2009/02/firebug-example-300x215.png" alt="firebug-example" width="300" height="215" /></a></p>
<h3>Using Less Database Queries</h3>
<p>This is very important on some sites, and irrelevant on others. If you are using a basic installation of Wordpress, for example, there really aren&#8217;t too many ways for an average person to speed it up, without writing the developers and telling them to use less queries next release. For heavily customized sites based on existing platforms and fully custom database driven sites, database queries can be the make or break of your site performance. Basic rule: if you are writing your own queries, and you don&#8217;t know how they perform, then you shouldn&#8217;t be writing your own queries. Having said that, never outright trust queries from other somewhat experienced developers. Developing for my firm, a Fortune 500 company, we have often encountered popular plugins for Wordpress or modules for Drupal that have queries in them that are written poorly. Just because another <em>developer</em> made it, doesn&#8217;t mean they did it right.</p>
<p>Another way to use less queries, is to outright display less content. This is easy to accomplish if you&#8217;re using a blog platform. If you&#8217;ve got 20 posts a page, reduce this number. The number of queries your blog site makes is directly proportional to the number of posts or entries it is trying to display.</p>
<h3>Better Hosting Plan or Better Hardware</h3>
<p>This isn&#8217;t an option for me, because the next level of hosting plans with GoDaddy is out of my current budget range. For others, however, it may be an option. It&#8217;s worth looking into.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php/easy-ways-to-improve-site-performance/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Wordpress Upgrades in Locked Down Environments</title>
		<link>http://www.ebrueggeman.com/blog/mysql/wordpress-upgrades-in-locked-down-environments/</link>
		<comments>http://www.ebrueggeman.com/blog/mysql/wordpress-upgrades-in-locked-down-environments/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 02:54:26 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[Wordpress]]></category>

		<category><![CDATA[enterprise]]></category>

		<category><![CDATA[Upgrading Wordpress]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=51</guid>
		<description><![CDATA[Wordpress, the engine that is behind the post you are currently reading, is easy to upgrade with most hosting environments. However, upgrades can get tricky in locked-down enterprise environments. 
For the most secure environment, the MYSQL database user that is used by Wordpress (hardcoded in your wp-config.php file) to retrieve and set information should not [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/AtjhjFdxpWYvZLNuJl-A0v4UvPc/0/da"><img src="http://feedads.g.doubleclick.net/~a/AtjhjFdxpWYvZLNuJl-A0v4UvPc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/AtjhjFdxpWYvZLNuJl-A0v4UvPc/1/da"><img src="http://feedads.g.doubleclick.net/~a/AtjhjFdxpWYvZLNuJl-A0v4UvPc/1/di" border="0" ismap="true"></img></a></p><p>Wordpress, the engine that is behind the post you are currently reading, is easy to upgrade with most hosting environments. However, upgrades can get tricky in locked-down enterprise environments. </p>
<p>For the most secure environment, the MYSQL database user that is used by Wordpress (hardcoded in your wp-config.php file) to retrieve and set information should not have Drop, Alter, and Create database privileges. If it did, malicious code would have an easier time disrupting the database. This is a problem because upgrading Wordpress usually involves some major additions or changes to the database structure, for which these privileges are needed.</p>
<p>So what is the solution? Basically it is running the upgrade as the root user. But, hardcoding root credentials in your wp-config.php file for a little bit is not good security practice, even if you change it back afterward. What you need to do is run an SQL script with all the database statements that are required for upgrade directly on the MYSQL server, or by using the MYSQL command line client.</p>
<p>Capturing the upgrade sequence is a little more difficult. Most enterprise installations have a development environment with looser security, possibly only accessible from inside the firewall of your organization. If your development environment is setup with a database user that does have alter, drop, and create credentials you&#8217;re in good shape. </p>
<p>Open up the /wp-includes/wp-db.php file and find the query() function. This is the function that Wordpress calls before each and every database call. On the first line of the function, add this line: </p>
<p>error_log($query); </p>
<p>This will output each query into your PHP error log file. Empty this file before attempting an upgrade on your development environment. Wordpress upgrades involve replacing all core files, and then logging in to the admin interface and upgrading the database by clicking a prominent upgrade button. After you have done this, the error log file will contain all the queries run by the upgrade.</p>
<p>There are more queries in here than are necessary to upgrade Wordpress. Delete all select queries, leaving a combination of alter, drop, create, or insert statements. Rename the error log file upgrade.sql and you&#8217;ve got the entire database upgrade in a ready to run script.</p>
<p>Publish the changed files up to your live environment. Your site will stay up even if a database change is required in the back end - this is a feature of Wordpress allowing for easier upgrades without site downtime. Next, run your upgrade.sql script with root privileges, or send it to your DBA team to run. You&#8217;re all upgraded, and you kept your site secure the entire time.</p>
<p>This same process can be used for upgrades to Wordpress Plugins, which often also require Alter, Create, and Drop statements to their tables.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/mysql/wordpress-upgrades-in-locked-down-environments/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Preventing Cross-Site Scripting and SQL Injection in PHP</title>
		<link>http://www.ebrueggeman.com/blog/mysql/preventing-cross-site-scripting-and-sql-injection/</link>
		<comments>http://www.ebrueggeman.com/blog/mysql/preventing-cross-site-scripting-and-sql-injection/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 14:45:37 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[cross-site scripting]]></category>

		<category><![CDATA[php security]]></category>

		<category><![CDATA[security]]></category>

		<category><![CDATA[SQL injection]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=49</guid>
		<description><![CDATA[Security is an important part of PHP programming, and PHP provides several tools for securing database queries and HTML display. However, knowing which function to use and when to use it can be somewhat confusing, as there&#8217;s many details to pay attention to. It&#8217;s important not to leave your website open to cross-site scripting or [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/2P7Pn-R-IP5a2gi7YddNoXovPB0/0/da"><img src="http://feedads.g.doubleclick.net/~a/2P7Pn-R-IP5a2gi7YddNoXovPB0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/2P7Pn-R-IP5a2gi7YddNoXovPB0/1/da"><img src="http://feedads.g.doubleclick.net/~a/2P7Pn-R-IP5a2gi7YddNoXovPB0/1/di" border="0" ismap="true"></img></a></p><p>Security is an important part of PHP programming, and PHP provides several tools for securing database queries and HTML display. However, knowing which function to use and when to use it can be somewhat confusing, as there&#8217;s many details to pay attention to. It&#8217;s important not to leave your website open to cross-site scripting or SQL injection attacks.</p>
<p>This example and explanation will focus on a common web scenario - a user submitting data via a form, being asked to confirm it, and then being displayed the data after it is stored in the database.</p>
<h3>1. Initial User Input</h3>
<p>In this scenario, a user will be presented an input box for which they can add a review about a particular product on a public facing website. The user is not allowed to use html, similar to the restrictions on product reviews on Amazon.com. The user inserts their review into the input box and then clicks submit.</p>
<h3>2. User Input Preview and Cross Site Scripting Prevention</h3>
<p>Now, we want to show the user their review and have them confirm it before we add it to the database. This is a prime example of a situation that leaves a website open for a XSS (cross-site scripting) attack. We don&#8217;t know what the user entered on the previous page, and they could be purposely entering malicious content. Because what they entered in going to be output to the screen, if they input raw PHP code, it could get executed unknowingly by our server. To prevent this, we need to html encode all potentially dangerous characters for display on the screen.</p>
<p>I would recommend using htmlentities() to do this, and use the ENT_QUOTES option as shown below, which means that both single and double quotes will also be encoded. Because we also decided that we wouldn&#8217;t allow HTML, we&#8217;ll want to strip that HTML before displaying the results, to give the user an accurate preview. Before doing any of this, we&#8217;ll have to grab the submitted data from the submitted form  - this tutorial won&#8217;t cover this, but there are plenty of web tutorials available for doing this.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//strip HTML tags from input data</span>
<span style="color: #000088;">$input_data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input_data</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//turn all characters into their html equivalent</span>
<span style="color: #000088;">$preview_data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input_data</span><span style="color: #339933;">,</span> ENT_QUOTES<span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//...display $preview_data </span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>3. Database Insert and SQL Injection Protection</h3>
<p>Now, let&#8217;s say the user previews above data, and clicks accept, which will send this data to another script for entry into the database. Protecting your database from SQL injection requires different steps than protecting against cross-site scripting. </p>
<p>It is very likely that you don&#8217;t want to store the user&#8217;s data in HTML encoded form. Let&#8217;s say you are using a varchar(32) column in your database, and you had an input box that was 32 characters long. If you were trying to store the HTML equivalent of this in your database, you would need a column that was much larger to guarantee that data didn’t get lost. This is because the HTML equivalent of a single character is often many characters long. For example, a double quote (&#8221;) becomes &quot;, and an ampersand (&#038;) becomes &amp;. We need to guard against single quotes, because these can cause an SQL injection problem, depending on how your database is setup. Consider this example:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;George'); DELETE FROM mytable; INSERT INTO mytable (name) VALUES ('you got hacked&quot;</span>;
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO mytable (name) VALUES ('$name')&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//...run the $sql query</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If your SQL server allows more than one SQL command on a single query request, you&#8217;ve just lost lots of data. While it&#8217;s a good idea to lock down your database server so it doesn&#8217;t allow this, you always want to secure your code independently of the database, in case you change your hosting setup later.</p>
<p>To secure your script, you can use the addslashes() function which will escape both single and double quotes, by adding backslashes before them, to prevent multiple queries from being executed. You&#8217;ll also want to test for the length of the input field, as forged form requests are easy as can be using tools like Firebug. Here is example code that will accomplish that:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//escape trouble characters</span>
<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//make sure not longer than expected length</span>
<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO mytable (name) VALUES ('$name')&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//...run the $sql query</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Note that there is the possibility of valid user input being cut off when being inserted into the database. If the user used all 32 characters in the input box, and a single quote was one of them, then the above code would trim off the last character. You may want to prevent this by using a 28 max character input box for a varchar(32) column, giving the user 4 opportunities to use an escaped character without having their input cut off.</p>
<h3>4. Retrieving and Displaying the Data From the Database</h3>
<p>Now that the data is safely in the database, everything is safe right? Wrong. We elected not to store HTML encoded data from users. This means that we are still at risk from a Cross Site Scripting attack every time we query and then display the data. What you&#8217;ll probably want to do is create a function that sanitizes database data before being displayed on the screen. No only should you HTML encode the data, but you&#8217;ll also want to remove the backslashes you added earlier, as these aren&#8217;t meant to be displayed. Here&#8217;s what your function could look like:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> sanitize_data<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input_data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input_data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> ENT_QUOTES<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/mysql/preventing-cross-site-scripting-and-sql-injection/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Creating Wordpress Posts with Thumbnails</title>
		<link>http://www.ebrueggeman.com/blog/php/creating-wordpress-posts-with-thumbnails/</link>
		<comments>http://www.ebrueggeman.com/blog/php/creating-wordpress-posts-with-thumbnails/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 14:21:17 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Wordpress]]></category>

		<category><![CDATA[post thumbnails]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=45</guid>
		<description><![CDATA[Creating blog posts is extremely easy in Wordpress, but adding simple thumbnails to your posts is not. I’m not talking about adding images within the post content, but adding them to your theme so they display in a standardized way in your blog loop, and on individual post pages.
The problem is that though Wordpress allows [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/QJWQ_jFczq4SJ6iFTEXa6U2sQqM/0/da"><img src="http://feedads.g.doubleclick.net/~a/QJWQ_jFczq4SJ6iFTEXa6U2sQqM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/QJWQ_jFczq4SJ6iFTEXa6U2sQqM/1/da"><img src="http://feedads.g.doubleclick.net/~a/QJWQ_jFczq4SJ6iFTEXa6U2sQqM/1/di" border="0" ismap="true"></img></a></p><p>Creating blog posts is extremely easy in Wordpress, but adding simple thumbnails to your posts is not. I’m not talking about adding images within the post content, but adding them to your theme so they display in a standardized way in your blog loop, and on individual post pages.</p>
<p>The problem is that though Wordpress allows inserting images within posts, there is no way to “attach” these images to the post, so that theme designers can pull standardized images out for display. By making a few changes to your Wordpress configuration and your theme templates, and by using the Post Thumbnails plugin that I have written, you will be able to add thumbnails to your site in an intelligent way.</p>
<h3>Wordpress Configuration</h3>
<p>First step is to login to your Wordpress admin dashboard and navigate to the Settings -> Miscellaneous page. Here you will need to change:</p>
<p>- Uncheck the “Organize my uploads into month- and year-based folders.”</p>
<p>- Change the Wordpress thumbnail size to your desired size. This is important! Take careful thought when making this decision – you will break old post’s thumbnails if you chose to change this value later. I recommend 50&#215;50 pixels.</p>
<h3>Plugin Installation</h3>
<p>Download the Post Thumbnail plugin using this <a href="http://www.ebrueggeman.com/blog/wp-content/php_examples/post-thumbnail.zip">link</a>. Unzip it and place the file within your wp-content/plugins/ directory. Navigate to the Plugins menu within the Wordpress admin dashboard and activate this plugin.</p>
<p>You’re going to need a default post thumbnail to be used when no image is associated with a post. Upload an image that is the same size as the thumbnail size you chose above to the wp-admin/uploads folder, or the same folder you have chosen for uploads if you’re using a custom location. On most installations that folder would be wp-content/uploads/.</p>
<p>After you’ve uploaded the image, paste the name into the first line of code in the plugin PHP file:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'DEFAULT_POST_THUMBNAIL'</span><span style="color: #339933;">,</span> <span style="">'default_thumbnail_file.jpg'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Change the above default_thumbnail_file.jpg to whatever your image was called.</p>
<h3>Plugin Usage</h3>
<p>After the plugin has been activated, you will notice a new box titled Post Thumbnail on the Wordpress Write and Edit screens. To get a thumbnail attached to your post, upload an image using Wordpress’ default image uploading mechanism, which is a button next to the Add Media label near the top of those screens. This opens up a popup window for uploading images. Upload your image, but you don’t need to insert it into the post. Instead copy the file name (ex. myimage.jpg) and then click on the X to close the window.</p>
<p>Next, paste the file name into the Source Image input field in the Post Thumbnail box and click save. After saving, you will notice that an image name will be displayed in the Generated Thumbnail box. This is the thumbnail version of your source image, that Wordpress resized for you when you initially uploaded the image.</p>
<p><img class="alignnone size-medium wp-image-47" title="admin_preview" src="http://www.ebrueggeman.com/blog/wp-content/uploads/2008/11/admin_preview-300x171.png" alt="" width="300" height="171" /></p>
<h3>Adding the Thumbnail to Your Theme</h3>
<p>Now that thumbnails are being attached to posts, we need to modify our theme to display them. It’s up to you to decide where you want to place them, but I would recommend adding the thumbnail to your template wherever you use the Wordpress loop. Below is a code snippet that inserts a thumbnail into the Default Wordpress theme’s index.php file, so thumbnails will show up on the main page of your site. Only one new line needs to be added.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;div class=&quot;post&quot; id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
    &lt;h2&gt;
    &lt;!-- Begin New Code--&gt;
    &lt;div style=&quot;float:left; margin:5px;&quot;&gt;
      <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> get_post_thumbnail<span style="color: #009900;">&#40;</span>get_the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;/div&gt;
    &lt;!-- End New Code--&gt;
    &lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; rel=&quot;bookmark&quot; title=&quot;Permanent Link to 
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title_attribute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&lt;/h2&gt;
    &lt;small&gt;
      <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_time<span style="color: #009900;">&#40;</span><span style="">'F jS, Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;!-- by <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> --&gt;
    &lt;/small&gt;
&nbsp;
    &lt;div class=&quot;entry&quot;&gt;
      <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="">'Read the rest of this entry &amp;raquo;'</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;/div&gt;
&nbsp;
    &lt;p class=&quot;postmetadata&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_tags<span style="color: #009900;">&#40;</span><span style="">'Tags: '</span><span style="color: #339933;">,</span> <span style="">', '</span><span style="color: #339933;">,</span> <span style="">'&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span> 
    Posted in <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_category<span style="color: #009900;">&#40;</span><span style="">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> | 
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> edit_post_link<span style="color: #009900;">&#40;</span><span style="">'Edit'</span><span style="color: #339933;">,</span> <span style="">''</span><span style="color: #339933;">,</span> <span style="">' | '</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>  
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> comments_popup_link<span style="color: #009900;">&#40;</span><span style="">'No Comments &amp;#187;'</span><span style="color: #339933;">,</span> 
    <span style="">'1 Comment &amp;#187;'</span><span style="color: #339933;">,</span> <span style="">'% Comments &amp;#187;'</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;/p&gt;
  &lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Here’s what my above post looked like with the above template code:</p>
<p><img class="alignnone size-medium wp-image-46" title="post_preview" src="http://www.ebrueggeman.com/blog/wp-content/uploads/2008/11/post_preview-300x193.png" alt="" width="300" height="193" /></p>
<p>Other templates like single.php and archive.php in the Default theme have very similar code where you can also insert the thumbnail. Your theme may differ, but usage is very simple. Just call the following function in your template wherever you want to display the thumbnail:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> get_post_thumbnail<span style="color: #009900;">&#40;</span>get_the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php/creating-wordpress-posts-with-thumbnails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Wordpress Scheduled Posts Issue</title>
		<link>http://www.ebrueggeman.com/blog/php/wordpress-scheduled-posts-issue/</link>
		<comments>http://www.ebrueggeman.com/blog/php/wordpress-scheduled-posts-issue/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 04:52:40 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Wordpress]]></category>

		<category><![CDATA[bugfix]]></category>

		<category><![CDATA[Wordpress bug]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=44</guid>
		<description><![CDATA[There has been much discussion about a recent bug/issue with Wordpress that prevents scheduled posts from working properly. The problem is somewhat unrelated to Wordpress itself, and is primarily a hosting setup issue. Scheduled posts are published via the use of a cron script. Wordpress attempts to call the cron script with an fsockopen() function [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/8Us7wYYjfkRUcpBfNy7uqPxTq1g/0/da"><img src="http://feedads.g.doubleclick.net/~a/8Us7wYYjfkRUcpBfNy7uqPxTq1g/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/8Us7wYYjfkRUcpBfNy7uqPxTq1g/1/da"><img src="http://feedads.g.doubleclick.net/~a/8Us7wYYjfkRUcpBfNy7uqPxTq1g/1/di" border="0" ismap="true"></img></a></p><p>There has been much discussion about a recent bug/issue with Wordpress that prevents scheduled posts from working properly. The problem is somewhat unrelated to Wordpress itself, and is primarily a hosting setup issue. Scheduled posts are published via the use of a cron script. Wordpress attempts to call the cron script with an fsockopen() function call. Many popular hosts disable the use of fopen() in their PHP configuration, which also disables the fsockopen() function, preventing your cron from firing which prevents your scheduled posts from being published.</p>
<p>A more robust way of programming this part of the code would be to try other functions like curl on failure of the original fsockopen() call. If curl is unavailable, then the calling script should just execute the code contained within the wp-cron.php file itself, instead of trying to make an http request to execute it.</p>
<p>I&#8217;ve put together a simple fix for sites that cannot use the fsockopen() function. It&#8217;s a simple addition, and just requires attention when upgrading. Steps for upgrading are detailed below the fix.</p>
<p>Open up your wp-cron.php file and copy the contents following the &#8220;if ( $_GET['check'] != wp_hash(&#8217;187425&#8242;) ) exit;&#8221; line.</p>
<p>Now you are going to have to paste these lines at the very end of the spawn_cron() function in wp-includes/cron.php. After pasting them in you are going to have to change the &#8220;if ( $argyle )&#8221; clause to be an if/else clause. This will cause the else clause to execute when fsockopen() fails. </p>
<p>In Wordpress 2.6.3, the spawn_cron() function within the wp-includes/cron.php file will now look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> spawn_cron<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$crons</span> <span style="color: #339933;">=</span> _get_cron_array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crons</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">return</span>;
&nbsp;
  <span style="color: #000088;">$keys</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$crons</span> <span style="color: #009900;">&#41;</span>;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$keys</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">return</span>;
&nbsp;
  <span style="color: #000088;">$cron_url</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span> <span style="">'siteurl'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="">'/wp-cron.php'</span>;
  <span style="color: #000088;">$parts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$cron_url</span> <span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="">'scheme'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="">'https'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// support for SSL was added in 4.3.0</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">version_compare</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">phpversion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="">'4.3.0'</span><span style="color: #339933;">,</span> <span style="">'&gt;='</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="">'openssl_open'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$port</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="">'port'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="">'port'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">443</span>;
      <span style="color: #000088;">$argyle</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span><span style="">'ssl://'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="">'host'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.01</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$port</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="">'port'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="">'port'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">80</span>;
    <span style="color: #000088;">$argyle</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span> <span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="">'host'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.01</span> <span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$argyle</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$argyle</span><span style="color: #339933;">,</span> 
      <span style="color: #0000ff;">&quot;GET {$parts['path']}?check=&quot;</span> <span style="color: #339933;">.</span> wp_hash<span style="color: #009900;">&#40;</span><span style="">'187425'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; HTTP/1.0<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
      <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;Host: {$_SERVER['HTTP_HOST']}<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
    <span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//BEGIN COPIED wp-cron.php CONTENTS</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> get_option<span style="color: #009900;">&#40;</span><span style="">'doing_cron'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #990000;">exit</span>;
&nbsp;
    update_option<span style="color: #009900;">&#40;</span><span style="">'doing_cron'</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #000088;">$crons</span> <span style="color: #339933;">=</span> _get_cron_array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000088;">$keys</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crons</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crons</span><span style="color: #009900;">&#41;</span> || <span style="color: #000088;">$keys</span><span style="color: #009900;">&#91;</span><span style="color:#800080;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">return</span>;
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$crons</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$timestamp</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$cronhooks</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">break</span>;
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cronhooks</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$hook</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$keys</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$keys</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$schedule</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="">'schedule'</span><span style="color: #009900;">&#93;</span>;
          <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$schedule</span> <span style="color: #339933;">!=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$new_args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$schedule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$hook</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="">'args'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="">'wp_reschedule_event'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$new_args</span><span style="color: #009900;">&#41;</span>;
          <span style="color: #009900;">&#125;</span>
          wp_unschedule_event<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$hook</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="">'args'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
          do_action_ref_array<span style="color: #009900;">&#40;</span><span style="color: #000088;">$hook</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="">'args'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    update_option<span style="color: #009900;">&#40;</span><span style="">'doing_cron'</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #666666; font-style: italic;">//END COPIED wp-cron.php CONTENTS</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Upgrading</h3>
<p>It is very important to remember that you&#8217;ll have to take special precautions when upgrading. When upgrading, you are going to have to perform the same copy/paste and alter within the wp-includes/cron.php file. Take whatever the contents of the wp-cron.php file are and copy them after the above mentioned hash check line.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php/wordpress-scheduled-posts-issue/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Easily Parse a String</title>
		<link>http://www.ebrueggeman.com/blog/php/easily-parse-a-string/</link>
		<comments>http://www.ebrueggeman.com/blog/php/easily-parse-a-string/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 06:46:55 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[parse]]></category>

		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=43</guid>
		<description><![CDATA[I recently came across the need to efficiently parse a string and find what was in between two &#8220;marker&#8221; strings. I was parsing a logfile with a particular preset format. The line looked like this:

$str = '2008-10-07_00:00:19 - ip:[213.21.198.231] page:[/phpsimplechat/documentation_usage.php]';

I wanted to be able to easily pull out the ip and page information in a [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/KdcHAslHjEZbfDssVpIwjOeKzA4/0/da"><img src="http://feedads.g.doubleclick.net/~a/KdcHAslHjEZbfDssVpIwjOeKzA4/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/KdcHAslHjEZbfDssVpIwjOeKzA4/1/da"><img src="http://feedads.g.doubleclick.net/~a/KdcHAslHjEZbfDssVpIwjOeKzA4/1/di" border="0" ismap="true"></img></a></p><p>I recently came across the need to efficiently parse a string and find what was in between two &#8220;marker&#8221; strings. I was parsing a logfile with a particular preset format. The line looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="">'2008-10-07_00:00:19 - ip:[213.21.198.231] page:[/phpsimplechat/documentation_usage.php]'</span>;</pre></div></div>

<p>I wanted to be able to easily pull out the ip and page information in a straight-forward way without repeating code. After a little tinkering I came up with a get_token() function that accomplished my goal nicely. The function is below:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_token<span style="color: #009900;">&#40;</span><span style="color: #000088;">$start_token</span><span style="color: #339933;">,</span> <span style="color: #000088;">$end_token</span><span style="color: #339933;">,</span> <span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> stripos<span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start_token</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000088;">$end</span> <span style="color: #339933;">=</span> stripos<span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$end_token</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span> <span style="color: #339933;">!==</span> <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$end</span> <span style="color: #339933;">!==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$start_pos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">+</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$start_token</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000088;">$end_pos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$end</span> <span style="color: #339933;">-</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end_token</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start_pos</span><span style="color: #339933;">,</span> <span style="color: #000088;">$end_pos</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let&#8217;s take my above example -  if I want to pull out the ip and page information from the given string, I only need to implement the below code:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="">'2008-10-07_00:00:19 - ip:[213.21.198.231] page:[/phpsimplechat/documentation_usage.php]'</span>;
<span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> get_token<span style="color: #009900;">&#40;</span><span style="">'ip:['</span><span style="color: #339933;">,</span> <span style="">']'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> get_token<span style="color: #009900;">&#40;</span><span style="">'page:['</span><span style="color: #339933;">,</span> <span style="">']'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The optional offset argument allows you to specify how many characters into the $haystack that you want to start looking;</p>
<p>Here is another example usage in which we want to pull the title out of some HTML code.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="">'This is the body'</span>;
<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> get_token<span style="color: #009900;">&#40;</span><span style="">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php/easily-parse-a-string/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP Coding Standards</title>
		<link>http://www.ebrueggeman.com/blog/php/php-coding-standards/</link>
		<comments>http://www.ebrueggeman.com/blog/php/php-coding-standards/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 19:41:52 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[coding standards]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=41</guid>
		<description><![CDATA[Those programmers who took computer science classes in C++ or Java and then started looking at popular open source PHP scripts may be surprised that the coding standards are somewhat different from what they are used to. Unfortunately, it&#8217;s hard to nail down good coding standards for PHP because it has so many syntaxes and [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/0nqHMsRb-QOpTm3g61aY4tJLZNs/0/da"><img src="http://feedads.g.doubleclick.net/~a/0nqHMsRb-QOpTm3g61aY4tJLZNs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/0nqHMsRb-QOpTm3g61aY4tJLZNs/1/da"><img src="http://feedads.g.doubleclick.net/~a/0nqHMsRb-QOpTm3g61aY4tJLZNs/1/di" border="0" ismap="true"></img></a></p><p class="MsoNormal">Those programmers who took computer science classes in C++ or Java and then started looking at popular open source PHP scripts may be surprised that the coding standards are somewhat different from what they are used to. Unfortunately, it&#8217;s hard to nail down good coding standards for PHP because it has so many syntaxes and does so many things. On one side, PHP is a serious object-oriented language and demanding of java-like syntax, but on another it’s a scripting language and very close to HTML.</p>
<p class="MsoNormal">
<p class="MsoNormal">One of the older and widely adopted PHP coding standards is based on Todd Hoff&#8217;s C++ Coding Standard and is available at <a href="http://www.dagbladet.no/development/phpcodingstandard/">http://www.dagbladet.no/development/phpcodingstandard/</a>. Though it is widely used, and (as of the time of writing this) the number one result for &#8220;php coding standards&#8221; on Google, I give this the least cred because not only was it adopted from a C++ standard but it was not put together by an open source community or company.</p>
<p class="MsoNormal">
<p class="MsoNormal">The two &#8220;most official&#8221; coding standards are the PEAR PHP Standards (<a href="http://pear.php.net/manual/en/standards.php">http://pear.php.net/manual/en/standards.php</a>) and the Zend Framework Coding Standard for PHP (<a href="http://framework.zend.com/manual/en/coding-standard.html">http://framework.zend.com/manual/en/coding-standard.html</a>).</p>
<p class="MsoNormal">
<p class="MsoNormal">There are also other PHP Standards the are specific to certain projects, like Drupal (<a href="http://drupal.org/coding-standards">http://drupal.org/coding-standards</a>) or Wordpress (<a href="http://codex.wordpress.org/WordPress_Coding_Standards">http://codex.wordpress.org/WordPress_Coding_Standards</a>), and are certainly valid as these frameworks have thousands of developers adding to their codebase, but they tend to be less detailed the above &#8220;official&#8221; coding standards.</p>
<p class="MsoNormal">
<p class="MsoNormal">I personally like the Zend standards the best, as they are the most detailed and easy to follow, but I actually prefer the variable and function naming convention of the Wordpress standard which uses all lowercase words separated by underscores (my_function()) as opposed to the more popular Java style function names (myFunction()) used by the other standards.</p>
<p class="MsoNormal">
<p class="MsoNormal">At some point in the future, I may write my own coding standards and publish them on this site, combining the best elements from each standard.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php/php-coding-standards/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP HTML Email Function</title>
		<link>http://www.ebrueggeman.com/blog/php/php-html-email-function/</link>
		<comments>http://www.ebrueggeman.com/blog/php/php-html-email-function/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 01:38:39 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[email]]></category>

		<category><![CDATA[functions]]></category>

		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=40</guid>
		<description><![CDATA[PHP provides the mail() function for easy sending of mail from your PHP server. The problem is that sending HTML email (the most popular format for emails) is not easily done using native PHP functions. I&#8217;ve provided below a somewhat foolproof function that will allow you to send HTML emails.
Simply pass in a to address, [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/FnmNorxDgVxOXJggjp1K8ccrmiA/0/da"><img src="http://feedads.g.doubleclick.net/~a/FnmNorxDgVxOXJggjp1K8ccrmiA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/FnmNorxDgVxOXJggjp1K8ccrmiA/1/da"><img src="http://feedads.g.doubleclick.net/~a/FnmNorxDgVxOXJggjp1K8ccrmiA/1/di" border="0" ismap="true"></img></a></p><p>PHP provides the mail() function for easy sending of mail from your PHP server. The problem is that sending HTML email (the most popular format for emails) is not easily done using native PHP functions. I&#8217;ve provided below a somewhat foolproof function that will allow you to send HTML emails.</p>
<p>Simply pass in a to address, from address, from name (which is used as a nickname in email clients instead of displaying the sender&#8217;s email address), subject, and a properly formatted HTML email message.</p>
<p>If you are sending to multiple recipients, you can pass the $to_email parameter in as an array of multiple email addresses.</p>
<h2>HTML Email Function</h2>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> send_email <span style="color: #009900;">&#40;</span><span style="color: #000088;">$to_email</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from_email</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$msg</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//split up to email array, if given</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to_email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$to_email_string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="">', '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$to_email</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$to_email_string</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$to_email</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Assemble headers</span>
	<span style="color: #000088;">$headers</span>  <span style="color: #339933;">=</span> <span style="">'MIME-Version: 1.0'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="">'Content-type: text/html; charset=iso-8859-1'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;From: $from_name &lt;$from_email&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
	<span style="color: #666666; font-style: italic;">//send via PHP's mail() function</span>
	<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to_email_string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$msg</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Usage</h2>
<p>You call the function like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">send_email<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;me@gmail.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;roger@att.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Roger&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Hello There&quot;</span><span style="color: #339933;">,</span> 
  <span style="color: #0000ff;">&quot;&lt;html&gt;Hello There &lt;strong&gt;Bob&lt;/strong&gt;! How are you doing&lt;/html&gt;!&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Or, alternately, you call it like this when sending to multiple email addresses:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">send_email<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;me@gmail.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;sal@gmail.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;roger@att.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Roger&quot;</span><span style="color: #339933;">,</span> 
  <span style="color: #0000ff;">&quot;Hello There&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;html&gt;Hello There &lt;strong&gt;Bob&lt;/strong&gt;! How are you doing&lt;/html&gt;!&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h2>HTML Email Formatting</h2>
<p>Remember that formatting HTML for email is different than for webpages. Do not use a CSS file and do not declare CSS in the head of your document. Instead, use either inline CSS or &#8220;Old School&#8221; HTML like font tags for formatting. Also, don&#8217;t use JavaScript, as major email clients don&#8217;t support it. </p>
<p>Though it may seem obvious, remember that images and links within your HTML email need to be full, absolute paths to their world wide web accessible location. No relative links!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php/php-html-email-function/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP trim_text() function - shorten text without cutting words in half</title>
		<link>http://www.ebrueggeman.com/blog/php/abbreviate-text-without-cutting-words-in-half/</link>
		<comments>http://www.ebrueggeman.com/blog/php/abbreviate-text-without-cutting-words-in-half/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 03:25:02 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[abbreviate]]></category>

		<category><![CDATA[functions]]></category>

		<category><![CDATA[trim]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=37</guid>
		<description><![CDATA[Here&#8217;s another PHP function I cannot live without - I add this into my standard PHP functions include on all projects. Often when displaying text, I am forced to abbreviate the text to a certain number of characters. You might jump in a decide to use substring() on your text to achieve this abbreviation, but [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/MLtL_DvL2lLICBWvfRKgw_ct6IU/0/da"><img src="http://feedads.g.doubleclick.net/~a/MLtL_DvL2lLICBWvfRKgw_ct6IU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/MLtL_DvL2lLICBWvfRKgw_ct6IU/1/da"><img src="http://feedads.g.doubleclick.net/~a/MLtL_DvL2lLICBWvfRKgw_ct6IU/1/di" border="0" ismap="true"></img></a></p><p>Here&#8217;s another PHP function I cannot live without - I add this into my standard PHP functions include on all projects. Often when displaying text, I am forced to abbreviate the text to a certain number of characters. You might jump in a decide to use substring() on your text to achieve this abbreviation, but that can cause several problems. The most blatant is that you will often split the text right in the middle of a word. In addition, if there are any HTML tags in the text, they could get cut in the middle too, or have the closing tag left off completely, potentially breaking the display or exposing the remaining part of the tag.</p>
<p>As a solution to these problems, I have written a function that only trims on the last space before the number of characters you specify, so it will never cut words in half. Also, it strips out HTML tags before doing the character trim, preventing possible display issues. And, as a convenience it adds ellipses (the &#8230;) to all trimmed text, as a visual cue to the reader that the text has been abbreviated.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #0000ff; font-style: italic;">/**
 * trims text to a space then adds ellipses if desired
 * @param string $input text to trim
 * @param int $length in characters to trim to
 * @param bool $ellipses if ellipses (...) are to be added
 * @param bool $strip_html if html tags are to be stripped
 * @return string 
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> trim_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ellipses</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000088;">$strip_html</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//strip tags, if desired</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$strip_html</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//no need to trim, already shorter than trim length</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$input</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//find last space within length</span>
	<span style="color: #000088;">$last_space</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="">' '</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000088;">$trimmed_text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$last_space</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #666666; font-style: italic;">//add ellipses (...)</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ellipses</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$trimmed_text</span> <span style="color: #339933;">.=</span> <span style="">'...'</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$trimmed_text</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php/abbreviate-text-without-cutting-words-in-half/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Simple PHP Security Framework</title>
		<link>http://www.ebrueggeman.com/blog/php/simple-php-security-framework/</link>
		<comments>http://www.ebrueggeman.com/blog/php/simple-php-security-framework/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 02:42:00 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[framework]]></category>

		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=39</guid>
		<description><![CDATA[Having a secure area to a PHP site is a common website requirement. Implementing the secure area, on the other hand, can be difficult and confusing. I&#8217;ve put together a basic security framework that you can use for your PHP based website.
What this framework is:

Simply coded &#038; easy to understand
Secure enough for most sites, blogs, [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/d21UEjAglvKEvHnsGIsCD0-hk2U/0/da"><img src="http://feedads.g.doubleclick.net/~a/d21UEjAglvKEvHnsGIsCD0-hk2U/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/d21UEjAglvKEvHnsGIsCD0-hk2U/1/da"><img src="http://feedads.g.doubleclick.net/~a/d21UEjAglvKEvHnsGIsCD0-hk2U/1/di" border="0" ismap="true"></img></a></p><p>Having a secure area to a PHP site is a common website requirement. Implementing the secure area, on the other hand, can be difficult and confusing. I&#8217;ve put together a basic security framework that you can use for your PHP based website.</p>
<p>What this framework is:</p>
<ul class="regular_list">
<li>Simply coded &#038; easy to understand</li>
<li>Secure enough for most sites, blogs, and internally accessed (intranet) sites.</li>
</ul>
<p>What this framework is NOT:</p>
<ul class="regular_list">
<li>Secure enough for sites involving e-commerce transactions and sensitive personal information.</li>
</ul>
<p>Note the &#8220;NOT&#8221; clause above - this implementation is not for securing sensitive data. Most people implementing this framework will be doing it on unsecure sites (sites NOT using the HTTPS secure socket layer), causing user passwords to be sent from script to script in plaintext, which is vulneable to intrusion. </p>
<h2>How the PHP security flow works</h2>
<p>This framework works as follows:</p>
<ul class="regular_list">
<li>User submits login form containing name and passsword to login processor script</li>
<li>Login processor form compares name and password to stored value</li>
<li>If name and password do not match expected value, send user back to login form</li>
<li>If name and password match expected value, set a session variable containing a unique hash value, and forward user to secure page</li>
</ul>
<p>When a user tries to access a secure page, this happens:</p>
<ul class="regular_list">
<li>Security code checks session variable with user hash and compares to expected value.</li>
<li>If the hash values match, continue displaying page.</li>
<li>If the hash values do not match, send user to login form.</li>
</ul>
<p>Seems simple right? It is! To implement the above workflow you&#8217;re going to need to use the code I have included below.</p>
<h3>Login Form</h3>
<p>Below is the login form code. Note that you must set the destination of the form to match the name of your check login script (login processor.)</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
* FILE: login.php
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Login&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;form name=&quot;login-form&quot; method=&quot;post&quot; action=&quot;check_login.php&quot;&gt;
    &lt;p&gt;User: &lt;input type=&quot;textfield&quot; id=&quot;user&quot; name=&quot;user&quot;/&gt;&lt;/p&gt;
    &lt;p&gt;Password: &lt;input type=&quot;password&quot; id=&quot;pass&quot; name=&quot;pass&quot;/&gt;&lt;/p&gt;
    &lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<h3>Login Processor</h3>
<p>The login processor has the main chunk of your security code. This is the script that decides whether you have a correct username and password, and what to do after logging in. Note that this code uses an include of login functions to accompish its purpose. These login functions are shown later in this article.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
* FILE: check_login.php
*/</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//include our login functions.</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="">'login_functions.php'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//retrieve post data</span>
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="">'pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/*
* Basic Login Logic
*/</span>
&nbsp;
clear_login_state<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>check_login_correct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//set appropiate session vars</span>
    login_user<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #666666; font-style: italic;">//redirect to secured page</span>
    send_to_page<span style="color: #009900;">&#40;</span><span style="">'secure_page.php'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//wrong user or password supplied, send back to login</span>
    send_to_page<span style="color: #009900;">&#40;</span><span style="">'login.php'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//no user or password supplied, send back to login</span>
  send_to_page<span style="color: #009900;">&#40;</span><span style="">'login.php'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>Login Functions</h3>
<p>The above Login Processor code uses some important functions to accomplish the secure login. These functions are enclosed in their own file and included by the above code.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
* FILE: login_functions.php
*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> check_login_correct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff; font-style: italic;">/**
  * This function is for you to fill in.
  * Typically, you would compare the user's password 
  * to the password stored in the database, and then return
  * either true or false, depending on the result.
  */</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span> <span style="color: #339933;">==</span> <span style="">'admin'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$pass</span> <span style="color: #339933;">==</span> <span style="">'Chelsea'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>; <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> login_user<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">session_regenerate_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #666666; font-style: italic;">//set the user session variable, for later app use</span>
  <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="">'user'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span>;
&nbsp;
  <span style="color: #666666; font-style: italic;">//set the hash session variable</span>
  <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="">'hash'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> calculate_secure_hash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//function sends the user to a page. Note this must be called </span>
<span style="color: #666666; font-style: italic;">//in the header, before any page output (echo's, html, print, etc) </span>
<span style="color: #000000; font-weight: bold;">function</span> send_to_page<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: $page&quot;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Redirect Failed&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//clears login state (logs you out) by unsetting login variables</span>
<span style="color: #666666; font-style: italic;">//must be called in header, before any page output (echo's, html, print, etc) </span>
<span style="color: #000000; font-weight: bold;">function</span> clear_login_state<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">session_unset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> calculate_secure_hash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//the security of your system is based on the hash seed below - change often</span>
  <span style="color: #000088;">$hash_seed</span> <span style="color: #339933;">=</span> <span style="">'this_is_a_secret'</span>;	
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="">'HTTP_USER_AGENT'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hash_seed</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> check_logged_in<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//retrieve session vars</span>
  <span style="color: #000088;">$found_hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="">'hash'</span><span style="color: #009900;">&#93;</span>;
  <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="">'user'</span><span style="color: #009900;">&#93;</span>;
&nbsp;
  <span style="color: #666666; font-style: italic;">//must not be empty</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$found_hash</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//recalculate the hash</span>
    <span style="color: #000088;">$calculated_hash</span> <span style="color: #339933;">=</span> calculate_secure_hash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #666666; font-style: italic;">//if recalculated hash matches, we have a logged in user</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$calculated_hash</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$found_hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      send_to_page<span style="color: #009900;">&#40;</span><span style="">'login.php'</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    send_to_page<span style="color: #009900;">&#40;</span><span style="">'login.php'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>Secure Page Code</h3>
<p>Okay, so we have successfully logged a user in - now what? Well, for each secure page accessed you have to reverify the hash value. On each secure page, you&#8217;re going to need to setup the top of your page (PHP script) like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
* FILE: secure_page.php
*/</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//include our login functions.</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="">'login_functions.php'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//do security check</span>
check_logged_in<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//now, display the page's content...</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;You are viewing a secured page!&quot;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<div id="download_code">
<h3>Download the Above Code</h3>
<p>You can <a href="http://www.ebrueggeman.com/blog/wp-content/php_examples/php_security_framework.zip">download this zip of the above code</a>, and run it on your server to test how the authentication code works. Remember, you&#8217;re going to need to customize the check_login_correct() function to fit your particular password storage method.
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php/simple-php-security-framework/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
