Is it safe to use this code with WordPress to get cloudflare SSL working

I want to ask if this code snippet is safe to use in WordPress. As I am new and their are many experts here. So I would like to ask them about this code.

if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) { $_SERVER['HTTPS'] = 'on'; }

This code solves the redirect loop when using cloudflare SSL certificate.

Many forums and threads I have suggested to use similar code but some say that $_SERVER['HTTPS'] = 'on'; using this code is not a good practice. I want someone to guide


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Yes, its found ok. If any issue kindly ping me.