PDA

View Full Version : Clear history of visited links on web pages using cookies


mypointofview
09-19-2006, 07:54 AM
Usually the preferences of a browser can be set to memorize visited links for some time. But how can I override this using PHP?

On my site the visitor clicks on a link and the link changes its color.

But I'd like to make it happen that the visitor will have this effect ONLY during one session. So, that the next day all will look "nice and fresh" again.

I've googled around but cannot find the answer to this. I like the usability of the visited link looking different to an unvisited link but I'd also like to have my site look good the next day. I think the site would look "stale" or "dirty" if the visited links would reappear in that darker color.

Does anybody know a solution to this, perhaps by setting a cookie?

Thank you,


Martin

raf
09-19-2006, 12:01 PM
you should not mess with your visitors history. you should accept how the user configures his browser, including how long he want's to keep pages in history. some users will rely on links from previous visits being highlighted, and might even think there is something wrong with their browser if you start messing with this browser-feature

if you want to implement something like that, then you best just create another class for visited links (like class="justvisited"), and then you store all visited pages their addresses in an array inside your session-collection. you then use output-buffering and use an ob_get_clean() at the end f your script. All that's left then is replace all visited links like

$output = ob_get_clean();
foreach($_SESSION['arr_visited_links'] as $link){
//$_SESSION['arr_visited_links'] contains elements like 'href="test.php"'
$output = str_replace($link, 'class="justvisited" '. $link, $output);
}

there will probably be a fancier way to do the replaces but this should work fine.

mypointofview
09-19-2006, 12:36 PM
Wow, thank you. Unfortunately I don't understand it very well yet because I'm not that advanced.

I'm sure I'll "get it" one day though ;-)


Martin

firepages
09-20-2006, 01:37 AM
try7 adding a random number to the link...


<a href="http://blah.com/index.htm?r=<?=rand(1,100);?>">link</a>


that wont work if the user hits the back button, otherwise should work.

raf
09-20-2006, 07:51 AM
hmm, in my opinion, it's a terrible hack that was introduced to prevent caching pages... we used to add the time() to the querystring for that purpose.

but i don't see how it would help here...
because, say the user comes on the homepage and there is a link to
http://blah.com/index.htm?r=10
he goes to that page, where he fils in a form, form is processed and he is redirected to the homepage where the link now is http://blah.com/index.htm?r=958
Will this link be shown as visited?

for this to work, you should add a sessionspecific value to the querystring (like the first 10 characters of the sessionID). like

echo '<a href="http://blah.com/index.htm?r=', substr(session_id(), 0, 10) ,'">link</a>'

and to implement this in an efficient manner, you still need to do the outputbuffering and then replace the links, so i'd still recommend my original sollution since it offers more flexability.

firepages
09-20-2006, 02:57 PM
Will this link be shown as visited?


err no ... I thought that was the point ?

The simplest method is to use CSS to style A.visited the same as A.link but the quick hack is errr quick and I dont see where output buffereing needs to come into it?

raf
09-20-2006, 04:00 PM
Usually the preferences of a browser can be set to memorize visited links for some time. But how can I override this using PHP?

On my site the visitor clicks on a link and the link changes its color.

But I'd like to make it happen that the visitor will have this effect ONLY during one session. So, that the next day all will look "nice and fresh" again.
he's trying to achieve the standard appearence you get with previously visited links, but only the links he followed in the present visit.

so in the example i posted, the link http://blah.com/index.htm?r=958 should be highlighted as visited. if he would visit that same page the next day, that link should not be highlighted.

so you need to be able to identify the links that were followed in the current visit, by storing them somewhere (array in a sessionvariable is the obvious choice) or by adding a sessionspecific identifier to each link (like the substr(session_id(), 0, 10) )

the output buffering comes into the frame because both methods require that you add a class or querystringvariable to the link-tag, so that you can highlight links from that class through css.
by buffering the output, and then processing it at the end of the script like in my first post, you can add this class/querystringvariable to the link-tag.

rafiki
09-20-2006, 07:16 PM
use an image of text as a link, no way to change colour after a click, but wont do anything when mouse over unless you use Jscript,

firepages
09-21-2006, 04:39 PM
he's trying to achieve the standard appearence you get with previously visited links, but only the links he followed in the present visit.


DOH, guess I should have read that bit, now I understand where you were going ... firepages# shutdown -now ;)

raf
09-22-2006, 07:49 AM
DOH, guess I should have read that bit, now I understand where you were going ... firepages# shutdown -now ;)no problem. now at least one of us knows where i'm heading for --> are you still with us, mypointofview?

mypointofview
09-24-2006, 12:20 AM
Yes, I wanted to have it look "visited" only for a day or only for a session. It's basically a practical thing to see your own "footsteps". The designer in me argues differently though. My navigation is a long column of links that turn darker when visited which would look really "lame" when somebody had checked out all of them and then comes back the next day...

Now I've come accross a JavaScript solution (sorry - I know it's the PHP board.. so I'll start a new post at the JavaScript section).

See what I've come up with here (http://uk.geocities.com/galery003/htmlforum/visitedlinks/14.html) -- but I have still problems...

The new thread which I started is in the JavaScript (http://www.codingforums.com/showthread.php?p=490267#post490267) department on CodingForums.

Still I'd think that PHP would be more elegant. I'm just starting to understand the technique with cookies and so on.

raf if you have a moment, could you post a full code example that I could try out? If that would work then I would find that a lot of designers would implement it. I've read so much, including blogs and List Apart and so on and there's this divided opinion about visited links (usability vs. design) so that such a compromise (have visited links only for a SHORT time) would be widely appreciated I think.

raf
09-25-2006, 09:01 AM
raf if you have a moment, could you post a full code example that I could try out? If that would work then I would find that a lot of designers would implement it. I've read so much, including blogs and List Apart and so on and there's this divided opinion about visited links (usability vs. design) so that such a compromise (have visited links only for a SHORT time) would be widely appreciated I think.
i'll see if i can find some time --> i'm not promissing anything, because i find that anyone with some php knowledge can expand a bit on the code that's posted here to implement this...
still, i'll see if i can find some time to post the code or some small tut about it here.