No it is not illegal for you to buy Viagra and Sertraline online even without a prescription. It is a violation of federal law for it to be sold without a prescription. However, that is a criminal violation for the seller. And FDA has not approved any online selling medications so their safety & efficacy are not regulated.
For further details-
So it's advised to avoid online medications purchasing without consulting your doctor.Sometimes they can cause adverse effects rather than providing benefits.
Hope this helps.