Can't get http_redirect() to work

can’t get http_redirect() to work… what am I missing… required is only param, right?

this very simple code is not redirecting for me…

http_redirect('photos.php?tn=pb');

it’s not inside a conditional or anything… it’s on its own in a test pg I created, wrapped only by <?php…> tags… got code from the docs

thank you…

this

header('Location: photos.php');

is also not working for me…

the url is good… it’s in same dir… (and, at any rate, if it weren’t, I’d get a 404, right?)

thank you…

PS: my PHP is version 5.3.26

Is the header the very first thing output to the screen?

hmmm… I don’t get your question…

what do you mean by “header”?? not <head> element, right?
(don’t have <head> element, just redirect line inside <?php......?> tags…

sorry, I know my q must sound stupid…

a redirect line should work wherever you put it, right? it does in JSP…

WHY IS THE FONT IN THIS TEXTAREA BOX SO SMALL? can hardly see what I type… not good…;-(
bad usability…

the header() function.

It must be the first thing the script outputs or you’ll get a “headers already sent” error,

So, no, it will not always work wherever you put it.

have file called test.php… this is all code in it:

<p>cccccc</p>

<?php

echo 'test<br>';

http_redirect('photos.php?tn=pb');

header('Location: photos.php');

echo 'test2<br>';

?>

first “test” line prints, “test2” line doesn’t print… and neither redirect line works…

thank you…

HTML code outside of the PHP code sends output, so does echo.

aaaaah… I know that…:wink:

why are my PHP redirects not working???

I have this in JSP:

   if (URLwQString.indexOf("tn") == -1 && rURI.indexOf("photos.jsp") != -1) {    
        if (phNo.equals("")) {
        // following line is relevant line:
            response.sendRedirect(rURL + "?pn=1");
        } else {
            if (!checkPhotoNo(phNo, iLastPhoto)) {
        // and this one:
                response.sendRedirect(rURL + "?pn=1");
            } else if (checkPhotoNo(phNo, iLastPhoto)) { 
                iPhNo = Integer.parseInt(phNo);
            }
        }
    } 

so how do I do something like this in PHP???

thank you…

You need to rewrite the code so that header() will be the first thing the file outputs.
Mixing HTML code with PHP code can get real messy and is best avoided if possible. That said, you could probably get away with putting all the mark-up into $variables and echo them later if they still need to be output after the header conditional blocks.

so redirect stmts can only be the first line in a PHP file?

seriously? I don’t get this…

so how do you put redirect stms inside conditionals and functions and such??

am stumped…

is this documented in the docs?

No, not exactly.

Good point, I don’t see mention of it on that page. I guess they assune those using it will have knowledge of how HTTP works.

how HTTP works???

I have never had a problem with this in JSP, not once…

I can put redirect stms wherever I want… never a problem…

Admittedly docs for http_redirect doesn’t mention it, but docs for header does.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

You have mentioned this a few times.

Whoever led you to believe that JSP and PHP are the same language has done you a great disservice.

1 Like

nobody has led me to believe they are the same language… I have been converting an entire website… of course I know they’re different…

it seems this redirect problem is more than a simple “language” problem though…

why on earth can you not redirect to another page after any output is sent… this is a “language” issue? well, I don’t know what to say, I don’t get this…:frowning:

Sending output – that is, an HTTP response body – implicitly means sending HTTP response headers, because in an HTTP message, the headers always come before any body content. Therefore, if you try to add or change headers after the headers have already been sent, then it’s too late.

The Java code you posted gets away with this because it isn’t actually sending any output yet. Instead, it’s collecting headers and content in a response object.

I’m not a 100% certain, but are not JSP files compiled?
PHP files are not compiled.
Your misunderstanding about how the PHP header function works is not a problem with PHP, but stems from your lack of understanding how HTTP works.

Don’t feel bad about it, covering HTTP could fill a book on its own. And it’s not really something most PHP programmers get into very deeply.

Just accept that if you are working with the PHP header function it must be the first thing the file sends as output

I have seen hackish code using output buffer functions to get around this requirement, and I guess in some cases it might actually be absolutely neccessary. But IMHO from where I’ve seen it used it’s been used more as a way to avoid rewriting the code in a better way.

If you haven’t seen the “headers already sent” error message, you must have error reporting and display turned off. I strongly suggest you turn them on while you’re converting the JSP to PHP. Doing so will save you a lot of bewilderment.

oh brother… :frowning: yes JSP’s are actually compiled… (was wondering if that’s the case with PHP…:wink:

what I’m trying to do with PHP is exactly the same thing I’m doing with the Java/JSP code I posted (which is in a scriptlet, i.e., wrapped in <%…%> tags)

actually in JSP (and will be the same in PHP) this redirect stmt is in a file that has no HTML output… it’s in an include (i.e, included) file…

I don’t see “headers already sent” messages, what I get is stuff like "the page isn’t redirecting properly (FF), or “This webpage has a redirect loop” (Chrome)

thank you…

all I’m trying to do, basically:

typical url for my photo site is:

......./photos.php?pn=9

say a user gets cute and wants to mess with with me and writes this in browser address bar:

......./photos.php?pn=dsjflsdjfds

in which case I want to redirect to

......./photos.php?pn=1

(programmers have to think of all contingencies, right? :wink: )

so is there a “proper” way to do this in PHP?

thank you…

Ah, I was going by your example in post # 6

It could be the redirect file is included straight away and that could be fine.

“redirect loops” are usually a result of buggy htaccess rules. Are you using an htaccess fle to do rewrites / redirects ?