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:
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.
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
@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
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
?>
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
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.
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
?>
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
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.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.