Optimize Code

Posted on 16th Feb 2014 by admin

Just started getting into the use of cookies. The following code will redirect users to a set URL should they view a preset number of pages. I'm a little concerned with the speed in which this executes though. Is there a better way, or this fine?

Code: [Select]public function onPrepareContent(&$article, &$params, $limitstart)

{

global $mainframe;



$content = $article -> text;
$current = JURI::current();

$duration = $this->params->get('duration');
$destination = $this->params->get('destination');
$max_count = $this->params->get('maxcount');

$site_id = 'aseihthaslhetlasiehtlasndlksahdlv';
$value = 1;


if(!(isset($_COOKIE["$site_id"])))
{
setcookie("$site_id", $value, time() + 60 * 60 * 24 * $duration);
}


if(isset($_COOKIE["$site_id"]))
{
$increment = TRUE;
if($_COOKIE["$site_id"] > $max_count)
{
$value = 1;
setcookie("$site_id", $value, time() + 60 * 60 * 24 * $duration);
$increment = FALSE;

header("Location: $destination");

}

if($increment)
{
$value = $_COOKIE["$site_id"];
$value++;

setcookie("$site_id", $value, time() + 60 * 60 * 24 * $duration);
/*$content = $_COOKIE["$site_id"];*/
}
}


$article -> text = $content;

}

Other forums