Welcome to the gastroenterology community! 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.
how long have you been on those medicines? I heard you shouldn't take them more than a week in a row.