I've had the most luck with a pomodoro timer. You basically commit to working for 15-30 minutes, then you take a short break. Take the breaks: they make your focus periods better. Meditating for a few minutes during breaks helps me. Try to shift your mindset from always drinking from the dopamine fountain, and tell yourself that it's a reward when you browse for a little bit during a break. Just make sure you start working again when the break is over.
Nothing is foolproof. Good luck.
http://rinik.net/pomodoro/