You need very good contacts inside an airline. Personal contacts with captain and the like. Sometimes you still can get jump seat flights (a rather uncomfortable seat used for ferrying extra crew) in the cockpit.
Pre-2001, it wasn't unusual for children to be shown into the cockpit during the flight. (Although, prices were higher back then, so it was less common for children to fly anyway.)
I saw the cockpit of an American Airlines plane when I was about 11 years old, and disappointed the captain when I was confused by his American humour.
It's still allowed, the passenger gets a temporary airline ID to wear around their neck so passengers don't get confused. And typically you close the forward galley curtain before opening the door, same as for crew entering and leaving the cockpit for in flight rest etc.
Edit: I'm in Europe, not sure how American airlines handle it.