Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encrypt laravel 5.2 URL or Routes?

I need to encrypt routes in this URL? Because I do not want user to access URL by changing the item id. For example, user can change /items/1234 to /item/5678. Although item 1234 and 5678 belong to the same user, I still want to restrict the behavior. What I am trying to do is encrypting the routes but I am not sure whether this is a proper way or not. Any suggestions?

like image 257
sandip kakade Avatar asked Dec 22 '16 05:12

sandip kakade


People also ask

How do I encrypt a URL?

Under Website security, click Traffic encryption (HTTPS/SSL). Choose when you want to redirect visitors to the secure URL. All http page requests will be redirected to the encrypted https page.

How encrypt URL in php?

PHP | urlencode() Function. The urlencode() function is an inbuilt function in PHP which is used to encode the url. This function returns a string which consist all non-alphanumeric characters except -_. and replace by the percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.

What encryption method is used in Laravel?

Laravel's encrypter uses OpenSSL to provide AES-256 and AES-128 encryption.

How can hide URL ID in Laravel?

There are multiple ways to hide/mask the ids in the URLs. The first way is by using UUIDs. UUIDs are Universally Unique Identifiers that can be used instead of the default incremental ids. Laravel comes with support for UUIDs.


1 Answers

You can encrypt your url parameter and decrypt it in your controller. You can try this:

In your view: Suppose your parameter is id or more parameter you can encrypt.

<?php
        $parameter =[
            'id' =>1,
        ];
    $parameter= Crypt::encrypt($parameter);
?>
<a href="{{url('/url/',$parameter)}}" target="_blank">a link</a>

Your route will be:

Route::get('/url/{parameter}', 'YourController@methodName');

In your controller, You can decrypt your parameter:

public function methodName($id){
    $data = Crypt::decrypt($id);
  }

You must be yous Crypt namespace in your top of controller

use Illuminate\Support\Facades\Crypt;

Note: You can encrypt url parameter with Crypt::encrypt($parameter) and decrypt with Crypt::decrypt($parameter)

like image 65
Nazmul Hasan Avatar answered Sep 20 '22 18:09

Nazmul Hasan