I have the same problem thoughout my few years with apple. Most of the time its either the phone jack or the lighting port. Read a lot of suggestions and some work while others dont.
Am gonna list a few things that i tried and would probably works.
Lighting Port: The port's not clean so you may try to use a torchlight to look into the port. Remove all the dusk inside it with a pin or needle. check and scratch gently the pins inside the port to see if there's rust in the pin or dusk between the pins that block the connection to headphones or charging. try to remove them gently.
Lighting Jack: The Jack may have problem when the heads are blocked by dust or rust. Try to clean it with a wet tissue paper first. be gentle though cause if too much material is removed the jack will not work anymore. In extreme cases when it still does not work use a pin to scratch it clean. It may works if the rust is not too deep.
The Charging is especially difficult when it is more easy to rust than the headphone ones as its copper alloy imo. And too much scratching means you gonna need to buy a new one.
The headphone one is easier since its made of aluminium alloy, but rusting still occurs and its almost unnoticeable. so whenever its unable to connect and it still looks brand new, just try to directly scratch it with a pin and its highly likely to work again.
Hope this helps.