473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to cache pages with login names

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
5 2694
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Just D. | last post by:
All, Did anybody see this strange effect? The web application is written in C#, ASP.NET, SQL, T-SQL, etc. A pretty usual stuff, complicated enough, but works fine until... Here is a question....
0
by: WebBuilder451 | last post by:
I've created a sub that will logoff a current user if another login occures with the same UserID. when a login occures the host name of the login is saved in the application cache: if "is a valid...
14
by: multiformity | last post by:
So I have been working on an opensource project for a while, and decided to really try to make it look better after focusing on the functionality most of this time. Up to now, I have simply used a...
18
by: siddharthkhare | last post by:
Hi All, what is the diference between these two cache control header. no-cache and no-store. I have read the w3.org explanation. So lets say I am using only no-cache ....my understanding is...
11
by: EagleRed | last post by:
I am writing an ASP.NET 2.0 application that uses master pages. I have some pages that must not be cached on the client. In ASP.NET 1.1 I achieved this using metatags: <meta...
4
by: three-eight-hotel | last post by:
I'm somewhat of a newbie to PHP coding, but have developed a site using the technology, and have been pleasantly surprised by the capabilities offered. I am more comfortable in the ASP world,...
3
by: momogi | last post by:
Response.Cache.SetNoStore(); I have read in the .Net Forum, that there's a way to tell the browser not to cache the previous pages. I wanna know if PHP have the same way to do this: ...
5
by: =?Utf-8?B?Y2hlY2tyYWlzZXJAY29tbXVuaXR5Lm5vc3BhbQ== | last post by:
I have a site which I secure with forms authentication. When the user's sign on and hit one of the secure pages, I have this line in my code to ensure that the browser does not cache the page;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.