Advanced “New” Labels
Once our handler has retrieved the user's ID from a cookie and established a connection with the database, we can store and retrieve session information at our convenience.
We can store information about this user in %session, the hash to which we tied Apache::Session. Each time our handler is invoked, we can retrieve information about this user based on his or her ID. For example, we can store a value with:
$session{"foo"} = "bar";
We can then retrieve that value in a later session with:
my $stuff = $session{"foo"};
While our program appears to be storing and retrieving values in
%session, it is actually retrieving them from
the database using DBI—which means that, so long as we ensure each
user has a unique ID, we can keep everyone's values separate.
Since we have what amounts to a hash that extends across sessions, how can we store information on which URLs we have visited and when? The easiest way is to use the URL as a key into %session, then store the last time the user visited the site. For example, we can store the URL with the following code:
my $document_uri = $r->uri;
$session{$document_uri} = time;
We want to retrieve this information when determining whether a user has recently visited a particular link. In order to do that, we will modify label_url so that it expects a fourth argument, a reference to %session. This way, label_url will be able to retrieve session information about the URL in question. We create the reference by preceding %session with a backslash (\%session) before passing it to label_url. We then dereference the copy of %session as follows, at the beginning of label_url:
my $session = shift;
my %session = %{$session};
The full code of a working version of Apache::TagNew, including the
label_url subroutine, is in Listing 3.
The rest of label_url is largely the same, except for a portion in the middle where we test to see if the URL begins with a slash (/). We must be sure to store and retrieve the same key from %session; otherwise, we will get false readings regarding when we last visited the URL. Since we store the URL based on $r→uri, which always begins with a slash and is relative to our server's root URL directory, we should retrieve the URLs in the same way.
We do this by getting the current URL and removing everything following the final slash:
$current_directory =~ s|^(\S+/)[\w.]+$|$1|;
What is left is indeed the current directory, to which we can prepend the URL:
$url = $current_directory . $url;Now we can retrieve the session information about that URL, confident we are using the same set of keys for retrieval as we did earlier for storage. We retrieve session information about when we last viewed the file in question, turning it into a number of days relative to right now:
my $last_time = (time - $session{$url}) / 86400;
Then we retrieve the modification timestamp of this file, by
prepending
$r→document_root (the
full path name leading to each file on the web site, normally
invisible to users) to the file. We can easily determine its
modification date:
my $full_filename = $r->document_root . $url; my $ctime = -M $full_filename;Finally, we compare $ctime (the number of days since the file was modified) with $last_time (the number of days since the user last saw the file). If the former is smaller than the latter, we add the label:
if ($ctime < $last_time)
{
$label = "<font color=\"red\">New!</font>"";
}
This module seems to do a good job of labeling new documents on a
user-by-user basis. As long as users enable cookies, they should be
able to get an accurate reading of which files they have not seen
in a long time.
For a medium that is supposed to adapt itself to our own needs, the Web is surprisingly primitive—for instance, in the way “new” documents are labeled on web sites. This month, we have seen how mod_perl allows us to personalize our site a bit more, showing people what is actually new from their perspective, rather than from the webmaster's perspective. I hope you also noticed how advanced some of these tools have become; with a little more than 100 lines of Perl code, we were able to make a substantial change to our web server that had little impact on performance, but provided great benefit to our users.

Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- RSS Feeds
- New Products
- Trying to Tame the Tablet
- What's the tweeting protocol?
- Dart: a New Web Programming Experience
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




3 hours 43 min ago
6 hours 5 min ago
22 hours 54 min ago
1 day 1 hour ago
1 day 2 hours ago
1 day 3 hours ago
1 day 3 hours ago
1 day 8 hours ago
1 day 9 hours ago
1 day 10 hours ago