Category: Web Development

Apache: Force HTTPS with .htaccess

By blobaugh, June 3, 2010 7:47 pm

When HTTPS is required and you are working with an Apache server that parses .htaccess files, here is a handy snippet of code to put in the .htaccess that will cause the server to force every page in that directory and all subdirectories to use HTTPS by sending them to the HTTPS URI if they are not already there.

 
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
 

PHP: Function to force HTTPS

By blobaugh, June 3, 2010 7:44 pm

Quite often HTTPS is required for security reasons, but the question is how to force HTTPS with PHP?  Really it is quite simple. Copy the following function into your functions file and call it on top of every page that requires HTTPS to ensure it is used.

 
function forceHttps() {
 $pageURL = 'http';
 if (!isset($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] != "on") {
	header("Location: https://" . $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
 }
}
 

Typo3: Count the number of Pages, Content, and Words in your install

By blobaugh, May 17, 2010 6:25 am

Ever wonder how many pages, words or tt_content element you have in your Typo3 install? I am working with two large Typo3 sites and I was curious so I wrote a little piece of code. It works really well and is super quick. Here is what the output looks like:

Counting Pages
SELECT COUNT(uid) AS num_pages FROM `pages`Counting Pages Not Deleted
SELECT COUNT(uid) AS num_pages FROM `pages` WHERE deleted='0'

Counting Content
SELECT COUNT(uid) AS num_content FROM `tt_content`

Counting Words
SELECT bodytext FROM `tt_content`

Totals
Num Pages: 4505
Num Pages Not Deleted: 2948
Num Content: 5152
Num Words: 28,394,588

If you would like to implement this yourself I provided the code below, instead of reinventing the wheel yourself.

(I put mine in the typo3conf folder for ease of use)

 
<?php
/* gather the db connection info from typo3 */
require_once('localconf.php');
error_reporting(E_ALL);
$mysqli = new mysqli($typo_db_host, $typo_db_username, $typo_db_password, $typo_db);
 
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 
/* db queries */
$sql_count_pages = "SELECT COUNT(uid) AS num_pages FROM `pages`";
$sql_count_pages_not_deleted = "SELECT COUNT(uid) AS num_pages FROM `pages` WHERE deleted='0'";
$sql_count_content = "SELECT COUNT(uid) AS num_content FROM `tt_content`";
$sql_bodytext = "SELECT bodytext FROM `tt_content`";
 
/* useful vars */
$num_pages = 0;
$num_pages_not_deleted = 0;
$num_content = 0;
$num_words = 0;
 
echo '<b>Counting Pages</b>';
echo '<br/>'.$sql_count_pages;
$result = $mysqli->query($sql_count_pages);
$result = $result->fetch_assoc();
$num_pages = $result['num_pages'];
 
echo '<b>Counting Pages Not Deleted</b>';
echo '<br/>'.$sql_count_pages_not_deleted;
$result = $mysqli->query($sql_count_pages_not_deleted);
$result = $result->fetch_assoc();
$num_pages_not_deleted = $result['num_pages'];
 
echo '<br/><br/><b>Counting Content</b>';
echo '<br/>'.$sql_count_content;
$result = $mysqli->query($sql_count_content);
$result = $result->fetch_assoc();
$num_content = $result['num_content'];
 
echo '<br/><br/><b>Counting Words</b>';
echo '<br/>'.$sql_bodytext;
$result = $mysqli->query($sql_bodytext);
while($r = $result->fetch_assoc()) {
	$num_words += str_word_count($r['bodytext']);
}
 
echo '<br/><br/><br/><b>Totals</b>';
echo '<br/>Num Pages: ' . $num_pages;
echo '<br/>Num Pages Not Deleted: ' . $num_pages_not_deleted;
echo '<br/>Num Content: ' . $num_content;
echo '<br/>Num Words: ' . number_format($num_words, 0, '.', ',');
 
$mysqli->close();
?>
 

New Website Launch: SirGecko

By blobaugh, May 3, 2010 10:55 pm

The SirGecko development group has been around for years building web-enabled projects, but up until now they have not had their own website. I was asked to build them a site and problem solved. Click the image below to visit the shiny new site.

SirGecko.com Screenshot

WP SlimStat 2 Now Available

By blobaugh, April 22, 2010 2:49 pm

I have long used the WP SlimStat plugin on my WordPress sites to track site usage. Unfortunately the plugin was rather old. Just recently though the author updated the plugin. The new version is beautiful and incorporates a ton more usability. I highly recommend installing this new plugin.

WP SlimStat 2

Build a Basic Authorize.net Payment Form

By blobaugh, March 10, 2010 7:44 pm

Zac Vineyard wrote a great tutorial on building a basic Authorize.net payment for and connecting to their payment gateway. Zac used some of my code and was kind enough to reference me for it, and since he wrote such a clean tutorial I see no need to write my own (used my code anywho!).

Check his out!

Build a Basic Authorize.net Payment Form

PHP: A new kind of template engine

By blobaugh, February 21, 2010 12:09 am

I have been developing a custom Content Management System (CMS) for Blam Designs and though there are several really good templating engines out there for PHP (such as Smarty) I find them usually bulky. Then there is also the need to learn the templating language as well. Though the templating languages are simple, it is one more thing for a designer to worry about knowing that pulls them away from their designs.

With that in mind I set out to find or create a new kind of templating system that works in a way similar to the JavaScript DOM, but works with PHP. The beauty of this sort of a system lies in the fact that a designer can create a layout with all the filler content and dummy text they can imagine and they do not have to remove any of it, or add any special template tags before uploading their snazzy layout to a web server. Simply add a normal HTML id attribute to whatever it is the designer wants to place content in and the template engine will automagically take care of the rest for you!

For example:

 
<div id="content">Lorem ipsum solet dolomar</div>
 

Might be replaced with:

 
<div id="content">Hello World! I am some content</div>
 

Sounds pretty cool doesn't it? I wound up creating my own PHP classes to handle this for me. I ran across a library called PHP Simple HTML DOM Parser (SHDP) which does all the heavy lifting for me. My class it basically a wrapper object that makes it easier to deal with the SHDP, in addition to providing some extra functionality. Using my class it is possible to replace the entire contents of a tag (such as <title>), replace by the element id (such as <span id="myid">), or through the use of "special" tags. The "special" tags I call special because they do not fit inside the rest of the template paradigm. A special tag works in much the same manner as a tag from a normal template system like Smarty would. Tags are created using curly braces and look similar to {{my_tag}}. I needed to keep these in in the case that some special piece of code needs to be dynamically added to the template, but is not an HTML tag or id. This happens with things such as file paths.

Though I may not be fully done tweaking the new template engine it is fully operational and I am going to provide a few code example and a download of my class with SHDP packaged with it. Eventually I would like to setup a new site dedicated to this template engine, and would like to fully re-write SHDP so that the template system is one complete unit, not relying on any external libraries.

HTML File:

 
<html>
<head>
	<title>Template Engine Demo</title>
	<meta name="author" content="Ben Lobaugh">
</head>
<body>
<div id="contents" style="border: 1px dashed #000000">
This is inside the contents div
</div>
<div id="d2" style="margin-top: 20px; border: 1px solid #000000">
This is inside the d2 div
</div>
 
{{special_tag}}
</body>
</html>
 

Replace the contents of a tag:

$t->setTag('title', 'This is a new title');

Replace the contents of an element by id:

$t->setById('contents', 'Hello World! I am new content');

Append the contents of an element by id:

$t->setById('d2', ' I am appended content', 'a');

Set a special tag

$t->setSpecialTag('special_tag', 'I am special!');

Final Output:

 
<html>
<head>
	<title>This is a new title</title>
	<meta name="author" content="Ben Lobaugh">
</head>
<body>
<div id="contents" style="border: 1px dashed #000000">Hello World! I am new content</div>
<div id="d2" style="margin-top: 20px; border: 1px solid #000000">
This is inside the d2 div
 I am appended content</div>
 
I am special!
</body>
</html>
 

Please be sure to template_engine_demo the template engine to see all the working examples.

If you use this template engine on your site please drop me a comment below.

Enjoy :D

New Website Launch: Integrity Excavation and Rock Products

comments Comments Off
By blobaugh, February 6, 2010 3:53 pm

A friend of mine recently took a course on HTML web design and after completing her class she developed a website for Integrity Excavation and Rock Products. As a first site she did a great job. Go check it out.

Integrity Excavation and Rock Products

jQuery: Oh How I Love Thee

By blobaugh, November 16, 2009 5:03 pm

I started using jQuery last week and I am finding it to be a great library so far. It is very easy to interact with elements on the page in fun and creative ways.

As a sample, I have been working with the Northwest Nazarene University web team and they wanted to develop a simple wizard they can put on any page that will help prospective students find the degree they are interested in. I decided to tackle that task. At first I was looking at slide show sample, then one of the other developers showed me the beauty of .load(), which allows me to load into any arbitrary element the contents of any file or web address. After playing with .load() and the event handlers for an hour I figured out a way to implement what they wanted. It took me maybe 4 hours going from ground zero of no knowledge on the subject to implementing a working system complete with calls to a backend PHP file that gets the contents for the wizard from the database. jQuery is really neat, I highly encourage other web developers to check it out. A working sample may be found here or on NNU's website.

Here is the jQuery code I used. Try not to be too critical, I am positive there has to be a better way of doing this. I still am learning the vast amount of options available to me in jQuery.

 
<script type="text/javascript">
 
$(document).ready(function() {
	$('#programs').load('programs.php', {}, function() { // Load the degree options
           $('.choice').bind('click', function() { // Load the school in that degree
	        $('.choice').bind('click', function() { // Load the list of degrees
						$('#programs').load($(this).attr('href'), {}, function () {
							$('.choice').bind('click', function() {
								$('#programs').load($(this).attr('href'), {}, function () { // Load a degree
					                        });
							});
					});
				});			
		 });
	});
 });
</script>
 

New Website Launch: Lewis County Chaplaincy

By blobaugh, August 17, 2009 8:54 pm

Launch another website this week. This one is for the Lewis County Chaplaincy, a group that serves the needs of emergency services personnel as well as people throughout the community in need.

Check out the Lewis County Chaplaincy website.

Theme by Blam Designs
Based on Themocracy