On the page where you want to set the `cookie` write this code and check

    if(get_query_var('my_var') == 'somevalue'){
        setcookie('my_cookie', get_query_var('my_var'), strtotime('+1 day'));
    }

If we go with your code then it will set `coookie` on every page who having query variable

`?my_var=somevalue`