Greg Aker

Entries Tagged with: php

var_dump($this) in EE/CI with no recursion

Filed in: PHP, ExpressionEngine, CodeIgniter

August 18, 2011

Here's a quick little tip for all you EE addon developers out there. I've seen some people moaning on Twitter about how a var_dump() can be hard on the browser due to the recursion in the CodeIgniter singleton. So check how I like to roll in my CI libraries/EE addons.

<?php

class Foo {

    protected $_foo;

    public function __construct()
    {
        $this->_foo = $this->config->item('something');
    }
}

Look maw, no $this->CI

Did you know you you can do this in a CodeIgniter library or an ExpressionEngine mcp/mod/extension/plugin/accessory file? It's pretty easy to accomplish, and ...

Read More

Showing PHP Memory Usage through your Template

Filed in: Performance, PHP, ExpressionEngine

June 18, 2011

Ever been deep in troubleshooting performance issues in your ExpressionEngine site and wondered what kind of memory is a certain plugin/weblog tag, etc is taking up? Unless you're holding out from blogging about it, there's absolutely no way I'm aware of that you're getting that info from shell on the server. So, here's a quick and dirty hack to EE's template parser that will show you this info.

Pop open system/expressionengine/libraries/Template.php and look for:

<?php

function log_item($str)

Now, a quick addition in there and you're good to ...

Read More

Session Cache in ExpressionEngine

Filed in: PHP, ExpressionEngine, tutorial

June 14, 2011

I think the session cache may quite possibly be one of the more under used tools in the third-party ExpressionEngine developers toolbox. The speed/performance increases from strategically caching items in this can be substantial. As with anything you do while developing your addon, benchmarking needs to be done every step of the way to ensure you aren't overdoing it and actually causing issues. I want to illustrate how adding something to the session cache can give a nice performance boost.

What is the session cache?

First, see the docs.

If you're a developer and haven't had ...

Read More

CURLOPT_FOLLOWLOCATION errors and what you can do

Filed in: PHP

April 23, 2011

I see this come up in waves every so often on Twitter or the ExpressionEngine forums.

Warning: curl_setopt() [function.curl-setopt]: 
CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or 
an open_basedir is set in /var/www/test.php on line 10

So what in the world does this mean, and what can you do about it?

First off, if we read the error, it's crystal clear. If you're using code that calls CURLOPT_FOLLOWLOCATION when safe_mode is enabled or open_basedir is in effect, you get this error. Bam!

Safe Mode, is generally thought of as an abomination, is off ...

Read More

What is xss_clean in CodeIgniter, and why should I use it?

and just as importantly, when should I not use it

Filed in: PHP, CodeIgniter, Security

March 30, 2011

If you're new to CodeIgniter, you've probably seen xss_clean() mentioned in the docs. When I was first starting with PHP and CodeIgniter, every time I saw xss_clean() mentioned, I'd think to myself "what the hell is that?!" So I hope I can show you what it is, why it's important to use and when you shouldn't use it.

What's XSS?

If you haven't heard the term XSS before, read over at Wikipedia about Cross Site Scripting.

A really quick example of an XSS attack would be to build a script like:

<?php

echo ...

Read More

Extending CodeIgniter's Controller

Filed in: PHP, CodeIgniter

March 18, 2011

I think it's safe to assume that I'm not in the minority when I say that I learned to program with CodeIgniter. I started in mid-2007 learning HTML/CSS while learning ExpressionEngine. Within a year or so, CodeIgniter seemed to be the logic step on what to learn, so I dove in head first. Lisa Wess gives me grief to this day for my forum post asking for someone to explain to me how the exp:weblog:entries tag worked. Her answer was correctly, "go look at the code." :)

When I was new to programming and CodeIgniter, one ...

Read More

Let's build an ExpressionEngine Module!

Control Panel Page (mcp file) and model. Part One

Filed in: PHP, ExpressionEngine, tutorial

March 16, 2011

  1. Project Setup and Module Installation
  2. Control Panel Page (mcp file) and model. Part One
  3. Control Panel Page (mcp file) and model. Part Two
  4. Front End Template Tag

I decided to break the control panel portion into two parts, so this will now be a four part series. First, we're going to build the form to submit a short link. This will include a model, view and controller (mcp file).

So, let's start by setting up the base for the module control panel file (mcp.shortee.php). Open that up in your editor, and let's get to work ...

Read More

Let's build an ExpressionEngine Module!

Intro & Install/Update File

Filed in: PHP, ExpressionEngine, tutorial

March 13, 2011

  1. Project Setup and Module Installation
  2. Control Panel Page (mcp file) and model. Part One
  3. Control Panel Page (mcp file) and model. Part Two
  4. Front End Template Tag

I think I'm a bit different than most in how I view the ease in which to build the different types of ExpressionEngine addons, but I definitely think that in most cases modules are not a heck of a lot more difficult than Plugins. Personally, I find Extensions the most difficult as you have to develop with a keen eye to ensure your extension is playing nice with the rest of the ...

Read More

CodeIgniter Reactor's Caching Drivers

Filed in: PHP, CodeIgniter

February 12, 2011

Before Reactor dropped, I snuck in some caching drivers (Docs Link) that Pascal had been tossing back and forth for several months. I've seen a few people talk about them, but I think they are still generally unknown so I want to give a brief intro on how to use them.

At this time, there are drivers available for APC, Memcached, file-based caching, and a dummy cache. If you haven't already, head over to BitBucket and get cloning or forking.

Examples

Caching API Calls

We can cache API calls pretty easily within controllers. Let's say we want ...

Read More

Keeping it so simple it's stupid

Filed in: PHP

December 15, 2010

I like simple code. Dead simple. I want to scan and know what the heck is going on. I am unimpressed with what some perceive as wizardry, with the exception of making hard tasks look like a beginner can understand them. :) Hard for the sake of hard belongs in the Perl world. An edict of Python is the simplest way is the best way, and I wish more PHP developers would subscribe to that way of thinking.

Take a very simple operation. An if else statement in php. We could have a model function in CodeIgniter that looks like this ...

Read More

CodeIgniter Authentication - Part 3

Content Protection and Logging Out!

Filed in: PHP, CodeIgniter

December 9, 2010

The last article was a marathon, so let's keep it light for a Thursday afternoon. We'll cover restricting controllers to logged in members. We left off with redirecting a user to the logged_in controller, so let's start there. Drop the following into application/controllers/logged_in.php.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Logged_in extends CI_Controller {

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();

        $this->output->enable_profiler(TRUE);
        $this->load->library('simple_auth');
        $this->load->helper('url');

        if ( ! $this->simple_auth->is_logged_in())
        {
            show_404();
        }
    }

    // --------------------------------------------------------------------------

    /**
     * Index
     */
    public function index()
    {
        $this->load->view('logged_in');
    }
}

Okay awesome. Let's go over ...

Read More

Creating a Simple Auth Class for CodeIgniter - Part Two

Filed in: PHP, CodeIgniter

December 7, 2010

This post is a continuation of Creating a Simple Auth Class for CodeIgniter - Part One, and here's we will focus on member registration and logging in. The code for these tutorials can be found in a repository over at BitBucket.

In all of these controller examples, I have the CodeIgniter Output Profiler turned on. Obviously, on a production website, you'll want to make sure that and display errors are off. :)

First things first. In order to log in, we need to be a member of the website. Let's start with our Registration Controller. Drop register.php into ...

Read More

Creating a Simple Auth Class for CodeIgniter - Part One

Filed in: PHP, CodeIgniter

December 6, 2010

Authentication is pretty essential to many web apps, and was probably the hardest thing for me to figure out when I first started a few years ago. So, I'd like to do a series of posts, where we develop a simple auth class for CodeIgniter.

In our SimpleAuth Library, we're going to require log in, log out, testing to see if a user is authenticated, user registration, changing passwords and a forgot password feature. On a side note, at EllisLab, we write user documentation before we write a line of code. The first time I did this, I ...

Read More

Developing on the CodeIgniter Trunk

Filed in: PHP, Mercurial, CodeIgniter

November 11, 2010

Earlier this evening, Kenny Meyers posed a question on Twitter about how to develop with CodeIgniter while working off the trunk. With Mercurial, it's easy.

We'll use a mercurial sub-repository so CodeIgniter is running off the EllisLab CodeIgniter repository. In fact, I created a quick repository over at BitBucket to help me easily start a new project.

Here is my basic project directory structure.

> tree 
.
├── fabfile.py
└── src
    ├── application
    │   ├── config
    │   ├── controllers
    │   ├── core
    │   ├── errors
    │   ├── helpers
    │   ├── hooks
    │   ├── index.html
    │   ├── language
    │   ├── libraries
    │   ├── models
    │   ├── third_party
    │   └── views
    ├── codeigniter
    │   ├── application
    │   ├── index.php
    │   ├── license.txt
    │   ├── system
    │   └── user_guide
    └── public
        ├── example.htaccess
        ├── index.php
        └── static
            ├── css
            ├── images ...

Read More

What's Your .htaccess?

Filed in: Performance, PHP

November 1, 2010

I don't use a whole lot of Apache anymore for my personal stuff, but when I do, here's my basic .htaccess.

Some people might say:

"Woah Greg, what the heck, you aren't gzipping your static content you hosehead!"

Well, I have an answer for you. See, gzipping can be a CPU-intensive operation on the web servers. If you're on a Cheap VPS, a la Linode, Rackspace Cloud, Slicehost, etc you're trying to squeeze every bit of CPU power out of it. Or on the flip side, an awesome host like EngineHosting (yes, I do work ...

Read More

Are you running APC?

Filed in: sysadmin, Performance, PHP

October 13, 2010

Given the plethora of tutorials out there on how to run your own VPS, and the number of hosts offering DIY VPSs for super cheap, many developers find themselves becoming sys-admins. So securely configuring your stack, and tuning/configuring to offer optimal performance out of low resources is key. For these cases, I totally prefer Nginx, and I'm waffling on the proper way to configure PHP, whether it is proxying to Apache, or running php-cgi. The only thing no one can argue with is APC.

If you know, and talk with sysadmins, you'll quickly realize that most good ...

Read More