Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Abort page load and redirect?

by FuBaR (Acolyte)
on Apr 09, 2005 at 18:59 UTC ( #446281=perlquestion: print w/replies, xml ) Need Help??

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

Hello all,
Is there any way to have a script load on a web page and if in the script a condition is met to have it abort the page load and redirect to another page?

The whole idea is I dont want to call the script from a link. That would be easy but the user could just type in the page and go there directly.
This isn't meant to stop someone who really wants to see the page, just a normal user navigating via the links or typed in in the address bar directly.

I want to have it so that if a condition is met they will NOT view the page they were loading and only view the page they are being redirected to.
I know this is a silly little excersise but since I couldn't get it to work the way I want it has me curious.
Calling the script isn't my problem...well maybe the WAY I'm calling it is but the script does run and check the condition. What is failing is the redirect.
using the exec SSI call is not an option so i call the script with a <script src=""></script> tag in the head of the html document.
Everything works fine up until this point in the script:
if(condition) { print "Content-type: text/html\n\n"; print "Location: $REDIRECT\n"; }
All that happens is the page that was being loaded is displayed. I know that the code inside the if statment IS being executed with a proper URL.
It's just not doing what I want.
Any ideas?

Replies are listed 'Best First'.
Re: Abort page load and redirect?
by ambs (Pilgrim) on Apr 09, 2005 at 19:02 UTC
    First, check if you are not printing anything before. If not, here are two ideas: add an exit after the location print statement. Other idea, why not to use and its redirect function?

    Alberto Simões

Re: Abort page load and redirect?
by Popcorn Dave (Abbot) on Apr 09, 2005 at 19:16 UTC
    ambs is right with the CGI tip. If you take a look at merlyn's new book Perls of Wisdom there's a section about calling web pages using CGI so you can limit linking directly to a subpage on your site. That should be enough to get you started. Plus the rest of the book is a great read. :)

    Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
        And not promote your book? :)

        Personally, I've picked up a number of good tips from the book so I'd encourage others to get it even if the columns are online.

        Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
Re: Abort page load and redirect?
by dave0 (Friar) on Apr 09, 2005 at 19:36 UTC
    Even assuming you're not printing anything before this, it won't work. You're printing two newlines at the end of your Content-type: header. This creates a blank line in your output, and in HTTP, a blank line indicates that headers are over and content begins on the following line. This should cause your Location: line to appear somewhere in the output document, rather than in the HTTP headers.

    You shouldn't even need a Content-Type header with Location, anyway.

    And, as said before,'s redirect() method is a nicer way to do this.

      Correct. I noticed the two newlines but didn't noticed they were not needed (and incorrect) in this case. dave0++

      Alberto Simões

Re: Abort page load and redirect?
by FuBaR (Acolyte) on Apr 10, 2005 at 00:15 UTC
    Hi Guys,
    Thanks for the hints.
    As i was afraid of the code is fine. If I use the script in a html link it works fine, redirect and all. It seems the problem is how I am calling the script within the html page.

    I did use the module and it worked well also, thanks for pointing that one out. Alas though, same result. It only works if I use it in a link.

    If anyone has any ideas on that one you can point me in the right direction, I don't expect people to post asnswers to html questions in a perl forum.

    Again, thanks :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://446281]
Approved by BazB
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2021-01-24 03:56 GMT
Find Nodes?
    Voting Booth?