Cookie is not set fast enough in my case #87

jscontrust opened this Issue Nov 20, 2012 · 2 comments


None yet

2 participants


Im using a customised version of your adaptive-images within a typo3 project. I dont know why, but the javascript cookie is not set in the adaptive-images.php when it is loaded for the first time. So the default resolution values are loaded when the user comes without an already set resolution cookie to the page for the first time.

This happens in firefox and opera mini, chrome manages to set the cookie before the first image is loaded with adaptive-images.

I wrote the following code to avoid this.

if (!isset($_COOKIE['resolution']) && !isset($_GET["dontforward"])) {
    $forward_url = $_SERVER["REQUEST_URI"];
    if (parse_url($forward_url, PHP_URL_QUERY)) {
        $forward_url .=  "&dontforward=true";
    else {
        $forward_url .=  "?dontforward=true";
    header('Location: ' . $forward_url); 

I pasted it after "$browser_cache = 606024*7;". When the cookie is not available, it forwards to itself. To avoid a forwarding loop, it adds an GET-Parameter to the url, so that the forwarding is only done one time.


That's pretty nice and does solve the issue you're having - but it would lead to some nasty behaviour if cookies were disabled on the user's browser. It'd mean that all image URLs would be requested twice every time, which is a big problem - the request is usually the most time consuming bit of a mobile connection.


Yes, you are right. I think, that most useres have activated cookies, but for the users who have not, the page will be slow.

But I think it is important to find a good solution for that problem. When my page has a adaptive image resolution of 320, and I open the page with a Pad which sends the word "mobile" in the user agent and the cookie is not set, then I will have really ugly pictures, because they will be upscaled.

I could imagine thre eother solutions, which work withouth cookies. In all of them, javascript with ajax is needed to make a GET request to the adaptive-images.php with a get parameter, for example ?setcookie=true&res=1280&ratio=1. The other possibility would be to make a second file (i.E. adaptive-images-setcookie.php and request it with res and ratio GET Parameter).

And then in PHP we could:

Possibility 1) Session without cookie
Set a session without cookies:

Could make problems with CMS systems, because the SessionID has to be in the URL. And it is not really beautiful to have it in the url.

Possibility 2) Make our own session
We could create text-files in the ai-cache, whoose name is slugify(ip-useragent).txt. In this file, we could save the resolution and ratio value like in the cookie. When the cookie is not set, we could try to open the file first, before falling back to the default values.

Could make problems on pages with a lot of users. There had to be a good cleanup idea :)

Possibility 3) Session in a Database
Same like number 2, but not with files but with Database entry.

Would be a good way I think, but it would complicate the installation of adaptive images...

@MattWilcox MattWilcox closed this Jul 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment