_SESSION fun #1: Visit History   Leave a comment

Practically SESSIONS have infinite serious uses. But, being serious is one part, being ‘interesting’ is more serious than that. Usually people won’t agree to include thee types of weird sections in their pages, since your browser will always decolorize the visited links! I thought it might get user attraction and they will pay attention to see their progress in finishing up reading web site contents. Anyway this is fun and those who have just started understanding SESSIONS; this will be a good part to play with … hopes they are also not in the lat minute to production!

The code does this:

  •  Tracks the current and past posts
  • Displays a list in the last visited order

Funny part, it will never show if you ever been here before, simply removes the current page from visited pages list, so visited will never know that they have been her once!

single.php


<?php
// Place this in the single.php

 get_header();
$visited_pages = $_SESSION['vpages'];
 $last_visited_page = $_SESSION['lpage'];

 while ( have_posts() ) : the_post();
   $the_title = get_the_title();
	echo "<h1>".$the_title."</h1>";
	echo "<h2>".get_the_excerpt()."</h2>";
	the_content();

	 if($last_visited_page != $the_title){
    	  if($visited_pages):
     	    $already_pages = explode("~", $visited_pages);
     	    $visited_pages = $last_visited_page;
     	    foreach ($already_pages as $item){
        	    if($the_title != $item && $last_visited_page != $item) :
        	      $visited_pages .= "~".$item;
        	    endif;
    	    }
    	 else:
    	   $visited_pages = $last_visited_page;
    	 endif;
endwhile;	 }
$_SESSION['vpages']= $visited_pages;
$_SESSION['lpage'] = $last_visited_page;

if($visited_pages)
  echo "<BR>Visited Pages:<ol> ". preg_replace("/([^~]+)~*/", "<li>$1</li>",$visited_pages) . "</ol>";

get_footer();
?>

Like always I think if you like this one, try to build one pie chart that shows how many posts are read and how many are left! You can use the Google Chart API to build the graph.

Advertisements

Posted February 4, 2012 by jsusmer in Lesson, mind waves

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: