By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

how to cache pages with login names

P: n/a
I'm fairly new to this web authoring lark, and I was pondering today
how to optimise my site for cacheing, but as far as I can see there is
no way to do it.

My site has a standard header that includes the option to either
"Login" or "Logout <username>". This header is generated by PHP
reading the session ID and looking up the corresponding username.

I guess I could cache the username in the user's session data to save
a DB lookup (or maybe even a cookie to save looking up any session
data at all). However, I still cannot see how I can ever cache the
resultant page since the header is user specific, even if everything
else is static?

How do other people handle this? Having the username at the top seems
a pretty common design, so I guess the problem must have been tackled
somehow?

Feb 18 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Since the PHP script is run for each request, you can output the name
in the session:

header.php:

<p>
You are logged in as
<?= htmlentities($_SESSION['userName'], ENT_QUOTES) ?>
</p>
On Feb 18, 4:17 pm, "lister" <listerofsme...@hotmail.comwrote:
I'm fairly new to this web authoring lark, and I was pondering today
how to optimise my site for cacheing, but as far as I can see there is
no way to do it.

My site has a standard header that includes the option to either
"Login" or "Logout <username>". This header is generated by PHP
reading the session ID and looking up the corresponding username.

I guess I could cache the username in the user's session data to save
a DB lookup (or maybe even a cookie to save looking up any session
data at all). However, I still cannot see how I can ever cache the
resultant page since the header is user specific, even if everything
else is static?

How do other people handle this? Having the username at the top seems
a pretty common design, so I guess the problem must have been tackled
somehow?

Feb 18 '07 #2

P: n/a
On Feb 18, 9:23 pm, "petersprc" <peters...@gmail.comwrote:
Since the PHP script is run for each request, you can output the name
in the session:

header.php:

<p>
You are logged in as
<?= htmlentities($_SESSION['userName'], ENT_QUOTES) ?>
</p>

On Feb 18, 4:17 pm, "lister" <listerofsme...@hotmail.comwrote:
I'm fairly new to this web authoring lark, and I was pondering today
how to optimise my site for cacheing, but as far as I can see there is
no way to do it.
My site has a standard header that includes the option to either
"Login" or "Logout <username>". This header is generated by PHP
reading the session ID and looking up the corresponding username.
I guess I could cache the username in the user's session data to save
a DB lookup (or maybe even a cookie to save looking up any session
data at all). However, I still cannot see how I can ever cache the
resultant page since the header is user specific, even if everything
else is static?
How do other people handle this? Having the username at the top seems
a pretty common design, so I guess the problem must have been tackled
somehow?- Hide quoted text -

- Show quoted text -
Yes, that's what I'm doing now (near enough).

My point was that many high hit websites cache the result of the php,
so that the script doesn't have to be run at all. I could not see a
way of doing that when user specific login details are displayed in
the header.

Feb 18 '07 #3

P: n/a
In Smarty for instance, you can have dynamic content in a cached page
using the "{insert}" tag. CakePHP has a similar <nocachetag. If
rolling your own, you could cache some content, and then run
preg_replace on it to insert your dynamic parts.

There's also PEAR Cache and Zend_Cache which are generic cache
containers, not template systems.
Yes, that's what I'm doing now (near enough).

My point was that many high hit websites cache the result of the php,
so that the script doesn't have to be run at all. I could not see a
way of doing that when user specific login details are displayed in
the header.

Feb 18 '07 #4

P: n/a
lister wrote:
I guess I could cache the username in the user's session data to save
a DB lookup (or maybe even a cookie to save looking up any session
data at all).
Personally, I tend to cache virtually *everything* about the user in
$_SESSION.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 19 '07 #5

P: n/a
lister wrote:
>
I guess I could cache the username in the user's session data to save
a DB lookup (or maybe even a cookie to save looking up any session
data at all). However, I still cannot see how I can ever cache the
resultant page since the header is user specific, even if everything
else is static?

How do other people handle this? Having the username at the top seems
a pretty common design, so I guess the problem must have been tackled
somehow?
HTTP specifically allows for this with the vary header (you'll need to send
an expires and/or a cache-control: max-age header too).

e.g.

header("Cache-control: max-age=86400; must-revalidate");
header("Vary: Cookie");

You can also cache parts of your code serverside (you should add code to
expire the cache):

$cache_key=$tmp_dir . md5($_SERVER['REQUEST_URI'] .
$_COOKIE[session_name()]);
if (file_exists($cache_key)) {
print file_get_contents($cache_key);
} else {
ob_start();
....
file_put_contents($cache_key, ob_get_contents());
ob_end_flush();
}

C.
Feb 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.