• Resolved Fungafly

    (@fungafly)


    Hi,

    Just thought I’d let the author know of an unusual problem that I encountered and how I got around it, just in case somebody has the same problem.

    I’ve recently had this issue on an Apache server with Varnish cache where the HTTP cookies were not being sent to the adaptive image script (a dump of $_COOKIE showed it was always empty). So it never knew my screen resolution and thus it served the original image at all times.

    Some testing indicated that the server did this when the request URL was for an image. A script called test.php would receive the cookies normally through $_COOKIE, whether accessed directly on the browser or through an .htaccess redirect from a fake address such as /test. However, if the fake address had an image extension, such as /test.jpg, the cookies were no longer received.

    Fortunately, there was a way around, as in these cases the server was populating $_SERVER[‘HTTP_X_COOKIES’] with the raw cookie data (e.g. var1=value; resolution=640%2c2).

    So I reworked the adaptive-images-script.php around line 82 to include an extra check, if it failed to obtain the cookie value directly:

    if ( isset( $_GET['resolution'] ) ) {
    
    	$cookie_resolution = $_GET['resolution'];
    
    } elseif ( isset( $_COOKIE['resolution'] ) ) {
    
    	$cookie_resolution = $_COOKIE['resolution'];
    
    } elseif ( isset( $_SERVER['HTTP_X_COOKIES'] ) && $_SERVER['HTTP_X_COOKIES'] ) {
    
    	$forwardedCookie = urldecode( $_SERVER['HTTP_X_COOKIES'] );
    	preg_match( '#(?:^|;\s*)resolution=(\d+,\d+)#', $forwardedCookie, $cookieMatch );
    
    	$cookie_resolution = $cookieMatch ? $cookieMatch[1] : null;
    	$_COOKIE['resolution'] = $cookie_resolution; // Important for other checks further on
    
    } else {
    
    	$cookie_resolution = null;
    
    }

    Cheers!

    https://wordpress.org/plugins/adaptive-images/

Viewing 1 replies (of 1 total)
  • Plugin Author Takis Bouyouris

    (@nevma)

    Hey there, @fungafly,

    Thank you very much, for pointing this out. Indeed the intended behaviour is that any caching server or Varnish or CDN should allow cookies to propagate to the origin server in order for the plugin to receive and make use of them.

    I will add your fix to our next version!

    Cheers,
    Takis

Viewing 1 replies (of 1 total)
  • The topic ‘Cookie failure on specific server setup’ is closed to new replies.