Archive for the ‘Tutorials’ Category

Oct

26

I ran into an interesting problem I’d never seen before this evening. I’m not sure if this is a problem isolated to the CodeIgniter framework because I’ve seen a few forum posts on Sencha/ExtJS reporting the same type of issue. But this was definitely happening for me on CodeIgniter.

The situation was that every time a user exported some data from one of my web applications to csv in Internet Explorer 8 and then opened that csv file, it would invalidate their session.  So, for example, they were running a report in the administrative console that provided results in an html table.  On all of the web applications that I write, I always give the user the ability to export the data to csv through a link above the table.  When a user clicked this link in IE and downloaded the csv file and then opened it, it essentially killed their session and forced them to log back in.  The key thing is that it would only do this if the user opened the file.  If they didn’t open it, their session was fine.

After doing some troubleshooting, I found that the issue was fixed by changing the Content-Type in the header.   In the method that exports the data to csv, it was set up as this:

header(“Cache-Control: must-revalidate, post-check=0, pre-check=0″ );
header(“Content-Type: application/vnd.ms-excel” );
header(“Content-Disposition:attachment; filename=”.$filename );
header(“Content-Transfer-Encoding: binary” );

print $out;

Where the “print $out” is actually printing out the details.

I was able to resolve this problem by changing the content-type header line above to:

header(“Content-Type: application/octet-stream”);

I didn’t see many forum posts about this, so hopefully this blog post will help someone out down the road who runs into this.

Apr

6

For a current project I’m working on, I’ve had to rank a list of teams based on their win-loss-tie record within their division. I searched the web but was unable to find anyone who had done this and posted an example so I figured I’d post my process. If you have corrections or improvements for this, please feel free to comment.

The code assumes that you have an array of team objects that each have a win/loss/tie property. As an example, I’ve got an array of five teams such that:

Array
(
  [0] => stdClass Object
  (
    [id] => 1
    [name] => Team 1
    [wins] => 5
    [losses] => 6
    [ties] => 3
  )

  [1] => stdClass Object
  (
    [id] => 2
    [name] => Team 2
    [wins] => 4
    [losses] => 8
    [ties] => 4
  )

  [2] => stdClass Object
  (
    [id] => 3
    [name] => Team 3
    [wins] => 2
    [losses] => 9
    [ties] => 6
  )

  [3] => stdClass Object
  (
    [id] => 4
    [name] => Team 4
    [wins] => 4
    [losses] => 9
    [ties] => 4
  )

  [4] => stdClass Object
  (
    [id] => 5
    [name] => Team 5
    [wins] => 4
    [losses] => 9
    [ties] => 5
  )
)

We’re going to use usort to sort the array based on each team’s win/loss/tie record.  First, we need to write our usort method:

function sort_win_loss($team1, $team2) {
  if($team1->wins > $team2->wins) {
    return -1;
  } else if($team1->wins < $team2->wins) {
    return 1;
  } else if($team1->wins == $team2->wins) {
    if($team1->losses>$team2->losses) {
      return 1;
    } else if($team1->losses<$team2->losses) {
      return -1;
    } else if($team1->losses==$team2->losses){
      if($team1->ties>$team2->ties) {
        return -1;
      } else if($team1->ties<$team2->ties) {
        return 1;
      } else {
        return 0;
      }
    }
  }
  return 0;
}

Lastly, we provide our array of teams to the usort and get our sorted list back:

usort($teams, “sort_win_loss”);
// After the usort, the teams will be ranked as
// 1) Team 1
// 2) Team 2
// 3) Team 5
// 4) Team 4
// 5) Team 3

Jan

30

We recently ran into a situation where one of our users was receiving a large amount of spam to their forwarded email account. Our setup is a dedicated server running Simple Control Panel at Godaddy. We handle email for a few clients and this one in particular was receiving 100′s of spam emails every hour.  Pretty much as soon as we set up this client, we started hitting our 1,000 SMTP Relay limit which prevented us from sending any further emails for any other client on the server.

There is a simple fix to this that I had a difficult time finding so I decided to create a post on this for anyone else who might run into this issue.

The first thing I tried was to turn on SpamAssassin through the Simple Control Panel (log in to your control panel, choose “Email” from the “Server Configuration” section, and choose “Filter incoming email using SpamAssassin”).  The result of this is that SpamAssassin started analyzing the emails coming in and marking them as Spam.  This didn’t fix the problem because the emails were not being dropped, but were instead being forwarded onto the receipient, just now with the “SPAM” designation in the message subject.

The only solution I discovered was to utilize Postfix’s header_checks.  This uses a regular expression to check the header of the email message and then apply a rule to the message accordingly.  Here’s what we did.

  1. edit the /etc/postfix/header_checks file.
  2. add the following line to the bottom of the file:

    /^X-Spam-Flag:.YES/ DISCARD spam

  3. edit the /etc/postfix/main.cf file an make sure the following line is in your file (it may just be commented out):

    header_checks = regexp:/etc/postfix/header_checks

  4. Now restart your postfix process by using the following command:

    postfix reload

What this does is look for the existence of “X-Spam-Flag” in the header of the message.  This flag is put there by SpamAssassin, so it is important that SpamAssassin is running.  Once it sees the “X-Spam-Flag”, it will discard the message.  This will effectively prevent your server from forwarding any spam emails onto your user’s email accounts.  So far this has worked out well for us.

Oct

6

I recently got a request from a client of mine to setup two different wordpress blogs for her company and then display the posts from both blogs in a single list on the home page of her web site. I wasn’t able to find much documentation of people doing this type of thing through various searches, so I figured I’d post an entry on it when I finished. As a disclaimer, I’m sure that this can be done more efficiently or in less code. If you have a better way to do it, please leave me a comment. I’m always open to learning new techniques.

Read the rest of this entry »

Sep

29

I’ve run into situations where I’ve installed content management systems for customers who like to add their own content and/or copy content from documents they’ve created. Often, this results in them copying non-ASCII characters such as smart quotes, elipsis, or em dashses. I’m not sure why (maybe someone can educate me by posting a comment below) that PHP can’t handle these characters, but I’ve come up with a way to replace these characters with characters or character sequences that PHP understands. The function is below.

function cleanString($string) {
  $find[] = '“';  // left side double smart quote
  $find[] = '”';  // right side double smart quote
  $find[] = "‘";  // left side single smart quote
  $find[] = "’";  // right side single smart quote
  $find[] = '…';  // elipsis
  $find[] = '—';  // em dash
  $find[] = '–';

  $replace[] = '"';
  $replace[] = '"';
  $replace[] = "'";
  $replace[] = "'";
  $replace[] = '...';
  $replace[] = '-';
  $replace[] = '-';

  return str_replace($find, $replace, $string);
}

The function essentially is a very simple string replacement that attempts to match an invalid character with a valid character and output the change.  This will prevent the weird diamonds or boxes that you may be seeing in text output using “echo” in php.

Sep

16

See The Demo

I recently completed a website design project for a local austin company that involved creating a simple image slideshow on the login page of their application. These types of slideshows have traditionally been done using an animation toolkit or framework such as Adobe’s Flash product. Starting around three years ago, I was introduced to script.aculo.us which provided the ability to use effects with DOM elements to provide cool animations.

Just within the last year, I’ve started working more with jQuery and have been introduced to the slew of extremely cool plugins.  The newest one I’ve come across is jQuery Cycle.  Using this plugin and 9 lines of javascript code, I was able to create an extremely simple but cool image slide-show for the website project for my client.

The first step in building the slideshow is to create the html element that will hold the images for your slideshow:

<div id=”slideshow”>
  <img src=”slides/slide-1.jpg” border=”0″/>
  <img src=”slides/slide-2.jpg” style=”display:none”/>
  <img src=”slides/slide-3.jpg” style=”display:none”/>
</div>

The above is a simple list of images within the div element that’s identified by the “slideshow” id.  This will enable jQuery to pick up the “slideshow” element and then iterate over the <img> elements within it when building the slideshow.

The second step is to write the actual javascript that will use the plugin to build and run the slideshow:

$(document).ready(function(){
  $(function(){
    $(“#slideshow”).cycle({
      fx: ‘fade’,
      speed:2500,
      timeout: 1000
    });
  });
});

A little explanation on what is happening here: The $(“#slideshow”) is grabbing the slideshow div element and feeding the images into jQuery Cycle Plugin’s initialization.  The plugin is applying the fade effect (see more information on the effects you can use here) at a speed of 2500 milliseconds and a timeout of 1000 milliseconds.  Speed controls how long the transition takes to complete and timeout controls the time between slide transitions.  You can find more information on the options for the plugin here.

All-in-all, this has turned out to be a ridiculously easy way to code up a simple slideshow.  You can enhance your slideshow in all sorts of ways such as loading your images through ajax after the page loads, randomizing ordering, and providing new transitions.  All without having to worry about motion tweens in Flash!

See the Demo