Functions for setting cookies and outputting their IDs
First of all, let me tell you that I have zero experience with Codeigniter, but I suppose it is an MVC / OOP based framework, and I have plenty of experience with those. I am not trying to declare myself an expert or anything, just trying to be helpful and constructive. So here we go...

A few general words of advice:
 - try to avoid nesting control structures. Work with early returns in stead. You hardly ever need to nest an if inside an if, or even use an else. If you realy do, you are probably not following the principles of single responsibility
 - don't waste resources by storing stuff in variables and only using them once immediately after you stored them. Use the value you wanted to store directly.
 - Put comments inside your code. A lot of them! It may take a little bit longer now, but it will safe you time when you ever have to debug or alter your code!

With those hints in mind, I would write your first function as follows:

function setcookie() {  
    // prepare the entry id
    $entry_id = $this-&gt;EE-&gt;TMPL-&gt;fetch_param('entry_id');

    // if cookie has not been set before
    if (! isset($_COOKIE['recently_viewed'])) {
        // write the current entry id to a cookie
        setcookie('recently_viewed', serialize(array($entry_id)), pow(2,31)-1, '/', '');
        // and we are done (return self for chaining)
        return $this;
    }

    // convert the cookie to an array
    $currentSession = unserialize($_COOKIE['recently_viewed']);

    // if the current entry id is already in the cookie
    if (in_array($entry_id, $currentSession)) {
        // we are done (return self for chaining)
        return $this;
    }

    // if there are already 5 entries
    if (count($currentSession) &gt; 5) {
        // shift of the first entry 
        // don't use an index here, cause there is no garantee the first element actualy has index 0
        array_shift($currentSession);
    }

    // push the current id onto the list
    $currentSession[] = $entry_id;
    // write it to a cookie
    setcookie('recently_viewed',serialize($currentSession), pow(2,31)-1, '/', '');
}


And the second function:

function recently_viewed() {
    // prepare the tagdata
    $tagdata = $this-&gt;EE-&gt;TMPL-&gt;tagdata;

    // if no recently_viewed cookie is present
    // (isset is supposed to be a little better in performance then array_key_exists)
    if (! isset($_COOKIE['recently_viewed']) ) {
        // do something and return the result
        return $this-&gt;EE-&gt;TMPL-&gt;parse_variables(
            $tagdata, 
            array(
                array('entry_ids' =&gt; null)
            )
        );
    }   

    // convert the recently viewed ids to a piped string
    // do something with it and return the result
    return $this-&gt;EE-&gt;TMPL-&gt;parse_variables(
        $tagdata, 
        array(
            array(
                'entry_ids' =&gt; implode('|', unserialize($_COOKIE['recently_viewed']))
            )
        )
    );
}


You could even put that function into a single 'line' of code, which should be a bit more performant, but a lot less readable. And imo you should always take readable code over performant code when the difference is small. Just as an example though:

function recently_viewed() {
    return $this-&gt;EE-&gt;TMPL-&gt;parse_variables(
        $this-&gt;EE-&gt;TMPL-&gt;tagdata,
        array(
            array(
                'entry_ids' =&gt; 
                    isset($_COOKIE['recently_viewed']) 
                    ? implode('|', unserialize($_COOKIE['recently_viewed'])) 
                    : null
            )
        )
    );
}