Results are reported on measurements of diffractive cross sections in deep-inelastic scattering (DIS) and photoproduction at HERA. The cross sections are compared for processes with a leading proton in the final state and with a large gap in the rapidity distribution of the final state hadrons. The cross section dependences on the proton fractional longitudinal momentum loss and the squared four-momentum transfer at the proton vertex t are interpreted in terms of an effective pomeron trajectory and a sub-leading exchange. The hypothesis of proton vertex factorisation is tested. The longitudinal structure function is extracted from the diffractive cross sections measured at different proton beam energies. The cross sections of diffractive dijet production are compared with QCD predictions at next-to-leading order (NLO) based on parton distribution functions obtained from diffractive inclusive DIS data. Combined NLO QCD fits to the inclusive and dijet DIS data are performed to determine diffractive quark singlet and gluon densities with a better precision. The ratio of the diffractive dijet cross sections in photoproduction and DIS is compared with NLO QCD predictions to test QCD collinear factorisation.