Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Send a Standard looking 404 error to unwanted vistors

by krisahoch (Deacon)
on Oct 01, 2002 at 22:05 UTC ( #202128=perlquestion: print w/replies, xml ) Need Help??

krisahoch has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I am trying to write a simple web page that checks your IP address. If your ip address is okay (I have a list that is checked against), then the page allows you to log into the web site. If you IP address is not okay, then it should give the 404 error.

The login part, and address checking part is no biggie, simple stuff really. The tricky part is getting the 404 error to display in the browser. Here is the code snippit that I currently use to send back the 404 error.

#!/bin/perl use strict; use warnings; sub NotFound { return '404'; } use CGI::Pretty; use CGI::Carp qw(fatalsToBrowser); $CGI::Pretty::INDENT = ' '; my $WP = new CGI::Pretty; print $WP->header(-status => &NotFound);

The code sends back the following.

Status: 404 Content-type: text/html; charset=ISO=8859-1
but in a browser it sends a blank screen. A view source reveals the following...
<html><body></body></html>

What I want it to send back is the normal 404 error page for the particular web browser that you are using. Click on this link 404 Error to see what I am after.

If anyone has had any experiance with this, then please let me know. I'd hate to be wasting my time on this.

Thanks yall

Kristofer Hoch

Replies are listed 'Best First'.
Re: Send a Standard looking 404 error to unwanted vistors
by Dog and Pony (Priest) on Oct 01, 2002 at 22:33 UTC
    First off, I seem to recall that status should be '404 Not found', not only the number.

    Secondly, for a message to display, you will have to actually print something. Such as

    print $WP->h1('404 Page not found');

    What you mean with 'for the particular browser you are using' I am not sure - is it that IE replaces errors with its own "friendly" pages? You should be happy if it does not for you, that is a really annoying "feature" that sometimes hides important information to the user.

    If you really want it to display the built-in message, I'm not sure why it doesn't trigger. Maybe because some header is missing, may be because it recognizes that it is a script, or at least not a plain HTML doc (on the files extension).

    You could, of course, redirect (IE) users to some bogus URL like so:

    print $WT->redirect('/showIE404') if($browser eq 'IE');
    I *hate* browser checks, but if you must, you must.

    What I really think you should do is have a fun 404 page instead, like SGI's (I think) that showed a random very sad and crying baby when a page could not be found. Of course with the proper status code. Or maybe one of my personal favourites, http://icculus.org/404 (got it from someone here, was it Aristotle? Whoever it was, thanks again! :) ). And oh, try reloading it for new messages.


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Send a Standard looking 404 error to unwanted vistors
by Ovid (Cardinal) on Oct 01, 2002 at 22:34 UTC

    krisahoch wrote:

    I am trying to write a simple web page that checks your IP address. If your ip address is okay...

    Ovid responds:

    $id_address ne $user;

    There is no reliable way to map IP address to user. Many people, due to their ISP or network configuration will have a different IP address every time they connect to your box. You will need to use some sort of authentication mechanism to attempt to ensure that you only have valid users.

    Further, if this is some sort of security measure, you should be aware that IP addresses can be spoofed.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      I should give a little background as to why I am doing this.

      My wife and I post family pictures and house pictures on the web. My wife also goes on talkers and makes a lot of friends. She only gives out the webpage to friends but sometimes others get it, and then harass her.

      We are about to move into our new house, and when we do, she is going to drop the external website and use the Server that I am putting together.

      Family members will have a redirection site to get to the pages, but others will have the direct site.

      Here is how the direct site works. When my wife gives out the site to someone, she goes to a page that only allows access from the localhost:).

      • She Enters the person's First and Last name (could be an alias)
      • She enters the person's email address
      • She enters the person's current ip address
      • She enters a timeout (15 mins to 30 mins).
      The system will e-mail the person with the neccssary login information (automatically generated username and password). When the time out lapses, that entry is logged and deactivated (That IP address can no longer connect).

      What do yall think? Is is too paranoid?

        What do yall think? Is is too paranoid?

        No, but your approach sounds like a royal pain-in-the-ass, and has a few rough edges to it, like "She enters the person's current ip address". How does she know? Does she rely on them knowing? How do they know? What happens when they're coming in through a proxy server (e.g., AOL) that might change IP addresses between page fetches? Yuck, yuck, yuck.

        I think you'd be better off setting up password protection for the directory. Augment it with some side bookkeeping so that you can expire users, or preallocate a number of users and hand them out on demand.

        Or, if you don't want people to even know that there's a hidden page somewhere. provide a page with an unlabeled "password" box. Provide a one-time (or time-bound) password, and be "redirected" to dynamic content. Then, all your wife has to do is hand out passwords.

        This all seems like far too much work. Just put the webpages at
        http://yourserver/~youruser/hidden/SOMESECRETWORDOFTHEDAY/picture1.jpg http://yourserver/~youruser/hidden/SOMESECRETWORDOFTHEDAY/picture2.jpg http://yourserver/~youruser/hidden/SOMESECRETWORDOFTHEDAY/picture3.jpg ...
        and then tell them to go to http://yourserver/~youruser/hidden/SOMESECRETWORDOFTHEDAY/. As long as there's an index.html at hidden (even as simple as I've got at http://www.stonehenge.com/pic/, which is my personal version of this), there'll be no listing of the contents, so no way to determine what the secret word of the day is. Change the SOMESECRETWORDOFTHEDAY frequently, and you control exactly how long someone can be out of touch with you and still see the page.

        -- Randal L. Schwartz, Perl hacker

Re: Send a Standard looking 404 error to unwanted vistors
by Abstraction (Friar) on Oct 01, 2002 at 22:34 UTC
    What I want it to send back is the normal 404 error page for the particular web browser that you are using.

    The 404 Error page is not determined by a browser, it's done at the server level. You can cutomize your 404 error page to say whatever you want it to. See this page for details on how to do this with Apache.

    If you want to do this in your code you'll want to do something like the following.

    #!/bin/perl use strict; use warnings; sub NotFound { return '404'; } use CGI::Pretty; use CGI::Carp qw(fatalsToBrowser); $CGI::Pretty::INDENT = ' '; my $WP = new CGI::Pretty; print $WP->header(-status => &NotFound), $WP->h1("404 Page Not Found");
Re: Send a Standard looking 404 error to unwanted vistors
by fsn (Friar) on Oct 01, 2002 at 22:29 UTC
    There is no such thing as the normal 404 error page for a particular browser. The browser is displaying what the webserver is sending to it. Create a nice informational 404 page, telling the user to go elsewhere or get lost or something, and have the script send it to the user.

    Here's a whole site dedicated to creating spiffy looking 404 pages: www.404lounge.net.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://202128]
Approved by Ovid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2023-02-04 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?