0

I have a cookie created in PHP with the jQuery method where the client enters a value in an input and this cookie is created with that Value while more values are created with a comma.

Example Creation cookie in jQuery:

<script type="text/javascript">
    $(function () {
        $("#btnAdd").click(function () {
            if($('#addtokendelivery').val() === ''){
            alert('Valor en Blanco');       
                                                   }
else {
            if ($.cookie('DeliveryToken')) {
            $.cookie("DeliveryToken", $.cookie("DeliveryToken") + ',' + $("#addtokendelivery").val());
            alert('Repartidores Agregados con Token´s: ' + $.cookie("DeliveryToken"));                  
            } else{
            $.cookie("DeliveryToken", $("#addtokendelivery").val());
            alert('Repartidor Agregado con Token: ' + $.cookie("DeliveryToken"));               
            }       
    }   
        });
        $("#btnRead").click(function () {
            alert($.cookie("DeliveryToken"));
        });
    });
</script>

Example cookie in google chrome value:

DeliveryToken: value1,value2,value3

Example of variable to compare with in laravel:

$orderitemaddons32 = DB::table('users')->where('id', $id)->first();
$orderitemaddons32 = $orderitemaddons32->userdelivery_id;
//result variable dd laravel
"value1"

For example, if it detects that the value 1 is inside the cookie, it proceeds to display the page.

What I want is to verify in the laravel blade if the cookie matches the information of the variable that it creates in my function and to show the page, otherwise not to show it or to put a message that the information is not valid.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

There are many ways to do what you want.

For having a condition before you proceed with a request or not, it's usual to use a middleware. https://laravel.com/docs/8.x/middleware From the middleware, you can continue with the request or to redirect the user to another page.

I guess your code could look like this in laravel:

$values = explode(',', $_COOKIE['DeliveryToken']);
if (in_array($orderitemaddons32, $values) {
    //all good, proceed
    // return $next($request);
} else {
    /*
    * return redirect()->route('home', [
    *        'message' => 'The information is not valid'
    *    ]);
    */
}

Btw, I don't find it secure this kind of usage with cookies, I mean, setting them from javascript and not encrypting them and securing them in server side. One could easily change the value of cookie. You could leave the cookies to laravel and thus use also the built-in functions.

xmanolis
  • 36
  • 5
  • Hi thanks for commenting, what would you call this function in laravel .blade? – Eduardo Rafael Jun 21 '21 at 22:43
  • For displaying a message you mean? it is just an if, you can have it in the controller and set a flag, or you can have it in the blade directly. If it had more advanced logic I would create a helper (https://stackoverflow.com/questions/28290332/best-practices-for-custom-helpers-in-laravel-5). For this case, I would prepare a flag in the controller and pass it then to blade. – xmanolis Jun 23 '21 at 05:50
  • Sorry, you asked what not how. Not sure. isUserInCookie() returning a boolean i guess? – xmanolis Jun 23 '21 at 06:02