Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Can I detect a broken link in my CGI Perl script

( #100383=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zach on Jul 27, 2001 at 21:22 UTC
Q&A  > CGI programming


Description:

How can I automatically determine if a link to a website in my script is broken (page doesn't exist) when the user clicks the link? If the link is broken, I want to link to a default page that explains that the link was broken. Thanks

Answer: Can I detect a broken link in my CGI Perl script
contributed by agent00013

I'm not sure about the CGI part of it, but for the actual checking of the link you might want to look into LWP::UserAgent and for an example of LWP::UserAgent in action, try looking here. It's a long script, but maybe it'll give you some ideas.

You may just want to let the server handle 404 errors on your side. If it's their server with the bad link, just validate your links every so often with a cron job and have somebody look over the results every so often to take out links that are permanently down. Checking every link as you click it would waste system resources and slow down your site more than necessary.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (11)
    As of 2014-09-18 17:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (120 votes), past polls