Re: [PHP] 304 Not Modified header not working within a class

2010-01-23 Thread Camilo Sperberg
Problem solved!!!

Everything was working ok with PHP. My class was working ok. The engineering
and logic behind PHP was working. So... what was the problem? Apache...
well, it wasn't a problem, but a misconfiguration or better said, a
mis-optimization.
In my first message, I stated: (quote)

 there is no way I can send a 304 Not Modified header, when the data is
 *over* ~100 bytes.


After 8 hours of working with this problem (which included sniffering and a
lot of workarounds), and while I was smoking my final cigarrette before
going to bed, I remembered that some time ago (well, some time like 2 years
ago xD), I had enabled mod_disk_cache, with the following configuration:
IfModule mod_disk_cache.c
  CacheRoot /tmp/apachecache/
  CacheEnable disk /
  CacheDirLength 1
  CacheDirLevels 5
  CacheMaxFileSize 128000
  *CacheMinFileSize 100*
/IfModule

I commented that part, restarted Apache and bingo Instantly I had an 304
header.
What do I think the problem was? Whenever Apache received a request, it
handled it directly from _his_ cache and simply omitted what PHP was telling
him to do. The weird thing was that the class entered the 304 header part,
but Apache always ended up sending an 200 OK header and then the CSS. In
first place it shouldn't have sended the CSS because when I entered the 304
part, it should have died. It simply couldn't send any other output. (And
that was why I sniffered, if it shouldn't send the CSS; he must have been
send some kind of error, but my surprise was really big when I saw that the
raw data was just plain CSS, no other data was present).
Why was Apache then sending a 304 whenever the data was under the 100 byte
limit? Because he didn't have it in his cache and was obeying what PHP told
him to do. (This configuration created a cache whenever the file size is
between the 100 and 128000 bytes).

Anyway... now I will be publishing the class soon on phpclasses.org under
the BSD license. I'll work now on documentation and code cleanup but
whenever it is ready I will leave the link in this same list (if it is
allowed) xD

Greetings, a lot of thanks to Richard for his code and Rene for his
suggestion to take a look at Apache and good night :P (Despite being 7AM xD)


On Wed, Jan 20, 2010 at 21:16, Camilo Sperberg csperb...@unreal4u.comwrote:



 On Wed, Jan 20, 2010 at 04:34, richard gray r...@richgray.com wrote:


 Camilo Sperberg wrote:

 Hi list, my first message here :)

 To the point: I'm programming a class that takes several CSS files,
 parses,
 compresses and saves into a cache file. However, I would like to go a
 step
 further and also use the browser cache, handling the 304 and 200 header
 types myself.

 Now, what is the problem? If I do it within a function, there is
 absolutely
 no problem, everything works like a charm. However, when I implement that
 same concept into my class, there is no way I can send a 304 Not Modified
 header, when the data is *over* ~100 bytes.



 Hi Camilo

 For what it is worth I have implemented cacheing in a class and for me the
 304 not modified header gets sent fine ... some example headers output is
 below together with the relevant code snippet..

 // See if client sent a page modified header to see if we can
 // just send a not modified header instead
 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) 
 $_SERVER['HTTP_IF_MODIFIED_SINCE'] == self::$_gmmodtime) {

   header('HTTP/1.1 304 Not Modified');
   return null;
 }

 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) 
 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == self::$_etag) {

   header('HTTP/1.1 304 Not Modified');
   return null;
 }


 HTTP/1.x 304 Not Modified
 Date: Wed, 20 Jan 2010 07:21:32 GMT
 Server: Apache/2.2.11 (Ubuntu)
 Connection: Keep-Alive
 Keep-Alive: timeout=5, max=1000
 Etag: 444fbd9951f540ec1b6928db864c10dc
 Expires: Sun, 24 Jan 2010 06:16:06 GMT
 Cache-Control: public, must-revalidate
 Vary: Accept-Encoding

 I hope it helps..

 Regards
 Rich


 I'll try this (and some other things I recently thought about) when I get
 back home on friday :) I'll keep you updated.

 Thanks!


 --
 Mailed by:
 UnReAl4U - unreal4u
 ICQ #: 54472056
 www1: http://www.chw.net/
 www2: http://unreal4u.com/




-- 
Mailed by:
UnReAl4U - unreal4u
ICQ #: 54472056
www1: http://www.chw.net/
www2: http://unreal4u.com/


Re: [PHP] 304 Not Modified header not working within a class

2010-01-20 Thread Camilo Sperberg
On Wed, Jan 20, 2010 at 04:34, richard gray r...@richgray.com wrote:


 Camilo Sperberg wrote:

 Hi list, my first message here :)

 To the point: I'm programming a class that takes several CSS files,
 parses,
 compresses and saves into a cache file. However, I would like to go a step
 further and also use the browser cache, handling the 304 and 200 header
 types myself.

 Now, what is the problem? If I do it within a function, there is
 absolutely
 no problem, everything works like a charm. However, when I implement that
 same concept into my class, there is no way I can send a 304 Not Modified
 header, when the data is *over* ~100 bytes.



 Hi Camilo

 For what it is worth I have implemented cacheing in a class and for me the
 304 not modified header gets sent fine ... some example headers output is
 below together with the relevant code snippet..

 // See if client sent a page modified header to see if we can
 // just send a not modified header instead
 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) 
 $_SERVER['HTTP_IF_MODIFIED_SINCE'] == self::$_gmmodtime) {

   header('HTTP/1.1 304 Not Modified');
   return null;
 }

 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) 
 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == self::$_etag) {

   header('HTTP/1.1 304 Not Modified');
   return null;
 }


 HTTP/1.x 304 Not Modified
 Date: Wed, 20 Jan 2010 07:21:32 GMT
 Server: Apache/2.2.11 (Ubuntu)
 Connection: Keep-Alive
 Keep-Alive: timeout=5, max=1000
 Etag: 444fbd9951f540ec1b6928db864c10dc
 Expires: Sun, 24 Jan 2010 06:16:06 GMT
 Cache-Control: public, must-revalidate
 Vary: Accept-Encoding

 I hope it helps..

 Regards
 Rich


I'll try this (and some other things I recently thought about) when I get
back home on friday :) I'll keep you updated.

Thanks!

-- 
Mailed by:
UnReAl4U - unreal4u
ICQ #: 54472056
www1: http://www.chw.net/
www2: http://unreal4u.com/


Re: [PHP] 304 Not Modified header not working within a class

2010-01-19 Thread Camilo Sperberg
On Wed, Jan 20, 2010 at 02:33, Rene Veerman rene7...@gmail.com wrote:

 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) AND
 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified) {


 shouldn't that be

 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) = $last_modified)

 ?


Now that I think about it... yes; but I send the last modified header anyway
the first time (when $_SERVER['HTTP_IF_MODIFIED_SINCE'] == null):
header('Last-Modified: '.gmdate('D, d M Y H:i:s',$last_modified).' GMT');

So if it isn't exactly equal, then the browser cache simply doesn't have the
latest version. It is impossible anyway that the browser can have a newer
version that doesn't previously exist on the server.
My best guess is that it doesn't affect the process: when I implement that
code in my class, it enters that part (meaning all the comparisons are ok)
but afterwards it keeps sending an 200 OK header when I explicitly tell
Apache to send the 304 Not Modified one.

Greetings and thanks for sharing :)

-- 
Mailed by:
UnReAl4U - unreal4u
ICQ #: 54472056
www1: http://www.chw.net/
www2: http://unreal4u.com/


Re: [PHP] 304 Not Modified header not working within a class

2010-01-19 Thread Rene Veerman
ok, you might wanna re-ask on an apache list in that case..

On Wed, Jan 20, 2010 at 6:48 AM, Camilo Sperberg unrea...@gmail.com wrote:


 On Wed, Jan 20, 2010 at 02:33, Rene Veerman rene7...@gmail.com wrote:

 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) AND
 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified) {


 shouldn't that be

 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) = $last_modified)

 ?

 Now that I think about it... yes; but I send the last modified header anyway
 the first time (when $_SERVER['HTTP_IF_MODIFIED_SINCE'] == null):
 header('Last-Modified: '.gmdate('D, d M Y H:i:s',$last_modified).' GMT');

 So if it isn't exactly equal, then the browser cache simply doesn't have the
 latest version. It is impossible anyway that the browser can have a newer
 version that doesn't previously exist on the server.
 My best guess is that it doesn't affect the process: when I implement that
 code in my class, it enters that part (meaning all the comparisons are ok)
 but afterwards it keeps sending an 200 OK header when I explicitly tell
 Apache to send the 304 Not Modified one.

 Greetings and thanks for sharing :)

 --
 Mailed by:
 UnReAl4U - unreal4u
 ICQ #: 54472056
 www1: http://www.chw.net/
 www2: http://unreal4u.com/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] 304 Not Modified header not working within a class

2010-01-19 Thread richard gray


Camilo Sperberg wrote:

Hi list, my first message here :)

To the point: I'm programming a class that takes several CSS files, parses,
compresses and saves into a cache file. However, I would like to go a step
further and also use the browser cache, handling the 304 and 200 header
types myself.

Now, what is the problem? If I do it within a function, there is absolutely
no problem, everything works like a charm. However, when I implement that
same concept into my class, there is no way I can send a 304 Not Modified
header, when the data is *over* ~100 bytes.

  

Hi Camilo

For what it is worth I have implemented cacheing in a class and for me 
the 304 not modified header gets sent fine ... some example headers 
output is below together with the relevant code snippet..


// See if client sent a page modified header to see if we can
// just send a not modified header instead
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])  
$_SERVER['HTTP_IF_MODIFIED_SINCE'] == self::$_gmmodtime) {

   header('HTTP/1.1 304 Not Modified');
   return null;
}

if (isset($_SERVER['HTTP_IF_NONE_MATCH'])  
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == self::$_etag) {

   header('HTTP/1.1 304 Not Modified');
   return null;
}


HTTP/1.x 304 Not Modified
Date: Wed, 20 Jan 2010 07:21:32 GMT
Server: Apache/2.2.11 (Ubuntu)
Connection: Keep-Alive
Keep-Alive: timeout=5, max=1000
Etag: 444fbd9951f540ec1b6928db864c10dc
Expires: Sun, 24 Jan 2010 06:16:06 GMT
Cache-Control: public, must-revalidate
Vary: Accept-Encoding

I hope it helps..

Regards
Rich

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] 304 Not Modified

2001-09-06 Thread Christian Reiniger

On Wednesday 05 September 2001 21:22, Ouster wrote:
 I'm making a sort of cache system. I fell to this problem: I leave a
 cookie with the timestamp of the last access, and when the user
 reconnect, I compare the timestamp of the last change with the
 timestamp sent me with the cookie. Then I leave a new cookie.

 So:
 if($last_modified = $last_access)
 {
 header(304 Not Modified);
 exit;
 }
 else
 {
 /* code genertating the new page */
 }

Tip: You don't even need a cookie for that. If you send a 
Last-Modified:  header along with all your pages (and perhaps even if 
you don't), the browser will automatically add an If-Modified-Since: 
header on each request. See the attached files for how to handle this

 Whether the If is TRUE, I get a blank page, else I see a new page
 (correct behaviour).
 Why I always get a blank page and not the cached page?

(see other responses)

-- 
Christian Reiniger
LGDC Webmaster (http://lgdc.sunsite.dk/)

/* you are not expected to understand this */

- from the UNIX V6 kernel source


?php
/**
 * Functions for HTTP info handling
 *
 * Authors : Christian Reiniger [EMAIL PROTECTED]
 * License : GNU GPL V2 or later (http://www.fsf.org/copyleft/gpl.html)
 *
 * Last change by $Author$
 * on $Date$
 * to $Revision$
 */


/**
 * Display the 404 - Not Found page
 */
function pbHTTP_404 ()
{
global $pbGlobals;

header (HTTP/1.0 404 Not Found);
include ($pbGlobals-Inc404);
exit ();
}

/**
 * Send a 304 - Not Modified response
 */
function pbHTTP_304 ()
{
header ('HTTP/1.0 304 Not Modified');
header ('Date: ' . gmdate ('D, d M Y H:i:s') . ' GMT');
exit ();
}


/**
 * Get the value passed in an If-Modified-Since header or -1 if none set
 * Returns a Unix timestamp;
 */
function pbHTTP_GetModifiedSince ()
{
foreach (getallheaders () as $Key = $Val) {
if ($Key == 'If-Modified-Since') {
return strtotime (trim ($Val));
}
}

return -1;
}


/**
 * Send a Last-Modified header
 */
function pbHTTP_LastMod ($Unixtime = 0)
{
if ($Unixtime == 0)
$Unixtime = time ();

$TimeS = gmdate ('D, d M Y H:i:s', $Unixtime);

header (Last-Modified: $TimeS GMT);
}


/**
 * Send headers to disallow caching
 */
function pbHTTP_NoCache ()
{
header ('Expires: -1');
header ('Cache-Control: no-cache');
header ('Pragma: no-cache');
}


?

?php
/*
 * Cached page handler
 *
 * Authors : Christian Reiniger [EMAIL PROTECTED]
 * License : GNU GPL V2 or later (http://www.fsf.org/copyleft/gpl.html)
 *
 * Last change by $Author$
 * on $Date$
 * to $Revision$
 */


require_once ('base/defaults.php');

require_once ('phpbase/phpbase.php');
require_once ('phpbase/database.php');
require_once ('phpbase/session.php');
require_once ('phpbase/db/pagecache.php');
require_once ('phpbase/util/http.php');



assert (defined ('PAGETYPE'));

$TheSession = new KSession ();
$TheSession-OpenLight ();

/*
 if ($pbSessionUser-IsRealUser () == false)
	$TheSession-Relogin ();
 */


list ($Page, $CTime) = GetPage ($REQUEST_URI);
if ($Page === false)
pbHTTP_404 ();

HandleCacheHeaders ($CTime);

if (defined ('PAGE_HITCOUNTER') || defined ('PAGE_USERINFO'))
{
$Page = Postprocess ($Page);
}

// There should be a zlib.output_compression = on in a config file!
print $Page;
//Output ($Page);


$TheSession-Close ();








function GetPage ($TheURI)
{
$Usermode = GetUsermode ();
$TheURI = FixURI ($TheURI);
$Page = false;

if (!defined ('PAGE_NOSERVERCACHE'))
{
$Cache = new pbPageCache (PAGETYPE);
$Page  = $Cache-Get ($TheURI, $Usermode);
$CTime = $Cache-GetCTime ();
}

if ($Page === false)
{
include ('ui/page_builder.php');

$Builder = new PageBuilder (PAGETYPE);
$Page = $Builder-Build ($TheURI, $Usermode);
$CTime = time ();
}

return array ($Page, $CTime);
}


function FixURI ($TheURI)
{
if (substr ($TheURI, -5) == '.html')
return $TheURI;

if (substr ($TheURI, -1) == '/')
return $TheURI . 'index.html';
else
return $TheURI . '/index.html';
}


function HandleCacheHeaders ($CTime)
{
if (defined ('PAGE_NOCACHE')) {
pbHTTP_NoCache ();
return;
}

$MS = pbHTTP_GetModifiedSince ();

if ($CTime == -1) {
pbHTTP_LastMod ();
return;
}

if (($MS != -1)  ($CTime  $MS)) { // changed in the meantime
pbHTTP_304 ();
}
else {
pbHTTP_LastMod ($CTime);
}
}


function GetUsermode ()
{
	global $pbSessionUser;

if (isset ($pbSessionUser))
{

Re: [PHP] 304 Not Modified

2001-09-06 Thread [EMAIL PROTECTED]

The problem is that PHP has to generate the Last-Modified header, so it
can't send only the HTTP/1.0 304 not modified, that is what I want.

I think Last-Modified relies on the last modified date and time of the
file, but this is irrilevant in server-generated pages.

Thank you for your help.
 -- Initial message ---

 From: Christian Reiniger [EMAIL PROTECTED]
 To  : Ouster [EMAIL PROTECTED], [EMAIL PROTECTED]
 Cc  :
 Date: Thu, 6 Sep 2001 11:53:50 +0200
 Subject : Re: [PHP] 304 Not Modified

 On Wednesday 05 September 2001 21:22, Ouster wrote:
  I'm making a sort of cache system. I fell to this problem: I leave a
  cookie with the timestamp of the last access, and when the user
  reconnect, I compare the timestamp of the last change with the
  timestamp sent me with the cookie. Then I leave a new cookie.
 
  So:
  if($last_modified = $last_access)
  {
  header(304 Not Modified);
  exit;
  }
  else
  {
  /* code genertating the new page */
  }

 Tip: You don't even need a cookie for that. If you send a
 Last-Modified:  header along with all your pages (and perhaps even
if
 you don't), the browser will automatically add an If-Modified-
Since:
 header on each request. See the attached files for how to handle this

  Whether the If is TRUE, I get a blank page, else I see a new page
  (correct behaviour).
  Why I always get a blank page and not the cached page?

 (see other responses)

 --
 Christian Reiniger
 LGDC Webmaster (http://lgdc.sunsite.dk/)

 /* you are not expected to understand this */

 - from the UNIX V6 kernel source



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] 304 Not Modified

2001-09-06 Thread Christian Reiniger

On Thursday 06 September 2001 12:28, [EMAIL PROTECTED] wrote:
 The problem is that PHP has to generate the Last-Modified header, so it
 can't send only the HTTP/1.0 304 not modified, that is what I want.

You send the Last-Modified on the requests where you *do* send a page 
back (i.e. those not generating a 304).

 I think Last-Modified relies on the last modified date and time of the
 file, but this is irrilevant in server-generated pages.

No. You know when you last modified the page after all:

   So:
   if($last_modified = $last_access)
   {
   header(304 Not Modified);
   exit;
   }
   else
   {
   /* code genertating the new page */
   }

-- 
Christian Reiniger
LGDC Webmaster (http://lgdc.sunsite.dk/)

/* you are not expected to understand this */

- from the UNIX V6 kernel source

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




RE: [PHP] 304 Not Modified

2001-09-05 Thread Seb Frost

You don't think your missing  might have soemthing to do with it?  Or maybe
it's not that simple at all.

- seb

-Original Message-
From: Ouster [mailto:[EMAIL PROTECTED]]
Sent: 05 September 2001 20:23
To: [EMAIL PROTECTED]
Subject: [PHP] 304 Not Modified


I'm making a sort of cache system. I fell to this problem: I leave a cookie
with the timestamp of the last access, and when the user reconnect, I
compare the timestamp of the last change with the timestamp sent me with the
cookie. Then I leave a new cookie.

So:
if($last_modified = $last_access)
{
header(304 Not Modified);
exit;
}
else
{
/* code genertating the new page */
}

Whether the If is TRUE, I get a blank page, else I see a new page (correct
behaviour).
Why I always get a blank page and not the cached page?
This always happen with PWS and IIS, and sometimes with Apache.
Thanks.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.274 / Virus Database: 144 - Release Date: 23/08/2001

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.274 / Virus Database: 144 - Release Date: 23/08/2001


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] 304 Not Modified

2001-09-05 Thread Chris Hobbs

Ouster wrote:

header(304 Not Modified);


Looking at the docs (I know, silly, eh?), the correct form of this 
appears to be:

header (HTTP/1.0 304 Not Modified);

In addition to the missing  as was already pointed out...

Chris Hobbs
Silver Valley Unified School District




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]