Using a custom user agent subclass to get the segment part

I used the following subclass in order to extend a custom User_agent referrer function that provides only the segment part within the application for the referrer URL. It's very simple and is only intended to remove the site_url() part.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
* MY User Agent Class
* Subclass for User Agent
* @subpackage  Libraries
* @category    User Agent
* @author      Julian Magnone
class MY_User_agent extends CI_User_agent {

    function __construct()
        function myreferrer()
            echo substr_replace( $this->referrer(), '', 0, strlen(site_url()) );

In my specific case, it was useful for 'nexturl' implementation. For example, in case you have two different point of entrance for a specific function. Imagine you have a list, view and edit pages. The list allows to edit an entry, the view also allows you to edit the entry. When editing, you save and then you you would like to return to the calling page.

In the edit function you can save a $nexturl page and then when saving you can redirect to the nexturl. The problem with next url is that if you plan to use Codeigniter redirect() implementation, you should pass the segment part (not only the url). Using $this->agent->myreferrer() it returns the segment part only.

In your edit function, you use:

$data['nexturl'] = $this->agent->myreferrer();

And then, in your save function you can redirect as follows:

if ($this->input->post('nexturl'))

Julian Magnone

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.