473,434 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,434 developers and data experts.

Making A History Page For BIG Sites

Ajm113
161 100+
Making a History Page for BIG Sites

Intro:
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 php.net 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", "http://www.yourdomain.com/path/to/thepage.php", time()+604800);
  2.  
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>
  5.  
  6.     <body>
  7.         <ul>
  8. <?php
  9.             // We'll put stuff here in a second.
  10. ?>
  11.         </ul>
  12.     </body>
  13. </html>
  14.  
So in the php tags enter this:

Expand|Select|Wrap|Line Numbers
  1. $page = $_COOKIE;
  2.  
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. {
  4.  
  5. }
  6.  
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. }
  4.  
Note:
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. }
  4.  
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>";
  2.  
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>
  5.  
  6.     <body>
  7.             <ul>
  8. <?php
  9.                 $page = $_COOKIE;
  10.  
  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>
  23.  
Here's a working example from my site:
Working Example

Thanks for your time,
Ajm113.
Sep 23 '07 #1
10 10341
pbmods
5,821 Expert 4TB
Very nice article, AJM. Keep up the good work!
Sep 23 '07 #2
Ajm113
161 100+
Thanks for your reply and cleaning the article up a bit. :)
Sep 23 '07 #3
kovik
1,044 Expert 1GB
That is a very interesting idea. I think I may have to one-up you. :P
Sep 24 '07 #4
pbmods
5,821 Expert 4TB
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
Ajm113
161 100+
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
pbmods
5,821 Expert 4TB
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.

Interesting....

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
Ajm113
161 100+
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",  "http://www.domain123.com/page.php", time()+3600); //One Hour

Place In History.php
Expand|Select|Wrap|Line Numbers
  1. $page = $_COOKIE["Page"];
  2.  
  3.                 ksort($page);
  4.                 foreach ($page as $key => $val)
  5. {
  6.  
  7.               $fp = fopen( $val, 'r' );
  8.  
  9.     $content = "";
  10.  
  11.  
  12.     while( !feof( $fp ) ) {
  13.  
  14.        $buffer = trim( fgets( $fp, 4096 ) );
  15.        $content .= $buffer;
  16.  
  17.     }
  18.  
  19.     $start = '<title>';
  20.     $end = '<\/title>';
  21.  
  22.     preg_match( "/$start(.*)$end/s", $content, $match );
  23.     $title = $match[ 1 ]; 
  24.  
  25.                         echo "<li><a href=\"$val\" title=\"$title\">$title</a></li>";
  26.  
  27.       }
  28.  
  29.  
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
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
updateHistoryPages();

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
Ajm113
161 100+
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
sandia
1
very nice article..

I like it very much
Jan 7 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jerry McEwen | last post by:
I'm working on a web site for a domestic violence group and these groups always have an "escape" button, which is simply a link to another site. Whether I open this link in the same window, a new...
2
by: Jenn | last post by:
Hi, I have a page that I am setting up at http://www.extravaganzadesign.com/aw/frame.htm I am using a frame on top for some navigation and the frame below for content that will go to external...
12
by: |-|erc | last post by:
when a user clicks back to get to my site, I want it to run a javascript function. can you detect when the FORWARD button is greyed out? Herc -- I call3d this fugly and I'm proud...
20
by: Dan | last post by:
Is there a way to obtain the last page visited? I don't want to go to that page, I just want to be able find out what page they came from, the url of that page. Is this possible?
8
by: Ed Jay | last post by:
I want to use history.go() to navigate between my previously loaded pages. I'm looking for a way to trigger a function call when a page is accessed using history.go(). Is there an event generated?...
2
by: Noodle | last post by:
Hi All, I know that document.referrer can be used to get the last page the user visited, but how can I get the page visited before that? Can this be done in JS? TIA
3
by: pentisia | last post by:
Hi there, We are using history.go(integer) to go back to the certain page directly. Because there are some pages interaction in between. For example, starting from page 1 to page 2. From page...
1
by: George | last post by:
Dear All, I am trying to write a simple script that prints out all the URLs in the history of the current window. I wrote this code: <html> <head> <title>Example Page</title> <script...
12
by: jim.richardson | last post by:
Hi all, I'd like a page to be excluded from the back button history, that is, when a user hits their browser's back button, it never backs into this particular page. Can anybody please tell...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
1
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.