Backend

Server side programming. LAMP Stack, NodeJS, CodeIgniter and more.

Trim long texts by words in PHP

Posted by Danny Herran on Oct 14, 2010 in Backend | No comments


We don’t like reading chopped sentences like “Star Wars Old Republic is be….”. It looks fugly. Usually, when trimming text you will go with substr(), but there is a much better way to do it, and believe me, you trimmed text will look so much better than before.

HTML Table Class on CodeIgniter 2.0

Posted by Danny Herran on Aug 31, 2010 in Backend | 3 comments

CodeIgniter 2.0 is coming out soon, so lets start studying the changes done to the HTML Table Class in the dev-release.

One of the most requested features was the ability to set tag attributes to individual cells. On CI 2.0 this is completely possible, however, since the documentation is not fully updated yet, I decided to make this short post to explain how it works.

Fixing UTF-8 encoding problems on MySQL queries with PHP

Posted by Danny Herran on Aug 24, 2010 in Backend | 4 comments

It happened to me a lot of times, I had my connection set to UTF-8, my database was UTF-8, even my tables and every single field, however, for some reason, the information I updated in phpMyAdmin wasn’t being displayed correctly on my website and the information edited on my website wasn’t being displayed correctly in phpMyAdmin.

You know all those weird characters, those that very often come up when you write accented letters or words in another language like Portuguese or Spanish? That’s what I am talking about.

Here is the solution. A single line that can save you hours of research.

Twitter feed on your website with cURL and PHP

Posted by Danny Herran on Aug 15, 2010 in Backend | 3 comments

Pulling code from here and there, I gathered enough information to create a small function that allow you to pull your Twitter feed and display it in your website. What you do is basically call the function and pass a couple parameters. The styling can be done with CSS.

Alternate row colors with PHP

Posted by Danny Herran on Aug 10, 2010 in Backend | 3 comments

I know there are several ways to accomplish this, however, of all the ones I’ve tried, this is the shortest so far:

$i = 0;
while(true)
{
	echo '<tr, td, div, row, whatever, here class="row'.$i.'">';
	$i = 1 - $i;
}

So, if you make two classes, row1 and row2, each with a different background color, the result will be alternated row colors in whatever you’re doing. Very useful for long lists.

Update: you might be interested in how to alternate row colors with pure CSS.

Create thumbnails on the fly with CodeIgniter

Posted by Danny Herran on Aug 9, 2010 in Backend | 9 comments

Before explaining the solution, you should know creating thumbnails on the fly is server inefficient, eats a lot of resources and they should be generated as soon as the images are uploaded. However, there are some occasions when we absolutely need to generate thumbs on the fly. In this article we will learn how to do this in CodeIgniter.

Add www subdomain automatically to your domain with htaccess

Posted by Danny Herran on Aug 9, 2010 in Backend | No comments

Sometimes we need to add the www to our domain automatically, maybe for aesthetics or ajax issues across subdomains. Apache .htaccess is our friend here. You just need to add this to your existing .htaccess file or just make a new one with the following contents:

RewriteCond %{HTTP_HOST} ^your-domain\.com [NC]
RewriteRule ^(.*)$ http://www\.your-domain.com/ [L,R=301]

Prevent image hotlinking with Apache and htaccess

Posted by Danny Herran on Aug 9, 2010 in Backend | No comments

Question of the million dollars. Those annoying image hot-linkers, eating our bandwidth, our food, our… nevermind. Anyway, if you want to give them hell and stop this behaviour, add the following to your .htaccess file (remember to edit your domain name):

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://your-server-ip/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://my-domain.com/.*$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.my-domain.com/.*$  [NC]
RewriteRule .*.(gif|GIF|jpg|JPG|png|PNG)$ - [F]

Bonus!: In the third line, you can optionally write your server IP for an extra layer of security. If you don’t know your server’s IP, just open an MS-DOS console (if you’re on Windows) or a Linux console and ping your domain. ie. “ping dannyherran.com”, would return 67.220.192.149 which is my server IP.

Prevent directory listing in Apache with htaccess

Posted by Danny Herran on Aug 9, 2010 in Backend | No comments

Piece of cake. If you need to prevent your files, images or anything else to be listed in those directories where an index file is not present, just add the following to your .htaccess file:

Options -Indexes

Piece of cake.