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
- setcookie("The Title of the Page", "http://www.yourdomain.com/path/to/thepage.php", time()+604800);
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
- <html>
- <head>
- <title>History</title>
- </head>
- <body>
- <ul>
- <?php
- // We'll put stuff here in a second.
- ?>
- </ul>
- </body>
- </html>
Expand|Select|Wrap|Line Numbers
- $page = $_COOKIE;
To extract the values and echo them out sorted add this next:
Expand|Select|Wrap|Line Numbers
- ksort($page);
- foreach ($page as $key => $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
- if ($key != "PHPSESSID")
- {
- }
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
- if ($key != "PHPSESSID" && $key != "zipCode")
- {
- }
Inside of your if block, add this code:
Expand|Select|Wrap|Line Numbers
- echo "<li><a href=\"$val\" title=\"$key\">$key</a></li>";
Finished Product:
This is how the history page should look:
Expand|Select|Wrap|Line Numbers
- <html>
- <head>
- <title>History</title>
- </head>
- <body>
- <ul>
- <?php
- $page = $_COOKIE;
- ksort($page);
- foreach ($page as $key => $val)
- {
- if ($key != "PHPSESSID")
- {
- echo "<li><a href=\"$val\" title=\"$key\">$key</a></li>";
- }
- }
- ?>
- </ul>
- </body>
- </html>
Working Example
Thanks for your time,
Ajm113.