Rest the leg. Avoid putting weight on the leg as best you can. If the pain is severe, you may need crutches until it goes away. Ask your doctor or physical therapist if they're needed.
Ice your leg to reduce pain and swelling. Do it for 20-30 minutes every three to four hours for two to three days, or until the pain is gone.
Compress your leg. Use an elastic bandage around the leg to keep down swelling.
Elevate your leg on a pillow when you're sitting or lying down.
Practice stretching and strengthening exercises if your doctor/physical therapist recommends them. Strengthening your hamstrings is one way to protect against hamstring strain.