If one fears that they may sin by looking at pornography what precautions can they take to prevent it especially in situations when they are alone or when they are going to sleep?

It’s true that loneliness is the best possible time for Shiatan to misguide ourselves. The best way to refrain from any sin we shouldn’t create or go in way where there is any possibility to be trapped by Shaitan.Also as much possible do not open any link or site which might have an offensive and vulgar scene or photo.
Do not go to sleep just after having your dinner I mean full stomach and before sleeping empty your bladder. Do not go to your bed unless you fall asleep. Avoid to be alone at your home. If your family members are going out and they aren’t call you then you also go somewhere else. Avoid such friends who is involved in watching porn films.
Remember Allah as much as possible and chant and Zikr when you feel that are going towards such evil activity.