Page 1 of 1

Can the page alias of the referring page be retrieved as a Smarty variable?

Posted: Tue May 26, 2009 4:27 pm
by jmcgin51
I can use PHP to get the URL of the referring page, like this:

("Location: ". $_SERVER['HTTP_REFERER']);

But is there any way to just retrieve the page alias of the referring page, hopefully as a Smarty variable? like: {$HTTP_REFERER_ALIAS}, which would output the page alias

I'm guessing this variable could be created in a UDT, but I don't know how to do this.

I want to do this so that I can use the referring alias in an if/then statement on a thank-you page.

{if $HTTP_REFERER_ALIAS == "page1"}
  print this thank you message
{elseif $HTTP_REFERER_ALIAS == "page2"}
  print a different thank you message
{elseif $HTTP_REFERER_ALIAS == "page3"}
  print another different thank you message
{/if}

Thanks!

Re: Can the page alias of the referring page be retrieved as a Smarty variable?

Posted: Tue May 26, 2009 9:48 pm
by jmcgin51
Ok, I built a simple UDT like this:

Code: Select all

echo $_SERVER['HTTP_REFERER'];
and called it in my page like this:
{referring_page}

and it prints the URL of the referring page, just as expected.

But I can't figure out how to get the alias of that page.  I know how to get the alias of the CURRENT page, or the current page's PARENT, or CHILDREN, or MOTHER-IN-LAW, but not the referring page.  I looked at the CGSimpleSmarty variables, and none of them seem to be quite what I want.

Re: Can the page alias of the referring page be retrieved as a Smarty variable?

Posted: Tue May 26, 2009 9:59 pm
by duclet
There is probably a better way of doing this but for a quick and easy solution, this should work. You can put this in a UDT by the way.

Code: Select all

global $gCms;

if(!isset($_SESSION['history_of_pages_or_whatever_you_want_to_call_it'])) {
    $_SESSION['history_of_pages_or_whatever_you_want_to_call_it'] = array();
}

$_SESSION['history_of_pages_or_whatever_you_want_to_call_it'][] = $gCms->smarty->get_template_vars('page_alias');

// Now echo out the previous page alias
$size = count($_SESSION['history_of_pages_or_whatever_you_want_to_call_it']); 
if($size > 1) {
    echo $_SESSION['history_of_pages_or_whatever_you_want_to_call_it'][$size - 2];
}

Re: Can the page alias of the referring page be retrieved as a Smarty variable?

Posted: Tue May 26, 2009 10:01 pm
by calguy1000
you can't.

the referrer could be an external URL... .like when somebody clicks on the site from a google search....
so essentially, you have to parse the URL and then figure out if it's an external site, or an internal one.
and if internal, extract the referring page alias, based on whatever kind of pretty url mechanism you're using.

session variables may be of use here: i.e:
a) use a UDT that analyzes the referrer
    b.1) if the referrer comes fom the same site (just another page)
            (involves parsing the refererrer URL)
            set the session variable.
      else
            make sure the session variable is unset
b) use a UDT such that if a special session variable is set... return (or assign a smarty variable with) it's value or an empty value


i.e:
    {check_referrer}
    {get_referrer_page assign='referer'}

writing UDT's something like this may get you 80% of the way there, but I can still think of times when it may screw up.  Some proxy servers, etc.  don't send the referrer data anyways.... so no matter what you do, you can't track it.

Re: Can the page alias of the referring page be retrieved as a Smarty variable?

Posted: Wed May 27, 2009 2:14 am
by jmcgin51
thanks, Calguy - that makes sense.

Reading back over my previous posts, I realize I didn't make it clear that I am only trying to get this information from pages within my CMSMS site.  I have a couple of pages which should redirect to "thank-you" pages, but need a different message depending on the referring page.  I thought it would be more efficient to have a single page, with an if/then statement as outlined earlier, rather than having two separate pages.

So, with that piece of the puzzle, is there still no way to do what I want (reliably)?

Thanks again!

Re: Can the page alias of the referring page be retrieved as a Smarty variable?

Posted: Wed May 27, 2009 2:15 am
by viebig
Here is a non session dependent solution, with an additional feature: return the page title too

EDIT: Only applies to standard content(referer cannot be news or any other module generated stuff)

Save as an UDT, put it before using the generated smarty variables and use it like:

Code: Select all

{udtname}
Now you can grab the alias  using:

Code: Select all

{$referer_alias}
and if you want the title too:

Code: Select all

{udtname title=1}
and grab the title and the alias like:

Code: Select all


You came from {$referer_title} which pages alias is: {$referer_alias}
Here is the UDT code:

Code: Select all

global $gCms;
global $config;
$db = &$gCms->db;

// Get cmsms host
$cmsms = parse_url($config['root_url']);
$cmsms_host = $cmsms['host'];

// Get the referer info
$referer = parse_url($_SERVER['HTTP_REFERER']);
$referer_host = $referer['host'];

if($referer_host === $cmsms_host)
{
    if($config['assume_mod_rewrite'] === true or $config['internal_pretty_urls'] === true)
    {
        // Lazy way to get the path elements
        foreach (explode("/",$referer['path']) as $key => $value) {
          if (trim($value) != "") {
            $elements[] = $value;
          }
        }
        
        $alias = end($elements);
        
        if($config['page_extension'] != "")
        {
            $alias = rtrim($alias, $config['page_extension']);
        }
    }
    else
    {
        $alias = ltrim($referer['query'], $config['query_var'].'=');
    } 

    if($params['title'] == 1)
    {    
        $q = "SELECT content_name FROM ".cms_db_prefix()."content WHERE content_alias='".$alias."'";
        $dbresult = $db->Execute( $q );
        if( !$dbresult )
        {
            echo '<!-- DB error: '. $db->ErrorMsg()."-->";
        }
        else
        {
            $page = $dbresult->FetchRow();
            $gCms->smarty->assign('referer_title', $page['content_name']);
        }
    }
    $gCms->smarty->assign('referer_alias', $alias);
}
Also, I accept donations  so as CMS Made Simple, but a simple applaud will do it! Enjoy, and let me know if it works how it's supposed to do.