Asked  2 Years ago    Answers:  5   Viewed   74 times

I'm totally new to how to cache images.

I output all images in a gallery with PHP, and want the images already shown, to be cached by the browser, so the PHP script don't have to output the same image again. All I want is the images to show up faster.

When calling an image I do like this:

<img src="showImage.php?id=601">

and the showImage.php-file does:

$id = (int) $_GET['id'];
$resultat = mysql_query("
    SELECT filename, id
    FROM Media 
    WHERE id = $id
$data = mysql_fetch_assoc($resultat);


//Only if the user are logged in
    header("Content-Type: image/jpeg");

    //$data['filename'] can be = dsSGKLMsgKkD3325J.jpg



If you are using php to check if the user is logged in before outputting the message, then you don't want the browser to cache the image.

The entire point of caching is to call the server once and then never call it again. If the browser caches the image, it won't call the server and your script won't run. Instead, the browser will pull your image from cache and display it, even if the user is no longer logged in. This could potentially be a very big security hole.

Sunday, August 7, 2022

I wrote this file cache function which basically just replaces file_get_contents. You can specify the amount of time the cache should last for in $offset or completely override the cache with $override. If you don't want to use /tmp/, just change that directory to something you can read/write to.

function cache_get_contents($url, $offset = 600, $override = false) {
    $file = '/tmp/file_cache_' . md5($url);
    if (!$override && file_exists($file) && filemtime($file) > time() - $offset)
        return file_get_contents($file);

    $contents = file_get_contents($url);
    if ($contents === false)
        return false;

    file_put_contents($file, $contents);
    return $contents;
Tuesday, October 25, 2022

Under Windows it seems that files in vendor/bin are actually batch files, invoking the original file (and not php files which phpdbg will understand).

In this case:

dir=$(d=${0%[/\]*}; cd "$d"; cd "../phpunit/phpunit" && pwd)
"${dir}/phpunit" "[email protected]"

i.e. ../phpunit/phpunit/phpunit (this path is relative to vendor/bin); thus the actual file is at vendor/phpunit/phpunit/phpunit.

And you can invoke it directly via phpdbg -qrr vendor/phpunit/phpunit/phpunit then.

Friday, December 23, 2022

Put the entire string in double quotes since spaces are not allowed in command line. In the following i enclosed both usages of $label in double-quotes using the " escape sequence:

$cmd = " -background transparent -font $font -pointsize $size label:"$label" -stroke black -strokewidth 2 ".
"  ( -clone 0 -tile "" . $image . "[0]" -stroke black -strokewidth 2 -gravity center -annotate +0+0 "$label" ) ".
Thursday, November 10, 2022

This works in all browsers:

window.location.href = '...';

If you wanted to change the page without it reflecting in the browser back history, you can do:

Sunday, November 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages