Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

URL Redirect (header already there)

by packetstormer (Monk)
on May 26, 2011 at 18:52 UTC ( #906878=perlquestion: print w/replies, xml ) Need Help??
packetstormer has asked for the wisdom of the Perl Monks concerning the following question:


I have a simple page that has some conditional statements on it. What I want to do is, if a certain condition is true I want to redirect to another page. The trouble is, in order to do this I need to print the header first. Once I print the header the redirect code :print $query->redirect('') doesn't work. Instead it just prints the usual "302 found location"

Does anyone know how to write this? If I remove the header the page doesn't load in the browser!

Replies are listed 'Best First'.
Re: URL Redirect (header already there)
by Fletch (Chancellor) on May 26, 2011 at 19:22 UTC

    You answered your own question if you just think about it: printing the redirect after the header doesn't work, so you need to determine what you want to do first then print the corresponding response (normal headers or a redirect). You also might want to take a look at something like CGI::Application which provides a more structured approach that aides in separating presentation and logic.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: URL Redirect (header already there)
by wind (Priest) on May 26, 2011 at 19:23 UTC
    Simply move the logic the determines you need to redirect before you output your regular header and page body.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://906878]
Approved by toolic
[stevieb]: I literally laughed. That's good :) Perhaps I just need to go climb another mountain and forget about it
shmem seeks a tate and smikes a pope
shmem .oO( Ca n'est pas un pipe )

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2017-03-28 22:16 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (342 votes). Check out past polls.