We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Custom plugin to capture and store variable from referring URL

Development and Programming

radar77's avatar
radar77
28 posts
17 years ago
radar77's avatar radar77

Hello All,

I need some assistance in creating a plugin from some code that I need to be portable across my entire EE site. Here is a basic synopsis of what I am trying to accomplish with a simple plugin:

  1. My EE site is located at subdomain.mydomain.com
  2. The main site, mydomain.com, is an ASP.NET site which utilizes a variable called “web_alias” in its URL structure.
  3. When users travel from mydomain.com to subdomain.mydomain.com, I need to grab and store the “web_alias” variable for use in static header links to be included in all EE templates.
  4. I only want to grab and store the “web_alias” the first time they come to the EE site and reuse for all pages served at the EE site.

Referring URL’s coming from mydomain.com to the EE site subdomain.mydomain.com would be in the following format:

http://mydomain.com/page.aspx?ID=web_alias

The links out of the EE site back to mydomain.com would be something like:

<a >cache['radar']['web_alias']; ?>">Link back to mydomain.com</a>

Below is the code that I am working with so far (with much help from Derek Jones). Any help on converting this to a usable plugin would be greatly appreciated.

<?php

global $SESS;
global $IN;

//vars
$SESS->cache['radar']['web_alias']="";
$SESS->cache['radar']['fs_refer']="";
$SESS->cache['radar']['host_refer']="";

//Find out where the user came from
$SESS->cache['radar']['fs_refer'] = $IN->GBL('HTTP_REFERER', 'SERVER');

if (($SESS->cache['radar']['fs_refer'] == false) OR ($SESS->cache['radar']['fs_refer'] == "")){

    $SESS->cache['radar']['web_alias'] = "grace";
    
} else {

    $SESS->cache['radar']['host_refer'] = parse_url($SESS->cache['radar']['fs_refer']);
    $SESS->cache['radar']['host_refer'] = $SESS->cache['radar']['host_refer']['host'];
    $SESS->cache['radar']['fs_refer'] = explode("=", $SESS->cache['cleure']['fs_refer']);

       //Set the WebAlis for outbound links back to mydomain.com
       if (($host_refer == "www.mydomain.com") OR ($fs_refer == "mydomain.com")) {

         $SESS->cache['radar']['web_alias']  = $SESS->cache['radar']['fs_refer'][1];

        } else {

         $SESS->cache['radar']['web_alias']  = "grace";

        }


}
?>

Cheers, Michael R.

       
Jamie Poitra's avatar
Jamie Poitra
409 posts
17 years ago
Jamie Poitra's avatar Jamie Poitra

Michael,

You are 99% of the way there. Have you read the plugin docs?

Looking at what you have it looks like you really just need to take your code and drop it into the Plugin structure.

Jamie

       
radar77's avatar
radar77
28 posts
17 years ago
radar77's avatar radar77

@Jamie I have read through the Plugin API docs and will take a crack at just dropping it in.

My main question is more of a functional one. Does the plugin code execute everytime a page is called in EE? I want to grab the referrer only the first time and set a variable based on that which is then stored and reused throughout the EE site.

       
Jamie Poitra's avatar
Jamie Poitra
409 posts
17 years ago
Jamie Poitra's avatar Jamie Poitra

Yes it is run EVERY TIME the template is hit. A simple conditional to check if it is already available should make that fact a negligible issue.

Jamie

       
radar77's avatar
radar77
28 posts
17 years ago
radar77's avatar radar77

@Jamie

Ok, so I have the plugin functioning, sort of… The plugin successfuly grabs and stores the web_alias variable in $SESS->cache[‘radar’][‘web_alias’]. However, on subsequent page calls, $SESS->cache[‘radar’][‘web_alias’] gets reset to the default value of “grace.” I’m trying to use !isset to check if the variable already exists or not because I only want to set the variable once and reuse it on subsequent page calls. Am I incorrectly checking the $SESS variable? Below is the code.

<?php

$plugin_info = array(
                        'pi_name'            => 'Radar',
                        'pi_version'        => '0.1',
                        'pi_author'            => 'Michael Reiner',
                        'pi_author_url'    => 'http://mydomain.com/',
                        'pi_description'    => 'Captures and stores WebAlias',
                        'pi_usage'            => Radar::usage()
                    );


class Radar
{

    var $return_data;
    var $web_alias;
    var $fs_refer;
    var $host_refer;
    
    /** ----------------------------------------
    /**  radar WebAlias
    /** ----------------------------------------*/

   function Web_alias()
   {
    global $SESS;
    global $IN;
      
        if (!isset($SESS->cache['radar']['web_alias']))
        {  
          
            //$SESS->cache['radar']['web_alias'] = "grace";   
            $fs_refer = $IN->GBL('HTTP_REFERER', 'SERVER');
        
            if ($fs_refer != "")
            {    
                $host_refer = parse_url($fs_refer);
                $host_refer = $host_refer['host'];
                $fs_refer = explode("=", $fs_refer);
            
                if (($host_refer == "www.mydomain.com") OR ($host_refer == "mydomain.com"))
                {    
                    $web_alias = $fs_refer[1];
                    $SESS->cache['radar']['web_alias'] = $web_alias;
                    
                } else 
                {
                    $web_alias = "grace";
                    $SESS->cache['radar']['web_alias'] = $web_alias;
                }
            
            } else {
                $web_alias = "grace";
                $SESS->cache['radar']['web_alias'] = $web_alias;
            }
            
      }
                  
        return $SESS->cache['radar']['web_alias'];
        
    }

     
/* END */
    
// ----------------------------------------
//  Plugin Usage
// ----------------------------------------

// This function describes how the plugin is used.
//  Make sure and use output buffering

function usage()
{
ob_start(); 
?>
Captures and stores the WebAlias.

{exp:radar:web_alias}  

<?php
$buffer = ob_get_contents();
    
ob_end_clean(); 

return $buffer;
}
/* END */


}
// END CLASS
?>
       
Jamie Poitra's avatar
Jamie Poitra
409 posts
17 years ago
Jamie Poitra's avatar Jamie Poitra

Michael,

I’m taking a look at right now. There are a few things I’m slightly unsure about regarding what you did.

First I’m not real sure why you did this:

$host_refer = parse_url($fs_refer);
$host_refer = $host_refer['host'];
$fs_refer = explode("=", $fs_refer);

From what I can tell you want to get the ID value right?

Why not just do:

$IN->GBL('ID');

A lot less work and at least on my server doing what you were doing before doesn’t work at all. I just get the domain in an array not the ID value I want.

Further looking at it I’m not sure what you want is the $SESS->cache which is I believe page specific. It gets cleared out after the page is finished rendering. What you want is a cookie I think.

Maybe something like this:

<?php

$plugin_info = array(
    'pi_name'            => 'Radar',
    'pi_version'        => '0.1',
    'pi_author'            => 'Michael Reiner',
    'pi_author_url'    => 'http://mydomain.com/',
'pi_description'    => 'Captures and stores WebAlias',
    'pi_usage'            => Radar::usage()
    );


class Radar
{

    var $return_data;
    var $web_alias;
    var $fs_refer;
    var $host_refer;

    /** ----------------------------------------
    /**  radar WebAlias
    /** ----------------------------------------*/

    function Web_alias()
    {
        global $IN, $FNS;
        
        $seconds = 60;
        
        if ($IN->GBL('web_alias', 'COOKIE') == '')
        {
            $web_alias = ($IN->GBL('ID') != '') ? $IN->GBL('ID') : 'grace';
            $FNS->set_cookie('web_alias', $web_alias, $seconds);
            return $web_alias;
        }
        else
        {
            return $IN->GBL('web_alias', 'COOKIE');
        }
    }
    /* END */

    // ----------------------------------------
    //  Plugin Usage
    // ----------------------------------------

    // This function describes how the plugin is used.
    //  Make sure and use output buffering

    function usage()
    {
            
        ob_start(); 
?>
Captures and stores the WebAlias.

{exp:radar:web_alias}  

<?php
        $buffer = ob_get_contents();

        ob_end_clean(); 

        return $buffer;
    }
    /* END */
}
// END CLASS
?>

Jamie

       
radar77's avatar
radar77
28 posts
17 years ago
radar77's avatar radar77

Hi Jamie,

Thanks for the input. I was under the impression that the $SESS variables were stored in the cache and not refreshed after each page rendering, but perhaps that is not good information. If in fact it does get refreshed after each page rendering, than indeed I’ll want to use a COOKIE. I don’t have much experience with cookies but will read up on them.

I tried using your code with some modifications with no success. I wasn’t able to store the web alias, but more importantly I was getting header errors and not sure why.

I’ll play more with the COOKIE idea and see what progress I can make and post again here.

Thanks for your help thus far.

       
Jamie Poitra's avatar
Jamie Poitra
409 posts
17 years ago
Jamie Poitra's avatar Jamie Poitra

Header errors?

Care to paste in what the errors said?

I ran that code through a few tests and it was working fine on my end. Could be I didn’t account for something though.

Jamie

       
radar77's avatar
radar77
28 posts
17 years ago
radar77's avatar radar77

Here are the error codes being being generated:

Warning: Cannot modify header information - headers already sent by (output started at /home/xxxx/domains/mydomain.com/html/_sys/plugins/pi.radar.php:73) in /home/xxxx/domains/mydomain.com/html/_sys/core/core.functions.php on line 726

Here is the code in the plugin:

<?php

$plugin_info = array(
    'pi_name'           => 'Radar',
    'pi_version'        => '0.1',
    'pi_author'         => 'Michael Reiner',
    'pi_author_url'        => 'http://mydomain.com/',
    'pi_description'    => 'Captures and stores WebAlias',
    'pi_usage'          => Radar::usage()
    );


class Radar
{

 /** ----------------------------------------
    /**  radar WebAlias
    /** ----------------------------------------*/

    function Web_alias()
    {
        global $IN, $FNS;
        
        $seconds = 60;
        
        if ($IN->GBL('web_alias', 'COOKIE') == '')
        {
            $web_alias = ($IN->GBL('ID') != '') ? $IN->GBL('ID') : 'grace';
            $FNS->set_cookie('web_alias', $web_alias, $seconds);
            return $web_alias;
        }
        else
        {
        return $IN->GBL('web_alias', 'COOKIE');
        }
        
    }
    /* END */

    // ----------------------------------------
    //  Plugin Usage
    // ----------------------------------------

    // This function describes how the plugin is used.
    //  Make sure and use output buffering

    function usage()
    {
            
        ob_start();
?>
Captures and stores the WebAlias.

{exp:radar:web_alias}  

<?php
        $buffer = ob_get_contents();

        ob_end_clean();

        return $buffer;
    }
    /* END */
}
// END CLASS
?>
       
Jamie Poitra's avatar
Jamie Poitra
409 posts
17 years ago
Jamie Poitra's avatar Jamie Poitra
Here are the error codes being being generated:
Warning: Cannot modify header information - headers already sent by (output started at /home/xxxx/domains/mydomain.com/html/_sys/plugins/pi.radar.php:73) in /home/xxxx/domains/mydomain.com/html/_sys/core/core.functions.php on line 726

Well, there’s a fun one. I’m not sure why that would happen.

Where is this plugin occurring in your templates?

Also, I didn’t catch this the first time, obviously, but the web_alias function should be all lowercase. You only do the first letter uppercase thing if your function has the same name as the class and then you don’t need to call the function in your EE tag.

I tried the plugin as you have it above on another of my systems again with no problem, so I’m slightly baffled at the error. Th

I’m curious enough to logon to your system to try figure out why that error is happening if you would like. Feel free to PM me your ftp/ee info should you feel like having me take a look.

Jamie

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
17 years ago
Lisa Wess's avatar Lisa Wess

If I may step in - that error (Cannot modify header information…) almost invariably happens if there is white space before the opening php tag, or after the closing php tag. That may be a quick fix, to make sure that there are no extra lines or spare whitespace.

       
Jamie Poitra's avatar
Jamie Poitra
409 posts
17 years ago
Jamie Poitra's avatar Jamie Poitra

Thanks Lisa,

I should have done a search. My troubleshooting other people’s problems sonar is a little rusty.

Jamie

       
radar77's avatar
radar77
28 posts
17 years ago
radar77's avatar radar77

I realized that as well, but there does not appear to be any extra whitespace anywhere, as far as I can tell. Any other ideas on why I would be getting that error?

@Jamie Let me know if you’re still willing to take a look. I’ll PM you info.

       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

Can you zip the file and attach it here, radar? It’s indicating a line number that output starts on that’s not possible with your code sample above.

       
radar77's avatar
radar77
28 posts
17 years ago
radar77's avatar radar77

I did make some changes to the plugin, which is perhaps why the error line wasn’t matching up.

Here is the latest error message I receive, which corresponds with the last line of the plugin:

Warning: Cannot modify header information - headers already sent by (output started at /home/xxxxx/domains/mydomain.com/html/_sys/plugins/pi.radar.php:66) in /home/xxxxx/domains/mydomain.com/html/_sys/core/core.functions.php on line 726

.zip file attached.

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.