Headers to Send to Force a Download in PHP

It can be difficult sometimes to force a web browser to popup a dialog to let someone download a file. A lot of times with things like a pdf or Microsoft Office files (Word, Excel, etc), browsers like Internet Explorer will just open them with a sub program in the browser. This can be annoying for both the developer and user. However to be honest with you, I am not sure if this is as big of an issue as it was a few years ago (with the way browsers are changing to enhance security).

Anyway, if you want to force a browser to accept a file as a download instead of opening it, I have found it is best to just be as vague as you can in your headers so the browser wont know what to do with it besides ask the user. Below is what I have used for years and it always seems to work no matter what browser or computer you are on.

header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="myfile.abc"');
header('Content-Length: ' . filesize('myfile.abc'));

readfile('myfile.abc');

This is something I just want to log for posterity out of an old email of mine.

Database Schema Searching Utility

This is a php script that I wrote a number of years ago. It allows you to search through schema of MySQL databases by table column name. It can connect to multiple servers and it will search the structure of every table in every database.

It is a great tool to find related columns in a complex server situation. You can get the source code here, feel free to download and use it.

I found this in an old email that I sent to myself. I wrote this utility at my first job out of college. They had a software system that had been through a number of reincarnations over the years and they were left with redundant data across numerous servers and databases. I used this script to locate related fields across systems and would then write translation code that would merge data into a consolidated system.

Improved Smarty strip_tags Modifier Plugin

I wrote this because I wanted to incorporate normal php strip_tags functionality into the Smarty modifier. I structured it so it would retain backwards compatibility with the original Smarty strip_tags modifier. You should be able to drop it into your plugins folder and run with it (name the file modifier.strip_tags.php).

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty strip_tags modifier plugin
 *
 * Type:    modifier

 * Name:    strip_tags

 * Purpose: strip html tags from text
 * @link    http://www.smarty.net/manual/en/language.modifier.strip.tags.php
 *          strip_tags (Smarty online manual)
 *
 * @author  Monte Ohrt <monte at ohrt dot com>
 * @author  Jordon Mears <jordoncm at gmail dot com>
 *
 * @version 2.0
 *
 * @param   string
 * @param   boolean optional
 * @param   string optional
 * @return  string
 */
function smarty_modifier_strip_tags($string) {
    switch(func_num_args()) {
        case 1:
            $replace_with_space = true;
            break;
        case 2:
            $arg = func_get_arg(1);
            if($arg === 1 || $arg === true || $arg === '1' || $arg === 'true') {
                // for full legacy support || $arg === 'false' should be included
                $replace_with_space = true;
                $allowable_tags = '';
            } elseif($arg === 0 || $arg === false || $arg === '0' || $arg === 'false') {
                // for full legacy support || $arg === 'false' should be removed
                $replace_with_space = false;
                $allowable_tags = '';
            } else {
                $replace_with_space = true;
                $allowable_tags = $arg;
            }
            break;
        case 3:
            $replace_with_space = func_get_arg(1);
            $allowable_tags = func_get_arg(2);
            break;
    }

    if($replace_with_space) {
        $string = preg_replace('!(<[^>]*?>)!', '$1 ', $string);
    }

    $string = strip_tags($string, $allowable_tags);

    if($replace_with_space) {
        $string = preg_replace('!(<[^>]*?>) !', '$1', $string);
    }

    return $string;
}

/* vim: set expandtab: */

?>

Two examples of same as always:

{$var|strip_tags}

{$var|strip_tags:false}

An example of php style strip_tags (will leave b and p tags):

{$var|strip_tags:'<b><p>'}

An example of both old and new functionality together:

{$var|strip_tags:false:'<b><p>'}

The original plugin (and therefore this plugin) is licensed under the LGPL.

Here is the link to its place on the Smarty plugin wiki.
http://smarty.incutio.com/?page=StripTags

Temporary Password Generation

I am working on registration for a system where it generates a password and then emails to the person registering. Here is the method I used to come up with short, unique and somewhat complex passwords.

I did it in php, but you can use the algorithm in any language.

<?php

function generate_password() {
    return substr(md5(time() . rand()), rand(0, 24), 8);
}

?>

It takes a random eight character slice of an MD5 of the time plus a pseudo-random integer, which will give you an alphanumeric string. It is a short but complex enough of a password to give someone until they can change it to whatever they want. I thought this was pretty clever, so I wanted to share it here.

PHP5 Upgrade Initiative

I didn’t know there was a whole initiative pushing both projects and hosts to upgrade. But with the end of support for PHP4 upcoming at the end of this year, I guess it is good there is one. I also would say that if you are still with a host who will not provide you PHP5 support, I would recommend switching immediately.

http://www.gophp5.org

Analyzing Arguments Sent to a Function

Here is a random trick in PHP that I stumbled upon today. I was trying to split a string into a number pieces and I was passing an equation of variables into substr. I wasn’t getting the result that I was expecting, so I began to look at what data I was passing into it one by one. Then I realized that I could just look at the whole thing by doing something like this.

Take this sample piece of code.

$x = substr($y, ($a + 8), ($b - $a + 8));

I did some calculations to find $a and $b and I was trying to use those numbers to extract a particular piece of the string $x. Instead having to echo $x, $y, $a, and $b individually, I just changed the line to this.

echo "$x = substr($y, ($a + 8), ($b - $a + 8));";

And it very plainly revealed to me what the second and third arguments (start and length) were evaluating to.

= substr(the random string I was extracting from, (22 + 8), (35 - 22 + 8));

This kind of seems like a pretty basic trick to use, but it was just something I never really thought about trying before and it turned out to work pretty well.

PHP’s isset() function

I did a quick experiment with the isset function in PHP that I thought might be useful to others (as well as myself down the road). I was curious as to how ‘isset’ acted when tested against function arguments that do not have default values set in the function definition.

Here is the code:

<?php

function one_arg($one) {
    return (isset($one)) ? 'true' : 'false';
}

class one_arg_class {
    public function one_arg($one) {
        return (isset($one)) ? 'true' : 'false';
    }
}

echo one_arg() . "\n";
echo one_arg(0) . "\n";

echo one_arg_class::one_arg() . "\n";
echo one_arg_class::one_arg(0) . "\n";

$object = new one_arg_class();
echo $object->one_arg() . "\n";
echo $object->one_arg(0) . "\n";

?>

The output came back:
false
true
false
true
false
true

There were warnings produced by the above code as well. I left them out for readability.

The the results show that no matter how you call a function; whether it be globally, or through a class by scope resolution or object declaration; if you do not specify a value to an argument that does not have a default value, ‘isset’ will return false.

The Seagull initiative

After much thought and a little bit of research I have started looking into application frameworks for PHP. What I found was there were a lot website builders and CMS’s that also liked to consider themselves ‘application frameworks’. However I was able to find a few promising leads and I have decided to follow up on one of them.

The Seagull Framework seems to have a nice interface and a good set of documentation and tutorials to dive into. I am hoping to find something that I can use to assist me in developing my little random apps. I haven’t got it up and running yet but I plan to soon. If it turns into anything promising I will follow up with more information.

Other frameworks I looked at were:
MODx
Xaraya

I went with Seagull first because it seemed to be all-around the most active, mature and user friendly project. But, I very well may try these others as well (that’s the beauty of open-source).

{
}