
An obsessed fan who threatened to electrocute himself to demonstrate his love for Nickelodeon star Miranda Cosgrove ... cannot come anywhere near her -- or send any more horrifying photos -- or else he'll find himself behind bars.
Cosgrove just appeared in an L.A. courtroom ... where a judge ordered Joshua Stockton to stay at least 100 yards away from the "iCarly" actress and not contact her for the next 3 years. He also has to turn in any weapons.
Stockton showed up to court wearing a big, black fur coat and a white bandana ... and seemed disoriented -- when the judge asked his age, Stockton had to check his drivers license.
We broke the story ... Cosgrove claimed Stockton stalks her wherever she goes ... and sent her chilling messages threatening suicide -- including frightening pics of a slit wrist.
In court docs, The 21-year-old actress said Stockton is "not living in the real world" ... and is not only a danger to himself ... but to her as well. The judge agreed.

See also

Nickelodeon Star Miranda Cosgrove ... Fan: I'm Going to Kill Myself 'Cause You Don't Love Me

'Modern Family' Star Sarah Hyland -- Ex-BF Ordered to Stay Away ... And That Goes for Her Little Dog Too!

Keanu Reeves -- I Need Protection from Delusional Woman
ncG1vNJzZmivp6x7tbnZZ5qopV9nfXKAjmpnaGhpZLqqvsCnm5plk6TAqL7Or5xmq6SWuayx0Wapnqukp66qusinnmanopmys3nJqKqhrZFiwLW7wqSrqKZf