Skip to content

VirtualHost *:80 — mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results

Today while setting up an ssl certificate i obtained for my new installation of activecollab i ran into this error:

VirtualHost *:80 — mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results

To fix this i found out i have to add this to the beginning of my virtual host

NameVirtualHost *:443

Also I had to change

<VirtualHost *>

to

<VirtualHost *:443>

Categories: Apache.

Tags: , ,

WordPress as a CMS

This tutorial is aimed at developers who just want wordpress to function as a lightweight CMS, that is, wordpress will only be used for editing page content.

First install wordpress in whatever directory you want the admin to live. I chose /admin.

Any page that needs to use wordpress functionality you must add this:

require('/path/to/wp-blog-header.php');

This gives you access to wordpress functions that we need:

  • get_page – Get the page data from the database
  • wpautop – Automatically add paragraph tags to page content

To get page content:

$page_data = get_page( 4 ); // Where 4 is the page ID in wordpress

There is a bug in certain versions of php, if you get the error:

Fatal error: Only variables can be passed by reference…

Then use this format instead:

$page_id = 4;
$page_data = get_page( $page_id );

$page_data now contains all of the info for that page, and is ready for use in your templates/views. Check the function reference to see the data you have available to you.

To display post_content use the wp function wpautop.

<?= wpautop($page_data->post_content) ?>

Categories: Wordpress.

Tags:

PHP template engines and why they’re unnecessary

For the past 4 or 5 years I’ve been a proponent of using PHP as it’s own template engine. I cringe every time I see someone using smarty, btemplate, fastTemplate, etc… because I’ve never had to do anything in my html that couldn’t be done with PHP. Never! Why add a layer to your app if you don’t have to?

To illustrate this I’m going to show you a template written in a template engine, and then in PHP.

Here’s an example from the Smarty website

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<table border="0" width="300">
    <tr>
        <th colspan="2" bgcolor="#d1d1d1">Guestbook Entries (<a href="{$SCRIPT_NAME}?action=add">add</a>)</th>
    </tr>
    {foreach from=$data item="entry"}
        <tr bgcolor="{cycle values="#dedede,#eeeeee" advance=false}">
            <td>{$entry.Name|escape}</td>        
            <td align="right">{$entry.EntryDate|date_format:"%e %b, %Y %H:%M:%S"}</td>        
        </tr>
        <tr>
            <td colspan="2" bgcolor="{cycle values="#dedede,#eeeeee"}">{$entry.Comment|escape}</td>
        </tr>
    {foreachelse}
        <tr>
            <td colspan="2">No records</td>
        </tr>
    {/foreach}
</table>

Here’s an example using PHP. e() is an escaping function you write yourself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<table border="0" width="300">
    <tr>
        <th colspan="2" bgcolor="#d1d1d1">Guestbook Entries (<a href="{$SCRIPT_NAME}?action=add">add</a>)</th>
    </tr>
    <?php if ( count( $data ) ): ?>
    <?php foreach( $data as $entry ): ?>
        <tr bgcolor="<?php if( $i++ % 2 ): ?>#dedede<?php else: ?>#eeeeee<?php endif; ?>">
            <td><?= e( $entry['name'] ) ?></td>        
            <td align="right"><?= date( $entry['entryDate'], 'e b Y H M S' ) ?></td>        
        </tr>
        <tr>
            <td colspan="2" bgcolor="<?php if( $i % 2 ): ?>#dedede<?php else: ?>#eeeeee<?php endif; ?>"><?= e( $entry['comment'] ) ?></td>
        </tr>
     <?php endforeach; ?>
     <?php else: ?>
        <tr>
            <td colspan="2">No records</td>
        </tr>
     <?php endif; ?>
</table>

As you can see there is only 2 added lines of code. I had to add an if statement to account for smarty’s foreachelse. No added layer. No extra work for the interpreter. Just as readable (unless you’re a PHP developer, then it will be more readable)

In closing read this post on the sitepoint forums. Pay particular attention to Voostind’s replies (#1, #2, #3)

Categories: PHP.

Tags: ,

Change Firefox’s default search engine

I recently installed yahoo messenger, and in doing so the damn thing set yahoo as the default search engine for firefox. I will have none of that.

Here’s how to change it back to google:

  1. Go to about:config
  2. Search for keyword.URL
  3. Change the value to http://www.google.com/search?q=

Categories: Firefox.

Setting mysql connection charset with PDO

Recently while creating a store locator for a Chinese website I ran into a problem. Some Chinese characters were being displayed correctly and some weren’t. First thing I did was check the charset of the HTTP headers and the database… both were set correctly to utf-8. I figured it had to be a database issue so I began googling and found this.

Apparently you have to set the character set of the connection as well via:

SET NAMES 'utf-8'

SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES ‘cp1251′ tells the server “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.)

Great!

The only issue remaining was the fact that I didn’t want to have to run that query on every page.

I use PDO, and I found out you can use the driver_options argument of PDO to run an initial command.

1
2
3
4
5
6
7
8
$dsn = sprintf( 'mysql:dbname=%s;host=%s', DB_NAME, DB_HOST );
$driver_options = array( PDO::MYSQL_ATTR_INIT_COMMAND =&gt; 'SET NAMES utf-8' );
try {
    $dbc = new pdo($dsn, $user, $pw, $driver_options);
}
catch (PDOException $e) {
    // Handle exception
}

Good reads about character sets

Categories: MySQL, PHP.

Tags: ,

Returning an instance of a class from PDO

PDO can create and return an object(s) of a specific class. It can be a little tricky getting it to work though…

With fetchAll() you can pass ( PDO:FETCH_CLASS, ‘class_name’ ) directly

$sql = sprintf( 'select * from %s where prod_id=:prod_id', TABLE );
$findProducts = $this->db->prepare( $sql );
$findProduct->execute();
$products = $findProducts->fetchAll( PDO::FETCH_CLASS, 'Product' );

But with fetch() you must initiate it with setFetchMode()

$sql = sprintf( 'select * from %s where prod_id=:prod_id', TABLE );
$findProduct = $this->db->prepare( $sql );
$findProduct->setFetchMode( PDO::FETCH_CLASS, 'Product' );
$findProduct->execute();
$product = $findProduct->fetch( PDO::FETCH_CLASS );

Categories: PHP.

Tags: