Prevent Image Caching

Browser image caching was causing me a problem today.
When a user of a client site uploads a profile image, it intentionally overwrites the old image. However, when viewing the profile page, the old image is shown unless you refresh the page.

Preventing browser image caching

The solution was quite simple. In order to force the browser to fetch the image from the server, I add a random number to a querystring at the end of the image name. Example:

No Comments Posted in Web development, php
Tagged , , , ,

PHP Multidimensional Arrays

Multidimensional arrays are arrays which contain arrays and each of those may also contain arrays. There is no limit to the number or level of sub-arrays.


$stoves = array(
0=>array('Bronze',
'elements'=>array('4 rings', 'oven'),
),
1=>array('Silver',
'elements'=>array('4 rings', 'oven', 'grill'),
)
?>

End of Multidimensional Arrays.

No Comments Posted in php
Tagged , ,
Web Developer Interview Questions

Most of us know what general interview questions to expect, but sometimes we may not be as prepared for interview questions as we think we are. So, here are a few interview questions that I have been asked whilst at interviews for web developer roles:

  1. How do you keep up to date with trends and changes in web development?
  2. Can you show us some example code?
  3. What is the most complex application you have built?
  4. What is XSS?
  5. How would you prevent MySQL injection?
  6. What can you tell me about CSS sprites?
  7. What do you know about the CSS Box Model?
  8. What is a recursive function?
  9. Do you know what multi-dimensional arrays are?

These are just a few sample web developer interview questions

No Comments Posted in Job hunting
Tagged , ,
Joining Tables: MySQL Join

MySQL

MySQL

MySQL Join Tables

The MySQL JOIN command allows you to acquire/manipulate data from more than one table in a single query statement.

MySQL JOIN commands can be used in SELECT, UPDATE and DELETE statements.


However, MySQL JOIN commands use a lot of database resources as they need conditional matches across all of the tables.

The Simple MySQL Join

This is the easiest of the Join queries. With this JOIN query you are basically conducting two separate queries on two related tables, but joining them into one query. The WHERE condition is used to connect corresponding results.

The syntax is as follows:
SELECT table_a.id, table_a.name, table_b.meals FROM table_a JOIN table_b WHERE table_a.name = table_b.name

In the join example above, you may use a comma in place of the word ‘JOIN‘.

The MySQL LEFT Join

Join Tables with MySQL

Join Tables with MySQL

The LEFT JOIN is slightly more complicated that the simple join. By using a LEFT JOIN, as well as all records which match the WHERE condition, I also retrieve all the records from the first table (left of the join) which do not match the WHERE condition.

The syntax is as follows:
SELECT table_a.id, table_a.name, table_b.meals FROM table_a LEFT JOIN table_b ON table_a.name = table_b.name

The MySQL Inner Join

INNER JOIN behaves in the same way as LEFT JOIN except that if no matches are found, no results are created.

The syntax is as follows:
SELECT table_a.id, table_a.name, table_b.meals FROM table_a INNER JOIN table_b WHERE table_a.name = table_b.name

1 Comment Posted in mysql
Tagged , , ,
htaccess AddType for Custom web page suffix

A week or so ago, another developer showed me the Nectar Points website (www.nectar.com) and pointed out that the webpages appear to have a custom suffix – in this case .nectar

So, how did they do it?

It appears that the website is on an Apache server, my guess is that they used the .htaccess ‘AddType’ directive.

The AddType Directive

The AddType directive allows you to specify what MIME type should be reported for specified file suffixes. This MIME type will then be used by the browser when serving up a web page having the specified suffix.

The syntax:
AddType mime-type file-extension

So, if I decide to change the suffix of any of my PHP files to ‘.beer’, then I will use this in the .htaccess file:
AddType application/x-httpd-php .beer

Now, pagename.beer will be treated by the browser like any other PHP file.

3 Comments Posted in Web development
Tagged , , ,
What is MySQL Injection?

MySQL injection occurs when someone unauthorised attempts to run an MySQL statement on your database. This will normally take the form of inserting a MySQL statement where you would normally expect some user input – maybe their username.

MySQL Injection Prevention

There is a PHP function which can be used to prevent MySQL injection attacks:

mysql_real_escape_string($variable)

The mysql_real_escape_string() makes the string safe from MySQL injection by escaping any injection code in the data which is to be used in the MySQL query.

MySQL injection attempts normally have the single quote character(‘). mysql_real_escape_string() inserts a backslash in front of it to escape it.

3 Comments Posted in mysql
Tagged , , , , ,
What are PHP recursive functions

Recursive functions are functions which call themselves. They are particularly useful when working with multi-dimensional arrays. However, using recursive functions should be avoided if the function is likely to be called over 200 times, as it may kill the script.

Here is a very basic example of how one could use a recursive function in PHP:

<?php


# This function iterates over the main array
# It checks each value and if not an array adds to variable
# If one of the values is an array the function calls itself
# and passes that sub array to itself (the function)

function total($numbers)
{
$total = FALSE; // initialise variable
foreach ($numbers as $number) {
if(is_array($number)) { //is the value an array
$total += total($number); //calls itself & passes sub array
} else { // not an array
$total += $number; // add values to variable
}
}
return $total;
}

$numberlist = array(10, 20, 5,
array(5, 2, 3)
);
$result = total($numberlist); // pass arrays to function
echo “Total = $result\n”;
?>


This example is a very basic recursive function.

No Comments Posted in Web development, php
Tagged , , ,
What is Codeigniter

CodeIgniter

CodeIgniter

CodeIgniter – created by EllisLabs and first released in 2006, CodeIgniter is a freely available MVC web application framework used for developing websites using PHP.

CodeIgniter ships with a large library commonly used OOP classes, enabling the developer to call them rather that using time creating her own. When compared to other similar MVC frameworks, CodeIgniter is said to be:

“..faster, lighter and the least like a framework.”  Rasmus Lerdorf 2008

Benefits of CodeIgniter

  • Get started quickly
  • No need to install other packages
  • No need for mass configuration
  • Compatible with PHP 4 & 5
  • Detailed user guide
  • Enables faster cleaner development
  • Easy to extend by adding your own classes
  • It’s OpenSource
  • MVC architecture allows for code separation

More information about CodeIgniter is available from CodeIgniter.com

1 Comment Posted in Codeigniter
Tagged , , ,
CodeIgniter base_url: Make it dynamic

CodeIgniter Base URL

When you install the CodeIgniter framework on your server, the base URL and certain other settings are automatically configured on install. These settings are relevant to the server you are installing on. The base url is set to the domain you are installing CodeIgniter on (eg. www.uk-webdeveloper.co.uk) and is called by a method in my class Start extends Controller in this manner:

function Start()
{
parent::Controller();
$this->load->helper('url');
$this->load->library('menu');
$this->load->library('menulower');
$this->load->library('banner');
$this->base = $this->config->item('base_url');
$this->css = $this->config->item('css');
}


As you can see from the method/function shown here, the base_url is located in the config file of your CodeIgniter installation.

Now, as mentioned above, CodeIgniter sets the base_url on  install to that of the domain it is installed to. So what happens when you move the whole application to another domain? It may stop functioning.

The Base URL solution

The solution is quite simple.

1. Locate and open your config file (usually at system/application/config/config.php)

2. Find this line:
$config['base_url']    = ‘http://www.yourdomain.co.uk’

3. Replace that line with
$config['base_url']    = ‘http://’.$_SERVER['HTTP_HOST']

4. Save the file.

Now wherever you move the application to, it should still work as expected.

codeigniter base url

No Comments Posted in Codeigniter
Tagged , , , ,
Start with HTML5

HTML5

Getting started with HTML5 is very easy.

HTML 5 is compatible with the XHTML syntax of self-closing tags, so, if you already use HTML5 then making the switch will be easy. Other XHTML features inherited by HTML5 include lowercase tags and double quoted attributes (eg, title=”the title”)

The Doctype declaration in HTML5 is so much easier to remember when compared to XHTML:

<!DOCTYPE html>

There’s a lot more to HTML5 and I will update this entry as I get more info.

 

No Comments Posted in General