Earth does have a magnetic field, and it's used to protects us from the suns sun rays and meteors that come towards earth the field comes from the liquid layers of earth. The magnetic field acts like a shield and protects earth.
Mars had a magnetic field 4.1 billion years ago but since the layers doesn't actively move and produce energy its uncappable of making a magnetic field.