Most stars in the Galaxy, including the Sun, were born in high-mass star-forming regions. It is hence important to study the chemical processes in these regions to better understand the chemical heritage of both the Solar System and most stellar systems in the Galaxy. The molecular ion HCNH+ is thought to be a crucial species in ion-neutral astrochemical reactions, but so far it has been detected only in a handful of star-forming regions, and hence its chemistry is poorly known. We have observed with the IRAM-30m Telescope 26 high-mass star-forming cores in different evolutionary stages in the J=3-2 rotational transition of HCNH+. We report the detection of HCNH+ in 16 out of 26 targets. This represents the largest sample of sources detected in this molecular ion so far. The fractional abundances of HCNH+, [HCNH+], w.r.t. H2, are in the range 0.9 - 14 X $10^{-11}$, and the highest values are found towards cold starless cores. The abundance ratios [HCNH+]/[HCN] and [HCNH+]/[HCO+] are both < 0.01 for all objects except for four starless cores, for which they are well above this threshold. These sources have the lowest gas temperature in the sample. We run two chemical models, a cold one and a warm one, which attempt to match as much as possible the average physical properties of the cold(er) starless cores and of the warm(er) targets. The reactions occurring in the latter case are investigated in this work for the first time. Our predictions indicate that in the warm model HCNH+ is mainly produced by reactions with HCN and HCO+, while in the cold one the main progenitor species of HCNH+ are HCN+ and HNC+. The results indicate that the chemistry of HCNH+ is different in cold/early and warm/evolved cores, and the abundance ratios [HCNH+]/[HCN] and [HCNH+]/[HCO+] is a useful astrochemical tool to discriminate between different evolutionary phases in the process of star formation.