Jump to content

cached images sometimes called over http not https


Recommended Posts

I'm using php code I got from StackOverload that looks like this:

 

	<?php
// Some URL for testing
$url = 'https://www.google.de/favicon.ico';
	// Time to cache the files (here: 10 minutes)
define('time_to_cache', 600);
	// Create a local file representation
$local = './cache/' . urlencode($url);
	// Determine whether the local file is too old
if (@filemtime($local) + time_to_cache < time()) {
    // Download a fresh copy
    copy ($url, $local);
	    // Store headers in case we need them (see alternative below)
    file_put_contents($local . '.hdr', join($http_response_header, "\n"));
}
	// Solution 1: Redirect to the local cache file
header('Location: ' . urlencode($local));
exit();
	// Alternative: Send headers and the actual file
// Note that this might cause problems, e.g. due
// to cache fields and the like.
	// Read and send headers
foreach(file($local . '.hdr') as $line)
    header($line);
	// Read and send the actual file
readfile($local);
	?>
	 
	

I've remarked out the "solution 1" lines, because that method doesn't work at all on my server. The alternative works, except for an https problem.

I'm loading a bunch of images from 3rd party sites. My site is only https, and I want to keep all local images using https.

I have lazyloading js applied to all images that load on the page, and when I use the above php, as I scroll down my page, my browser https lock will turn to unsecure. So I can assume, that lots of the images are loading properly from https, and then suddenly, it loads an image from http instead of https. So for some reason, the above php is not keeping my https, despite that the script looks to only call the image from my local location.

 

I'm fairly new to learning php, but am I missing something here? From what I can see from the "readfile" line, all images should be shown from my cached area, which I've already stated as https, and have tried both "//" and specifically hardcoding "https" into the img src, but I still get these problems, where as I scroll down the page, the browser https secure lock will change to the mixed content, indicating that the browser has just loaded something NOT from https.

Can anybody help me with what I need to add, so that the above code either keeps my secure protocol or doesn't try to load the image if it's not from https?

Any help would be appreciated!

Link to post
Share on other sites

It sounds like a redirect coming from one of the files you cached - specifically, something in the list of headers you downloaded.

How much control do you have over the site configuration at the server level? Do you have root access? Can you change your host/virtualhost configuration? What you need here is a reverse proxy and that's best handled by the web server and not PHP code.

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.