By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,350 Members | 1,311 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Making A History Page For BIG Sites

P: 161
Making a History Page for BIG Sites

Ok, let's say after a while your website has grown massive. We're talking search engine, forum and video hosting -- you've got a LOT of content. And you are wondering, "Why do I need yet another feature for my big site?"

Well, some people can become forgetful every time they see content on your site, and let's suppose that one day they needed to work on a good php script for their class and they remember seeing your tutorial... but forgot the name and url. Wouldn't it be neat if they could go back to your site, hit the "History" link and see what content they looked at in the past week (or month or year or...).?

Of course, the browser keeps track of your history, too, but browser histories are very disorganized, and sometimes it takes longer to find the page in your history than it does just to go back to the site and click around randomly until you find what you were looking for!

Before I start posting codes and how they work I will give you a list of PHP functions we will be using. I have included links to for each function so that you can read about anything that you don't understand.

So if you don't know any of these functions or words please click the links!

echo - Outputting text / HTML
setcookie - Setting a Cookie value
foreach - Iterating through an array
if - Conditional operator
ksort - Sorting an array by key

What You Need
1. A sever with PHP installed and properly configured.
2. A website with some pages in php that you want your site to record.
3. A page named "history.php" in your web server.
4. A brain that has an intermediate level of PHP proficiency.

Getting Started:

Edit one of the pages on your site and add this to the top of your PHP code:
Expand|Select|Wrap|Line Numbers
  1.     setcookie("The Title of the Page", "", time()+604800);
Use the title of the page as the name of the cookie, and the URL of the page as the cookie's value.

Incidentally, 'time() + 604800' will set your cookie to expire in exactly one week.

Now, every time the User visits this page, his browser will set a cookie that will
keep track of the fact that he has visited the page.

Add similar code to the top of the other pages on your site.

Writing the Code:

Create history.php:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>History</title>
  4.     </head>
  6.     <body>
  7.         <ul>
  8. <?php
  9.             // We'll put stuff here in a second.
  10. ?>
  11.         </ul>
  12.     </body>
  13. </html>
So in the php tags enter this:

Expand|Select|Wrap|Line Numbers
  1. $page = $_COOKIE;
PHP retrieves all the cookies from our site and converts them into a array, which we are then assigning to a variable named $page.

To extract the values and echo them out sorted add this next:
Expand|Select|Wrap|Line Numbers
  1. ksort($page);
  2. foreach ($page as $key => $val) 
  3. {
  5. }
foreach will run through $page and assign each key (the name of the page) to $key and each value (the URL of the pge) to $val.

Now, there's one caveat. See, our script isn't the only entity that uses cookies. For example, PHP adds a PHPSESSID cookie (the exact name is configurable in php.ini) so that it can identify each computer. So we have to tell PHP to skip the PHPSESSID cookie (also known as the "session cookie").

To skip the session cookie, put this code inside of the foreach loop.
Expand|Select|Wrap|Line Numbers
  1. if ($key != "PHPSESSID")
  2. {
  3. }
Your site may use other cookies that don't relate to the User's history. You can add them to the if conditional to exclude them, similarly to the way you exclude the session cookie.

For example, if one of your scripts uses a cookie named 'zipCode', you can exclude it from appearing on the history page by using:

Expand|Select|Wrap|Line Numbers
  1. if ($key != "PHPSESSID" && $key != "zipCode")
  2. {
  3. }
Now it's time to output the history links.

Inside of your if block, add this code:
Expand|Select|Wrap|Line Numbers
  1. echo "<li><a href=\"$val\" title=\"$key\">$key</a></li>";
Here we are getting the title of the page and url and putting them into a link!

Finished Product:
This is how the history page should look:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>History</title>
  4.     </head>
  6.     <body>
  7.             <ul>
  8. <?php
  9.                 $page = $_COOKIE;
  11.                 ksort($page);
  12.                 foreach ($page as $key => $val) 
  13.                 {
  14.                     if ($key != "PHPSESSID")
  15.                     {
  16.                         echo "<li><a href=\"$val\" title=\"$key\">$key</a></li>";
  17.                     }
  18.                 }
  19. ?>
  20.             </ul>
  21.     </body>
  22. </html>
Here's a working example from my site:
Working Example

Thanks for your time,
Sep 23 '07 #1
Share this Article
Share on Google+

Expert 5K+
P: 5,821
Very nice article, AJM. Keep up the good work!
Sep 23 '07 #2

P: 161
Thanks for your reply and cleaning the article up a bit. :)
Sep 23 '07 #3

Expert 100+
P: 1,044
That is a very interesting idea. I think I may have to one-up you. :P
Sep 24 '07 #4

Expert 5K+
P: 5,821
Homework Assignment, due Friday, September 28:
Re-engineer the code so that it only uses *one* cookie.

Hint: Cookies cannot hold arrays, but if you were to, say, convert the array to a string (a process also known as 'serialization'), you might be able to make it work....
Sep 24 '07 #5

P: 161
So you mean, by one cookie is to cramp every piece of data in it.

Or do you mean something like having one name and then giving the cookies multi able values so when the history page loads. Then it will get each page/site listed in the if statments. So if I had a name of page and the value of 45 it will then make the string value to the website and url.
Sep 25 '07 #6

Expert 5K+
P: 5,821
Heya, AJM.

Your words inspired me, and I did some research. I discovered the following in the PHP Manual:

If you wish to assign multiple values to a single cookie, just add [] to the cookie name.


Your solution works, but the problem is (for example in the case of the session cookie) you have to specifically exclude every non-history-related item.

My challenge to you is to find a way to isolate your history cookies from the other cookies that the site uses.
Sep 25 '07 #7

P: 161
Here is one way, but it mite result into longer loading time. I didn't test it yet, but let me know if it works.

Place in a random page: Just change the value in the cookie to your page's address.
Expand|Select|Wrap|Line Numbers
  1. setcookie("Page",  "", time()+3600); //One Hour

Place In History.php
Expand|Select|Wrap|Line Numbers
  1. $page = $_COOKIE["Page"];
  3.                 ksort($page);
  4.                 foreach ($page as $key => $val)
  5. {
  7.               $fp = fopen( $val, 'r' );
  9.     $content = "";
  12.     while( !feof( $fp ) ) {
  14.        $buffer = trim( fgets( $fp, 4096 ) );
  15.        $content .= $buffer;
  17.     }
  19.     $start = '<title>';
  20.     $end = '<\/title>';
  22.     preg_match( "/$start(.*)$end/s", $content, $match );
  23.     $title = $match[ 1 ]; 
  25.                         echo "<li><a href=\"$val\" title=\"$title\">$title</a></li>";
  27.       }
This code will get all the cookies named "Page" and get the value which is the url to that page. Then when it sorts out the value in the foreach function it will read the url and try to get the title tag and place it on the url.
Sep 26 '07 #8

P: 1
I like the discussion here ... this is very similar to Google feature which allows you to get previous search results. However, this would not be of any benefit to me as a user. The reason is that I only allow for cookies in managing sessions. Every time I close the Firefox browser, my cookies are deleted. So if I wanted to come back a couple days latter and get it. I would have difficulty.

I imagine that this process could be fixed by using a database to store the history in almost the exact same way. Instead of having 1 line in each page there would be maybe ... two lines:

#include historyPages.php

historyPages.php holds the function updateHistoryPages() and inserts in to sql information about the user and the page.

One draw back to this is that it does not work for drive-by users ... but I guess (maybe mistakenly) that drive-by users aren't interested in their history. However, being that I am a user that cleans cookies at end of each session ... I get incredibly frustrated with sites I visit on a regular basis that are too dependent on cookies ... I have registered at your site ... take the time to remember my settings on your database.
Nov 6 '07 #9

P: 161
Thanks for your reply, sorry if I posted late. I want to keep this post short since I am at school right now.

I see where your going at, like a shopping cart you mean isn't it?

I'll try to play around with that when I can, I am just jumping in and out of projects so I'll, put on how to use mysql for this when I can.

Thanks, Ajm.
Nov 19 '07 #10

P: 1
very nice article..

I like it very much
Jan 7 '08 #11