Tag Archives: php

Using a Zend view helper inside a partial

When rendering a page using one or more partials, I often need to call a helper to do some extra stuff for me.

One caveat I found was, that the view variables was not available in my helper.

After some research it seems that the partial acts as the new view.
So in order to access the views variables, you need to pass them to your partial.

<?php echo $this->partial('partial-path/partial', array('variable1', $this->variable1, ...)); ?>

Problems connecting to unix:///var/mysql/mysql.sock

In a previous post I talked about MySQL 5.5 and Mac OSX.

In this post I’ll go through fixing the problems with PHP and connecting to your local mysql install, using “localhost”.

The problems began a while back, with lots of errors in my apache error log saying:

[error] [client ::1] PHP Warning:  mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in ...

In the rush I was in, I quickly changed my mysql connection to use: 127.0.0.1, which is the IP of your localhost. So basically the same thing.

Today I’m doing some freelance work for a customer, who has some problems with his server, after the PHP version was upgraded. I decided to fetch all his php files to my local Mac and then run through his webshop, fixing any errors I might see in the log etc.
When starting the my apache and running the website, I quickly found the MySQL connect error again.

Since this project should be a “search and fix” mission, I didn’t have time to change the mysql_connect(xxx) statements in the code (yes yes, not my code, so I didn’t create the mess…), so instead, I wanted to fix my local PHP->MySQL connection.

The fix was relatively easy, and only contains 1 to 2 steps:

Step 1 (if needed)

If you haven’t activated php.ini on your local install, open a Terminal and write the following command:

sudo cp /etc/php.ini.default /etc/php.ini

This copies the default php settings to the php.ini file, which the apache server uses.

Then restart your apache server. (Using System Preferences->Sharing->Web sharing)

Your PHP is now using the php.ini file.

Step 2

Open /etc/php.ini file using your favorite text editor.

Goto line 1216 (or search for “mysql.default_socket = ” without the quotes) and change /var/mysql/mysql.sock to /tmp/mysql.sock

Restart your apache server and you should now be able to connect to localhost again.

Still have problems?

If you still have problems, then try the following:

Open Terminal and write:

mysqladmin version

It should print something like this:

mysqladmin  Ver 8.42 Distrib 5.1.53, for apple-darwin10.3.0 on i386
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version		5.1.53
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/tmp/mysql.sock
Uptime:			2 hours 52 min 6 sec

Threads: 3  Questions: 58  Slow queries: 0  Opens: 16  Flush tables: 1  Open tables: 9  Queries per second avg: 0.5

The path in the UNIX socket is the “localhost” connection point. So go back to Step 2 and use that path instead.

Handle different environments with PHP

Being both a Rails and PHP developer, I’m often lacking a few things when I’m switching from Rails to PHP.

One of the things I miss the most, is the different environments that Rails has, which makes testing, developing, staging and production environments easy.
However, I found a way to do this in PHP as well, without using frameworks like Zend, CakePHP etc.

Getting started

There are basically two things you need to do, in order to get this setup to work properly.

Configure your server (apache)

First we need to configure the apache server (If you are using nginx, look at their documentation for help).

The magic lies in using the SetEnv function in the apache server.
This functions makes a variable available in the $_SERVER object in PHP, which is what we are using to differentiate between the environments.

Virtual hosts

If you are using virtual hosts, then simply add it with in the section.

An example configuration with a “test” environment could be:

<VirtualHost *:80>
  ServerName my_site.test.dk
  DocumentRoot /var/www/my_site.test.dk

  SetEnv APPLICATION_ENV test

  <Directory /var/www/my_site.test.dk>
    Options Indexes FollowSymLinks -MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>

  ErrorLog /var/log/apache2/my_site.test.dk.error.log

  # Possible values include: debug, info, notice, warn, error, crit,
  # alert, emerg.
  LogLevel warn

  CustomLog /var/log/apache2/access.log combined
</VirtualHost>

Using the apache.conf / httpd.conf

On my mac the file is called httpd.conf, but on the ubuntu linux servers I’m managing it’s called apache.conf.

Anyways, just head to the bottom of the file located here:
Linux: /etc/apache/apache.conf
Mac: /etc/apache/httpd.conf

And add the following line:

SetEnv APPLICATION_ENV "development"

Exchange “development” with the environment you are configuring (In my setup, production = no value)

Configure your application

Now we need to use the variable passed to the $_SERVER object, in order to see what environment we are “in”, and configure the application accordingly.

In most projects I have an environment.inc.php file, which has the following structure:

<?php

// initializing the configuration array (mostly to avoid null warnings)
$envConfiguration = array();

// the environment configuration for the development environment (local machine)
if(isset($_SERVER['APPLICATION_ENV']) && $_SERVER['APPLICATION_ENV'] == 'development') {
  $envConfiguration = array(
    'db_password' => '12345',
    'db_user' => 'root',
    'db_host' => '127.0.0.1',
    'db_name' => 'my_dev_db'
  );
}
// the environment configuration for the unit testing environment (local machine)
if(isset($_SERVER['APPLICATION_ENV']) && $_SERVER['APPLICATION_ENV'] == 'unittest') {
  $envConfiguration = array(
    'db_password' => '12345',
    'db_user' => 'root',
    'db_host' => '127.0.0.1',
    'db_name' => 'my_unittest_db'
  );
}
// add more environments here... E.g. staging, test etc

// Not having the APPLICATION_ENV variable set, forces the application to
// use PRODUCTION settings!
// The reason for this is, that I don't always have control of the production
// servers, while I have control over the staging and test servers. 
// (You can of course have a production value set
else {
  // production environment settings here.
  $envConfiguration = array(
    'db_password' => 'some_strong_password',
    'db_user' => 'some_production_user',
    'db_host' => '127.0.0.1',
    'db_name' => 'production_database'
  );
}
?>

Pretty simple ye?

What we are doing is simply checking if the APPLICATION_ENV variable is set in the $_SERVER object, and if it is, we test what it is.

The reason I’m checking if the APPLICATION_ENV isset, is because it gives a lot of warnings if the variable is not set (which would be in production for my setup).

What about unit testing? (phpunit)

Well, I have an answer there as well.

Since the $_SERVER variable is not available in unit tests, we simply create it ourselves and set the APPLICATION_ENV to “unittest”.

Here is a sample unittest include file, which should be included at the very top of your unittest.
Let’s call this file unitTestConfiguration.inc.php and put it in a folder called tests

<?php

// includes the phpunit framework
require_once 'PHPUnit/Framework.php';

// constructs the SERVER variable to set the environment to unittest.
$_SERVER = array(
  'APPLICATION_ENV' => 'unittest',
  'SERVER_NAME' => 'localhost',
  'REQUEST_URI' => ''
);
// SERVER_NAME and REQUEST_URI is not needed, but nice to have

// includes our environment file (remember to add a unittest section there!
include('config/environment.inc.php');

// includes the database file, which reads the $envConfiguration variable
// (which is set in the environment.inc.php file) and connects to the database
include('config/db.inc.php');

// sets the default timezone (Because strftime will throw a warning in PHP5+)
date_default_timezone_set("Europe/Copenhagen");

?>

When creating your unit test, simply do the following:

<?php

// includes the unit test configuration (including the PHPUnit framework)
include('tests/unitTestConfiguration.inc.php');

class EnvironmentTest extends PHPUnit_Framework_TestCase {
  /**
   * A small test to see if our environment is actually set.
   * (You don't need this test in your test files, this is 
   * just for the scope of this post!)
   */
  function testEnvironment() {
    $this->assertTrue(isset($_SERVER['APPLICATION_ENV']));
    $this->assertTrue($_SERVER['APPLICATION_ENV'] == 'unittest');
  }
}
?>

To run the unit test, simply open a terminal / command / cmd (or what ever you are using), and go to the project folder.
There you should run the following command:

phpunit tests/environmentTest.php

On my machine that gives the following output:

$ phpunit tests/environmentTest.php 
PHPUnit 3.4.14 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 5.75Mb

OK (1 test, 2 assertions

Files for this post: 2011-01-08_php_testing_article

Read more, continue reading, and how to easily fix it using WP

Yesterday I updated my grayish blog theme to a new blue and white theme, with more whitespace and less constraints.
While the new theme is isn’t perfect, I shined my website up a bit and it seems nice.

However, scrolling through my twitter list today, I read a blog post by Karan over at mardahl.dk, where she discusses the usage of “Read more” and “Click here” links on webpages. (Direct link to the blog post here).

This made me investigate what my own website was doing, and while the wording was a bit different, I had the same usability problem she describes in the blog post (you should really read it). People tend to overlook the wording, since lots and lots of advertisements use it. So when I use excerpts on my blog front page, and use the wording “Continue reading”, I’m actually scaring the user off, because my message will be misunderstood.

How I fixed it

When I made my new theme, I simply copied the twentyten theme supplied with wordpress 3, gave it a new name, fixed the style.css file to have my details in it and started to change the stylesheet to make it fit what I wanted.

Karan suggests using a different wording on the links, so it has the “Read more” wording, but includes the title of what it actually is, you are reading about.

She suggests the following:

Read more about Education
Read more about our Picture Archive
Read more about meeting the museum around town

And that’s the approach I’m going to use as well. However, I will be using the wordpress wording of “Continue reading”, since it was there to begin with and because it sounds more like a continuation instead of a whole new beginning to me.

Another thing to consider is, that my blog post’s link will be auto generated, so it’s easier to do something a bit more generic.

This means, that I will be using the following convention:

Continue reading “my blog post title”

To do this using the twentyten theme, open up the functions.php file that you copied with the theme.

If this is the first time you are altering the file, simply go to line 240, else search for the function named: “twentyten_continue_reading_link()”.

The function is defined as follows:

/**
 * Returns a "Continue Reading" link for excerpts
 *
 * @since Twenty Ten 1.0
 * @return string "Continue Reading" link
 */
function twentyten_continue_reading_link() {
  return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) . '</a>';
}

After browsing a bit of documentation in the “original” functions.php that wordpress core provides, it seems using globals is that way to go, if you want access to the current post object.

This will make the code look like this:

function twentyten_continue_reading_link() {
  global $post;
  
  return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) . '</a>';
}

We now have access to the post object and can get the post title from the title from it.
This is done using the variable post_title from the $post object.

The finished code looks like this:

function twentyten_continue_reading_link() {
  global $post;
  
  return ' <div class="read_more"> '. __('Continue reading', 'twentyten') .' <a href="'. get_permalink() . '">"'. $post->post_title .'" &raquo;</a> </div>';
}

I added a div with a class of “read_more” around the link, so it would make it easier to style.

After the div there is the wording Continue reading and then the link to the blog post, using the title of the post.

What’s your approach ?

And do you agree with Karan that this will make it more usable or is it the same or even worse ?

All I can say is, that I have already changed it on my blog.

Switching coffee and PHP testing

Whoa, long time since my last post.

Anyways, what has happened recently?

I’ve switched jobs and are now working at a small IR firm in Denmark, which is located on Amager.
We do webcasts / Quarterly reports for firms in Denmark.

Saying “We do” is a bit off though.
I don’t, that is. I help out if we are missing a technician, else i’m creating webapps to help the business complete daily tasks faster.

And how is that going?
Well, I’ve completed a few systems already, and within one month the first system was deployed to the wild. It was fun to create a whole new system, especially because I am on my own.
Roughly speaking, i’m the only developer there, so I don’t get a lot of sparing on the code i’m writing. Only the designs and the general “look and feel” of an app.
It’s fun, it’s different and I like the pace.

An other thing i’ve been working on, is moving the old and new sites to new virtualized servers.
I’ve been managing a few servers for the last 6 years, but not anything on this scale. But it’s fun. It takes a little time from the programming though, but after it’s configured, it mostly runs smoothly.

I developed an SSO system and deployed it to the wild a few months ago. So far i’ve only had minor problems with it, and all things are running great.

And yes, I code in PHP. It was by choice actually. On my last jobs I’ve coded Bash, Perl, C, C++, then Java and then Ruby on Rails and on the new job I was given a free choice.

So I chose PHP.

“Why?” you might ask, when there’s soo much hype about Ruby on Rails these past few years. Without starting a flame wall in the comments, let’s just say, that while Ruby is a beautiful language, I just couldn’t get accustomed to the Rails frame work. So many rules, so many conventions. So little time. (The testing was nice though.. but meh)
I might return to Rails someday, but for now, it’s PHP controlling the battle.

The webapps i’ve created at my new job are all created using my small github project called php-mvc-base, which basically is just a structure I use to get started on a PHP project. It gives me “nice urls”, but other than that, the rules are pretty basic.

The structure outlines as follows:

/app
/app/controllers
/app/models
/app/views
/public/images
/public/javascripts
/public/stylesheets

And all controllers (ofc) go in the app/controllers folder.
So let’s say, that we want a new controller at the URL http://localhost/my_projects/ there is basically two things that can be done:
* Create a folder in the app/controllers folder, called my_projects. Place an index.php file there, do the controller code.
* Create a my_projects.php file in the app/controllers folder. Do the controller code.
Both options give the same path.

Simple and clean (well, at least in my world. And I use the folder approach btw, if you wanted to know).

(You can find the project at http://github.com/jimmiw/php-mvc-base along with a simple example.)

I’ve also released a few javascripts to CodeCanyon and I’ve had a few purchases already. It’s nice knowing people can use your stuff. I mostly coded them because I had a problem they could solve, but releasing them there, made the code so much better. This was mostly due to the javascript approval team on CodeCanyon (Thanks Jeremy McPeak for the patience and help).

You can see my profile on CodeCanyon here: http://codecanyon.net/user/jimmiw

I’ve also taken over a small project with a designer friend of mine called Janus C.
The project is familielivet and is a danish page the centers around the family. It’s free to use and hopefully easy to understand and use for all ages.
I’ve not actually released any code to the system yet, but we a doing some design changes and a total rewrite of the codebase, and it will hopefully be released soon.

After switching from Ruby to PHP I missed the easy testing that Ruby offered. Thank god for phpunit btw, this lovely tool simply makes testing fun again. Be sure to check it out when you are writing tests for your webapps (As you should be!).

On an interesting side note, Rails offers the ease of different environments for you to use.
E.g. Test, Staging and Production.
I actually found a simple way of doing this with PHP as well, but it requires that you have access to the apache configuration files (which you at least have on your development machine).

Roughly speaking, all you need to do, is to add a variable to your apache config, and then use the $_SERVER variable in PHP to test what environment your are currently working on.
Nice and simple.
A small example is this (taken from my development machine):
Just add the following piece of code to the bottom of your httpd.conf file
SetEnv APPLICATION_ENV "development"

And in PHP you can do the following when initializing the database connection:

<?php
  // development machine
  if($_SERVER['APPLICATION_ENV'] == "development") {
    mysql_connect(HOST, USER, PASSWORD);
    mysql_select_db(DATABASENAME);
  }
  // add as many environments as you want
  // the ELSE part is used for production. On shared hosts you cannot edit the
  // httpd.conf file, so if nothing is set, assume it's Production ;)
  else {
    mysql_connect(PRODUCTION_HOST, PRODUCTION_USER, PRODUCTION_PASSWORD);
    mysql_select_db(PRODUCTION_DATABASENAME);
  }
?>

I have a “unit” environment as well, which I use when testing database models using phpunit tests. This makes it easier to wipe database tables when starting tests.

And I switched from black coffee to Lattes.
No idea why, but after drinking black coffee for about 10years, they suddenly seem a bit boring…

WPML, what is the current language?

While building a wordpress theme that has multilingual support, I needed to know what the current language was for fixing some static parts of my template.

After searching a bit on the WPML site, I found a code example where you are shown howto create your own language selector menu:
http://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/

I then wrote this function to get the current language of a site:

/** 
 * Function for getting the active language of the WPML plugin
 * @return the currently active language, defaulting to 'en'
 */
function getActiveLanguage() {
  // fetches the list of languages
  $languages = icl_get_languages('skip_missing=N&orderby=KEY&order=DIR');

  $activeLanguage = 'en';

  // runs through the languages of the system, finding the active language
  foreach($languages as $language) {
    // tests if the language is the active one
    if($language['active'] == 1) {
      $activeLanguage = $language['language_code'];
    }
  }

  return $activeLanguage;
}
?>

Just add this code to the theme’s functions.php file and you are ready to find the active language of your site!

(php) php-mvc-base project released

Whenever I create a new PHP project I use a certain base structure.
This structure allows me to have pretty URLs and have a nice way of configuring the database connections. Furthermore this structure also allows me to use a form of plugins in my system.

I have even included an example when you start your server, once the base structure is installed. Go check it out on http://github.com/jimmiw/php-mvc-base

(php) flash-message plugin

For a long time I’ve wanted a way to send messages from my controller to my view, when posting data. The message could be “Your update went well” or some error.

In the old days, I have done it like this:

controller, users/update.php:

<?php
$result = mysql_query("update users set email = '".$_POST['email']."' where id = ".$_POST['id']);

if($result) {
  header("Location: /users/index.php?message='Some long message, that might bug out'");
}
else {
  header("Location: /users/edit.php?message='ERROR because....'");
}

?>

view, users/index.php and users/edit.php:

<?php
echo $_GET['message'];
?>

However, these days are over now. With my little plugin, you can easily pass messages from the controller to the view.
All you need to do, is to include the script on your pages, then call: flash(label, message) from your controller. From your view, you can call echo getFlash(label) and that is it!

Using the same example from before, we get the following:

controller, users/update.php:

<?php
$result = mysql_query("update users set email = '".$_POST['email']."'  where id = ".$_POST['id']);

if($result) {
  // set the message
  flash('success', 'Some long message, that will NOT bug out');
  // do the redirect
  header("Location: /users/index.php");
}
else {
  // set the message
  flash('error', 'You had an error in your data.');
  // do the redirect
  header("Location: /users/edit.php");
}
?>

view, users/index.php:

<?php

echo getFlash('success');

?>

view, users/edit.php:

<?php

echo getFlash('error');

?>

Head on over to the github reposition at get it: http://github.com/jimmiw/php-flash-message

I hope you enjoy it