I doubt that the headaches have any connection to the medications. If by
magnesia, you mean
milk of magnesia, then it is safe to take either miralax or
milk of magnesia for long periods of time (like years) as long as you are seeing a doctor while doing this. You shouldn't take these medications for more than a week as doubleaaaron says, if you are not seeing a doctor. If you are seeing a doctor then it is safe.