PHP generated image caching

So a recent challenge I had was storing binary data (a PNG image) in a MySQL database behind PHP, to which I would pull out upon request, format it and use it as a displayed image in the webpage.

My initial code looked very simple and easy.

header(“Cache-Control: private, max-age=10800, pre-check=10800”);
header(“Pragma: private”);
header(“Expires: ” . date(DATE_RFC822,strtotime(” 2 day”)));

header(‘Content-type: image/png’);
header(‘Content-MD5: ‘. base64_encode(pack(‘H*’, md5($row[‘Icon’]))));
header(‘Content-Length: ‘.filesize($row[‘Icon’]));
header(‘Content-Disposition: attachment; filename=”‘.$_GET[“ElementID”].’.png”‘);
echo $row[‘Icon’];

Now I wouldn’t suggest using this, as I found out Content-Length was way wrong, along with expires just set for 2 days. I needed to manually flush my browser cache to see any updates. Bad way to push out an image from MySQL through PHP.

The link below from was the majority of my solution, but to that I had to change the way it got the additional header information. This was because apache_request_headers() wasn’t a function in my PHP. So in the link to php manual (below) I found and then had to slightly modify the code for the .htaccess file to this:

RewriteEngine on
RewriteRule (.*) – [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule (.*) – [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]

Now I can use:


And how did I check for update change? Just set a hook in MySQL to update a timestamp  whenever that row is updated!

Links where I got info:


I have an increased interest in databases, their management, structure, and the inner workings of them. I think this interest stems from the fact that I know *some* concepts in DBA, but I am by no means anything above a rookie. Often I have found structuring and normalizing databases to be a fun and challenging task. I had struggled with the concept at first, but as I became more familiar with its functionality it has become quite intriguing.

In my early years of application development, working with any sort of persistent data was always a huge challenge. More often than not I would have to come up with some semblance of a file format that would do the job. This technique worked for the trivial applications of yesteryears. But as I have matured in being a developer the need for multi-user data access has made the database a beautiful thing!

DBA and DB programming is just one more good example that the more you learn how to work with it, the more easier it is to accomplish what you set out to do. I am at the stage where learning a new technique that works well and efficiently with databases has with it an extreme sense of reward. Learning something new in a topic that interests you, yet you know little about is quite the thrill. No matter how ‘dry’ of a concept databases may seem.

This is one thing I wish was present better to me in college. Instead of running pointless SQL INSERT statements on data, and working with boring concepts that I suppose get the point across, but don’t give much interest to it… Perhaps instead it would be better to teach the student all the while programming an application that the student actually wants. I learned over 85% of my knowledge about databases from work outside of the classroom setting. As a matter of fact it wasn’t until after I had graduated from college that I took notice of the use for databases and developed a respect for common-practice normalization. It was the driving interest of creating an application that interested me when I realized that I would need a strong DB back-end to make things possible. In thus was born my climbing interest in databases.

I suppose there was always an interest somewhere in me for data structuring, as the fine detailed differences in disk formats always interested me. I often catch myself reading up on the latest developments of BTRFS and NTFS. Or researching the performance benefits or weighing differences and features between common-place file formats. For the fact that disk formatting is very similar to database strucutring. They both store and retrieve data, one is more dealing with individual items of data (not always though), and the other is dealing with linked together (relational) items. More and more though this line is getting blurred.

Anyways, this is my interest that I have more recently become aware of.


$query = “INSERT something into some table somewhere“;
//  Run query
$row = mysql_query($query);

// Make sure query inserted user successfully
if ( !mysql_insert_id() ){
die(“Error: User not added to database.”);

This little nifty function “mysql_insert_id()” has solved several of my problems, although the problems were never surfaced, I knew that it would lead to problems in high traffic scenarios. My old method was to do the insert and then immediately call for the latest ‘ID’ by date… the problem is what happens when two people click the ‘submit’ button at the same time? The ‘ID’ returned to the user may be the other persons! All depending on the somewhat random order that the code gets executed. I knew this would be a problem, but now after finding this nifty function, it has alleviated all of that! Says this:

The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established.

For you PHP/MySQL programmers out there, note this down! It is good to know for the bigger than normal DB-based project.