Hi, I have a complicated (for me) question and it is driving crazy... I just can't find the issue. Basically I'm trying to enable the browser cache for a certain php file. I'm using a script called osdate but don't have much success getting help on their forum. To explain it clear and to the point, that's what I have now:
- in the php file (getsnap.php) I have this:
ob_start();
if ( !defined( 'SMARTY_DIR' ) ) {
include_once( 'minimum_init.php' );
}
include(OSDATE_INC_DIR.'internal/snaps_functions.php');
if (isset($_REQUEST['username']) && $_REQUEST['username'] != '') {
$userid = $osDB->getOne('select id from ! where username = ?',array(USER_TABLE, $_REQUEST['username']) );
} else {
// include ( 'sessioninc.php' );
if( !isset($_GET['id']) || (isset($_GET['id'])&& (int)$_GET['id'] <= 0 ) ) {
$userid = $_SESSION['UserId'];
} else {
$userid = $_GET['id'];
}
}
if (!isset($_GET['picid']) ) {
if ((isset($_REQUEST['type']) && $_REQUEST['type'] != 'gallery') || !isset($_REQUEST['type']) ) {
$defpic = $osDB->getOne('select picno from ! where userid = ? and ( album_id is null or album_id = ?) and default_pic = ? and active = ? ',array(USER_SNAP_TABLE, $userid,'0','Y','Y' ) );
if ($defpic != '') {
$picid = $defpic;
} else {
$picid = $osDB->getOne('select picno from ! where userid = ? and ( album_id is null or album_id = ?) and active=? order by rand()',array(USER_SNAP_TABLE, $userid,'0','Y' ) );
}
unset( $defpic);
}
} else {
$picid = $_GET['picid'];
}
$typ = isset( $_GET['typ'])?$_GET['typ']:'pic' ;
$cond = '';
if ( ($config['snaps_require_approval'] == 'Y' || $config['snaps_require_approval'] == '1') && $userid != $_SESSION['UserId'] ) {
$cond = " and active = 'Y' ";
}
$sql = 'select * from ! where userid = ? and picno = ? '.$cond;
/* Get the watermarked picture file from cache directory. */
$row =& $osDB->getRow ( $sql, array( USER_SNAP_TABLE, $userid, $picid ) );
$img = getPicture($userid, $picid, $typ, $row);
$ext = ($typ = 'tn')?$row['tnext']:$row['picext'];
if ( $img != '' && ( ( hasRight('seepictureprofile') && ( $config['snaps_require_approval'] == 'Y' && $row['active'] == 'Y' ) ||$config['snaps_require_approval'] == 'N' ) || $userid == $_SESSION['UserId'] ) ) {
$img2 = $img;
} else {
$gender = $osDB->getOne( 'select gender from ! where id = ?', array( USER_TABLE, $userid ) ) ;
if ($gender == 'M') {
$nopic = SKIN_IMAGES_DIR.'male.jpg';
} elseif ($gender == 'F') {
$nopic = SKIN_IMAGES_DIR.'female.jpg';
} elseif ($gender == 'C') {
$nopic = SKIN_IMAGES_DIR.'couple.jpg';
}
$img2 = imagecreatefromjpeg($nopic);
$ext = 'jpg';
}
ob_end_clean();
header("Pragma: public");
header("Content-Type: image/".$ext);
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 * 24 * 7;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header($ExpStr);
header("Content-Disposition: attachment; filename=profile_".$userid."_".$typ.".".$ext);
/*
if ($_SESSION['browser'] != 'MSIE') {
header("Content-Disposition: inline" );
}
*/
if ($ext == 'jpg') {
imagejpeg($img2);
} elseif ($ext == 'gif') {
imagegif($img2);
} elseif ($ext == 'png') {
imagepng($img2);
} elseif ($ext == 'bmp') {
imagewbmp($img2);
}
imagedestroy($img2);
- in the htaccess file I have this:
<IfModule mod_php5.c>
php_value display_errors 0
php_value allow_url_fopen 0
</IfModule>
<Files showprofile-mini.php>
ExpiresDefault "access plus 2419200 seconds"
</Files>
<Files getsnap.php>
ExpiresDefault "access plus 2592000 seconds"
</Files>
<Files showprofile.php>
ExpiresDefault "access plus 604800 seconds"
</Files>
<ifModule mod_deflate.c>
<filesMatch "\.(css|js|x?html?|php|txt)$">
SetOutputFilter DEFLATE
</filesMatch>
</ifModule>
# END Compress text files
# BEGIN Expire headers
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 600 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/jpg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
ExpiresByType text/css "access plus 2592000 seconds"
ExpiresByType text/javascript "access plus 2592000 seconds"
ExpiresByType application/javascript "access plus 2592000 seconds"
ExpiresByType application/x-javascript "access plus 2592000 seconds"
ExpiresByType text/plain "access plus 604800 seconds"
ExpiresByType text/html "access plus 3600 seconds"
ExpiresByType application/xhtml+xml "access plus 3600 seconds"
</ifModule>
# END Expire headers
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch "getsnap">
Header set Last-Modified "Mon, 9 Jan 2012 00:00:00 GMT"
Header append Cache-Control "public, must-revalidate, proxy-revalidate"
Header set Cache-Control "max-age=2592000, must-revalidate"
</filesMatch>
<filesMatch "\.(ico|jpeg|jpg|png|gif|swf|bmp)$">
Header set Last-Modified "Mon, 9 Jan 2012 00:00:00 GMT"
Header append Cache-Control "public, must-revalidate, proxy-revalidate"
Header set Cache-Control "max-age=2592000, must-revalidate"
</filesMatch>
<filesMatch "\.(css|js)$">
Header set Last-Modified "Mon, 9 Jan 2012 00:00:00 GMT"
Header append Cache-Control "public, must-revalidate, proxy-revalidate"
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch "\.(x?html?|php|htm|txt)$">
Header set Last-Modified "Mon, 9 Jan 2012 00:00:00 GMT"
Header append Cache-Control "public, must-revalidate, proxy-revalidate"
Header set Cache-Control "max-age=3600, private, must-revalidate"
</filesMatch>
</ifModule>
# END Cache-Control Headers
# BEGIN Turn ETags Off
<ifModule mod_headers.c>
Header unset ETag
</ifModule>
FileETag None
# END Turn ETags Off
# BEGIN Remove Last-Modified Header
<ifModule mod_headers.c>
Header unset Last-Modified
</ifModule>
# END Remove Last-Modified Header
# Protect .htaccess and .htpasswd files
<FilesMatch "^\.ht">
Order allow,deny
Deny from all
</FilesMatch>
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)_([0-9]+).jpg$ getsnap.php?username=$1&typ=tn&height=100&picid=$2
RewriteRule ^([^/]+).jpg$ getsnap.php?username=$1&typ=tn&height=100
RewriteRule ^([^/]+).jpg$ getsnap.php?username=$1&typ=tn
So, as you can see, I rewrite the getsnap.php to look like a .jpg file (trying to get a higher rank in Page Speed Score). Now, what I'm trying to do is set a cache, for the getsnap.php, of 1 week (2592000"). I'm trying to do this in the php code and in the htaccess file. The problem is that the cache seems to be set by the ExpiresByType application/xhtml+xml, in the htaccess (1 hour), and the Page Speed tool keeps telling me that the cache is to low and it should be 1 week, at least, for the .jpg (getsnap.php) files.
How can I set the cache to one week, for only getsnap.php? I don't want to modify the ExpiresByType application/xhtml+xml because it will make all php files expire after one week, and I want only one. I thought the code from the php file will be enough:
$offset = 60 * 60 * 24 * 7;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
...but it seems it is overwritten by the htaccess :(
Is there any way to do this?
Thanks!