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))
        {
                if ($pbSessionUser->CheckPerm (PAGETYPE, 'maintainer')) {
                        return 'maint';
                }
                elseif ($pbSessionUser->IsRealUser ()) {
                        return 'logged_in';
                }
        }

        return 'std';
}


function Postprocess ($Page)
{
        include ('ui/postprocess_page.php');
        return PostprocessPage ($Page, GetUsermode ());
}


function Output ($Page)
{
        global $HTTP_SERVER_VARS;

        if (strstr($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], 'gzip')) {
                if (function_exists ('gzencode')) {
                        $Page = gzencode ($Page);
                        header("Content-Encoding: gzip");
                }
        }

        header ("Content-Length: " . strlen ($Page));
        print ($Page);
}



?>
-- 
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]

Reply via email to