WordPress single sign on using cookies with shared user role functionality between more than 2 wordpress subdomains

did you try to use a plugin instead,

https://premium.wpmudev.org/forums/topic/configure-single-sign-on-for-multiple-wordpress-sites-and-make-it-secure

have a look at this and this one
https://wordpress.org/plugins/wp-multisite-sso/

hope this might help