This paper presents an online economic re-dispatch scheme based on the generation cost optimization with security constraints to mitigate line overloads before and after line and generation contingencies. The proposed optimization model considers simplification of mathematical expressions calculated from online variables as the power transfer distribution factor (PTDF) and line outage distribution factor (LODF). Thus, a first algorithm that calculates economic re-dispatch for online operation to avoid overloads during the normal operation and a second algorithm that calculates online emergency economic re-dispatch when overloads occur due to line and generator contingencies are proposed in this paper. The results show that the proposed algorithms avoid overload before and after contingencies, improving power system security, and at the same time reducing operational costs. This scheme allows a reduction of power generation units in the electricity market during online operation that considers line overloads in the power system.