Posts Tagged ‘PHP’

Force SSL by loading the same page via HTTPS

December 26th, 2009 | No Comments »

Just a simple one, I place this in a common file, and then call it at the top of each page that I require to be secured by SSL.

function force_ssl() {
    if ($_SERVER['HTTPS'] != 'on') {
        header("Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
        exit();
    }
}
Tags: | Categories: Useful Stuff

MySQL string escaping in PHP

December 26th, 2009 | No Comments »

Simple function to prepare user inputted data for inserting into a MySQL database. It simply checks if stripslashes() should be called, and then escapes the string. Basic, but handy.

function mysql_escape($string) {
    if (get_magic_quotes_gpc() == 1) {
        $string = stripslashes($string);
    }
    $string = mysql_real_escape_string($string);
    return $string;
}
Tags: , | Categories: Useful Stuff

The most useful PHP function you’ll ever use!

December 26th, 2009 | No Comments »

This is the first thing I add to any PHP based project and I couldn’t live without it..

Most people will probably understand and appreciate what this does, but for those that don’t, it simply displays the contents of an array in a very human readable way by using the <pre> tag to preserve the spacing of the print_r() command.

// Debug Array
function da($array) {
    echo "<pre>\n";
    print_r($array);
    echo "</pre>\n";
}

Enjoy!

Tags: | Categories: Useful Stuff