Pardeep Poria
Pardeep Poria

Reputation: 1049

Encrypting url(parameters only)

In one of projects i got recently have all the urls like

www.abc.com/controller/action/1222

I want to encrypt the url parameters only to achieve something like

www.abc.com/controller/action/saddsadad232dsdfo99jjdf

I know I can do it by changing all the urls one by one and sending the encrypted parameters and dealing with them all over the places in the project.

So my question is Is there a way I can encrypt all the urls at once without making changes to every link one by one ?

Just need a direction.I guess I put all the details needed.

thanks !

Upvotes: 0

Views: 320

Answers (2)

Alexander Popov
Alexander Popov

Reputation: 836

Here is a solution if you use the site_url helper function and you are sure that all your URLs comply this format www.abc.com/controller/action/1222:

All you need is to override the site_url method of the CI_Config class

class MY_Config extends CI_Config
{

    public function site_url($uri = '', $protocol = NULL)
    {
        $urlPath = ltrim(parse_url($this->_uri_string($uri), PHP_URL_PATH), '/');
        $segments = explode('/', $urlPath);
        $numOfSegments = count($segments);
        $result = [$segments[0], $segments[1]]; // controller and action

        // start from the third segment
        for($i = 2; $i < $numOfSegments; $i++)
        {
            // replace md5 with your encoding function
            $result[] = md5($segments[$i]);
        }

        return parent::site_url($result, $protocol);
    }

}

Example:

echo site_url('controller/action/1222'); will outputwww.abc.com/controller/action/3a029f04d76d32e79367c4b3255dda4d

Upvotes: 1

DinosaurHunter
DinosaurHunter

Reputation: 702

I use a Hashids helper. I think this will do what you're after.

You can pass parameters to your functions like this:

base_url('account/profile/' . hashids_encrypt($this->user->id))

You can then decrypt it within the function and use it however you want:

$id = hashids_decrypt($id);

Upvotes: 0

Related Questions