1.6 engine running when turned off 2006

Hi I've got a 1.6 sport and I'm having trouble with ignition at times it wont start and key flashes on dash I can get it started by disconnecting the battery but at times it keeps running when ignition switch off and key out the only way to stop it is to stall it and then it will not start and I have to disconnect battery and leave it for a bit then it will start.. has anyone come across this problem any help would be much appreciated