How do I know from which page the users are coming? I'm looking for a way to determine where the users are coming from to build the breadcrumb for a page.

Is there something I can do with hook_breadcrumb_alter(&amp;$active_trail, $item), or should I use the Flag module or any activity tracker module?
If you have control over the Urls linking to your target page, append a page-specific parameter. Suppose the page you are linking to is http://www.example.com/target-page:


On the first referring page, make this Url: http://www.example.com/target-page?referer=page-1
On the second referring page, make this Url: http://www.example.com/target-page?referer=page-2


Then in your hook_menu_breadcrumb_alter function, the referer will be available in the $_GET variable, and you will be able to do something like this in the template.php file for your theme:

function mytheme_breadcrumb(&amp;$variables) {

  if (request_uri() == '/target-page') {
    switch($_GET['referer']) {
      case 'page-1':
        $variables['breadcrumb'][] = l('First referring page', '/page-1');
        break;
      case 'page-2':
        $variables['breadcrumb'][] = l('Second referring page', '/page-2');
        break;
    }
  }
}


If you don't want to add parameters to the Url, and are not using HTTPS, you could use the approach above except with $_SERVER['HTTP_REFERER'] in the switch statement. Or alternatively, you could set a cookie on the referring pages and read it on the target page. 

The example above is probably the easiest way to manipulate breadcrumbs but strictly speaking, there should not be business logic in a theme function. See these questions for more detailed info on how to manipulate breadcrumbs:


Altering the breadcrumb contents
Move user profiles under a menu item
https://stackoverflow.com/questions/10087168/how-to-add-breadcrumb