Tag Archives: php

Disable browser cache easily with CodeIgniter

I have a project where users are validated by session data. When the user logs out I destroy the session and redirect them to the log-in page, but this does not prevent them from clicking the browser’s “back” button, in which case they still see the data.

My first solution was to set headers with PHP’s header() function. Then I decided to use CodeIgniter’s setheader() function from the Output library (no real difference, just using CI methods when possible). Finally, I decided the best way to do this would be to extend the Output library itself. This way I am not repeating multiple function calls in each controller. Here is end-result… Continue reading

CodeIgniter Form Validation Callback Functions with Multiple Arguments

If you’re reading this you most likely already know that you can only send one argument to callback functions using CodeIgniter’s Form Validation Class. If you are not yet familiar with this feature/limitation, here is an example of how the callback works.

Here is our controller with a normal callback (_foo) and our multiple-argument callback function (_valid_login)… Continue reading

Future proof PHP class constructer compatible with php4 and higher

Since there was no real viable reference to either confirm or oppose my methods, I figured this may help someone else that is trying to make sure they don’t have to change their code based on the installed PHP version.

As most of us already know, initializing a new class object in PHP4 required the constructor to have the same name as the class.

class Myclass {
 function Myclass()
 {
  echo 'Myclass initialized';
 }
}

Then __construct() was introduced in PHP5.

class Myclass {
 function __construct()
 {
  echo 'Myclass initialized';
 }
}

Now you may be thinking “I’ll just use this from now on”. I however have to write for multiple versions of PHP4/5 and must keep backwards compatibility in mind (at least for now). Here is what I use to make sure it works on all versions of PHP that currently support object-oriented programming or OOP. Anything earlier than PHP4 and I simply tell the client to find a new host, lol. Continue reading

Validate email domain using PHP’s checkdnsrr with CodeIgniter

I recently noticed a bunch of garbage registrations for an existing client’s newsletter function written quite some time ago. After making a copy of the subscribers table and “cleaning” the data (proper-cased names, lowercased emails, etc), I started looking at ways to limit these automated registrations.

The client would not be happy with registration confirmation emails, so I decided to make a callback function for use with CodeIgniter’s Form Validation library. Since it was only needed in one location, this method made more sense to me than extending the main library.

Here is the function…

Continue reading

CodeIgniter, MySQL and User-Defined Variables

The lesson for today is… how to sort records sequentially on a numeric column and provide the ability to move records up and down in the existing sort order.

There are 2 functions I use for manual record sorting to change the display order in admin/user areas. The following example is based on a “pages” table and the commands are located in a model. For this example, the table has the following fields: page_id (int), page_sort (smallint), page_title (varchar), and page_content(text).

Continue reading

Table/record sorting made easier for CodeIgniter

In a project I am currently working on, I needed a way to efficiently allow sorting of the records in an admin area while preserving the functionality of a search function (that passes the query in the url) and the pagination library. Initially it started off as a section of each method where sorting was allowed (record lists of things like countries, states, etc). My sorting routines finally ended up being put into their own helper.

Continue reading

Codeigniter Image_lib convert() v1.3 JPG to GIF to PNG

I recently performed some code-fixing for a library extension I found over in CodeIgniter’s forum. The mod provided a convert() method for the Image_lib library. It lets you to convert (duh) the original image from/to JPG (default), GIF or PNG and optionally delete the original image.

When I first added this extension into my CI distribution, everything still worked without throwing errors so I started calling the new function. Then…

Continue reading

Convert extended ASCII in a string using PHP strtr instead of Normalizer or iconv

I am working on a CodeIgniter project that has information stored in the database with extended ascii characters (128-255) and UTF-8 encoding. As most of you know, CodeIgniter is fairly strict on what is allowed in a url (they only allow what SHOULD be permitted per RFC 1738 ;)). Furthermore sunce you can create basically any character by allowing a percent sign (%) in the URL, alowing that was not a viable option for me.

For instance you cannot use “ñ” (n with a tilde) in the URL parameters. Replacing it with “n” for the database query will however return a valid match. Problem solved! I can just use Normalizer or iconv, right?

Continue reading

Codeigniter Core Mod for $_SERVER[‘DOCUMENT_ROOT’] on GoDaddy subdomains with $_SERVER[‘SUBDOMAIN_DOCUMENT_ROOT’]

I was tinkering around with a CodeIgniter project earlier today and at some point, I needed to use PHP’s file_exists() function. I tend to use $_SERVER[‘DOCUMENT_ROOT’] for my starting location for finding files located in the web-viewable areas (makes sense huh?). This makes it easy to get the file url by simply replacing the document_root from the path of the filename which results in a nice absolute path for linking. In this particular instance, I was checking for an image to show in place of a heading tag. Granted the code was simple and straight forward, but it could not get PHP to locate the file and I knew it was there.

Continue reading

PHP/MySQL Nested <select> using <optgroup> with LEFT JOIN on same table in a single query.

I looked around on the web for a while trying to find an example of what I was trying to do. Some were close, but none performed the JOIN portion of the query and on the same table from the SELECT portion (self referencing). So, here is the code I used to created a sub-category only dropdown using the parent categories as the optgroup labels.

Continue reading

How to fix dtheatre’s FormMail.php v5.0 form variables

Well, if you’re here you most likey are not getting any data from your FormMail script in the email you received. The problem is that dthetre’s script expects register_globals to be turned on. Since PHP4, most installations have register_globals turned off. This means your script will not work unless the feature is enabled. Most hosts are not likely to enable this feature (we’re one of them).

So here’s a quick workaround for the script. Add this code right after the header section of the script (before “// for ultimate security, use this instead of using the form”).

CODE UPDATED – SEE BELOW

Continue reading