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

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

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

Post 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!
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

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

Post 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.
User avatar
duclet
Forum Members
Forum Members
Posts: 187
Joined: Fri Jun 23, 2006 12:55 pm

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

Post 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];
}
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

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

Post 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.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

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

Post 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!
viebig

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

Post 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.
Last edited by viebig on Wed May 27, 2009 2:27 am, edited 1 time in total.
Post Reply

Return to “CMSMS Core”