You get headaches when you get dehydrated.. which is the same thing that happens after getting drunk and getting a hangover the next morning.
If you want the specifics WHY this happens, it's because of this.
When you start to get dehydrated, your blood thickens and your body starts losing water from every where to try to keep your blood consistency thin enough to travel in your body. So you start losing water from various organs after awhile so your blood doesn't get too thick. The brain is FULL of water and blood. Only other body part with more is the heart water/blood per square inch.
So, when you get dehydrated, your brain literally shrinks. This shrinking causes headaches, and depending on your genetics, even hemoragging as your brain pulls away from the skull.
Ask anyone that's ever had a hangover if they have a headache. The reason is the same and different. When you drink alcohol, your body replaces the water it's using with the fresh intake of alcohol. The more you drink, the more it replaces and the more you pee out the "real water" your body needs. By the time morning comes around, your liver has finally filtered out and chemically processed the alcohol in your system into something else your body can't use as lubrication like water. Now, you are dehydrated because when the alcohol replaced all the water your body would use a lubrication when you pissed it out, chances are you didn't drink water again before passing out. Now, with all the alcohol processed, you can have severe dehydration which will cause headaches in the manner I described above.
NOW, if you are getting headaches after being in the sun for only moments, which wouldn't cause dehydration, then you have a photosensitivity to bright lights. Your eyes get hurt from the brightness of sunlight and due to close wiring of the optic nerve in some people to nerves in the brain, that pain sensation is passed along and felt in the brain as a headache. Note, not everyone who is really photosensitive to light will get headaches either, it just depends on genetics and how close your nerves for your eyes are wired to you brain. For me, mine are closer to my nose so anytime I get hit with bright light I go into sneezing fits until my eyes adjust. Which happens to other people as well.
Usually people with lighter eye color are prone to photosensitivity problems. I have bright green eyes so I can be. My dad has blue eyes so light they are almost white when he's indoors. When he steps outside, his eyes go bloodshot in an instant.