Welcome! Log In Create A New Profile

Advanced

[PHP] header() not working?

Posted by Tedd Sperling 
Tedd Sperling
[PHP] header() not working?
December 13, 2016 05:41AM
Hi gang:

For several years I have had an example of a redirect working on my college’s Server for my students to review.

The code is simple:

header ("Location:$where");
exit(0);

The $where is URL’s such as “amazon.com” coming from a POST:

$where = isset($_POST['where']) ? $_POST['where'] : null;

Like I said, this has worked for years and now it doesn’t.

I suspect something has changed in the school’s Server, but I don’t know what that may be.

Any ideas as to why and how to fix it?

Cheers,

tedd
_______________
tedd sperling
tedd@sperling.com




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
[PHP] Re: header() not working?
December 13, 2016 05:41AM
On 08.12.2016 at 17:30, Tedd Sperling wrote:

> Hi gang:
>
> For several years I have had an example of a redirect working on my college’s Server for my students to review.
>
> The code is simple:
>
> header ("Location:$where");
> exit(0);
>
> The $where is URL’s such as “amazon.com” coming from a POST:
>
> $where = isset($_POST['where']) ? $_POST['where'] : null;
>
> Like I said, this has worked for years and now it doesn’t.
>
> I suspect something has changed in the school’s Server, but I don’t know what that may be.
>
> Any ideas as to why and how to fix it?

Have you checked that no output has been generated before these lines
are executed? Even a single space could stop the headers from being
sent. I suggest you enable error_reporting, and check the respective
log file; there you might find something like "Cannot send headers.
Headers already sent".

--
Christoph M. Becker

>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ryan Pallas
Re: [PHP] Re: header() not working?
December 13, 2016 05:41AM
On Thu, Dec 8, 2016 at 10:40 AM, Christoph M. Becker <cmbecker69@gmx.de>
wrote:

> On 08.12.2016 at 17:30, Tedd Sperling wrote:
>
> > Hi gang:
> >
> > For several years I have had an example of a redirect working on my
> college’s Server for my students to review.
> >
> > The code is simple:
> >
> > header ("Location:$where");
> > exit(0);
> >
> > The $where is URL’s such as “amazon.com” coming from a POST:
> >
> > $where = isset($_POST['where']) ? $_POST['where'] : null;
> >
> > Like I said, this has worked for years and now it doesn’t.
> >
> > I suspect something has changed in the school’s Server, but I don’t know
> what that may be.
> >
> > Any ideas as to why and how to fix it?
>
> Have you checked that no output has been generated before these lines
> are executed? Even a single space could stop the headers from being
> sent. I suggest you enable error_reporting, and check the respective
> log file; there you might find something like "Cannot send headers.
> Headers already sent".
>
>
If having passed that check, have you verified the header isn't returned?
If it is, the browser may not be respecting it, because according to the
specification[1] "The field value consists of a single absolute URI", and
the string "amazon.com" does not validate as an absolute URI according to
the specs for that[2] which states "absoluteURI = scheme ":" *( uchar |
reserved )".


[1] - https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
[2] - https://www.w3.org/Protocols/HTTP/1.0/spec.html#URI


> --
> Christoph M. Becker
>
> >
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Christoph M. Becker
Re: [PHP] Re: header() not working?
December 13, 2016 05:41AM
On 08.12.2016 at 18:53, Ryan Pallas wrote:

> On Thu, Dec 8, 2016 at 10:40 AM, Christoph M. Becker <cmbecker69@gmx.de>
> wrote:
>
>> On 08.12.2016 at 17:30, Tedd Sperling wrote:
>>
>>> Hi gang:
>>>
>>> For several years I have had an example of a redirect working on my
>> college’s Server for my students to review.
>>>
>>> The code is simple:
>>>
>>> header ("Location:$where");
>>> exit(0);
>>>
>>> The $where is URL’s such as “amazon.com” coming from a POST:
>>>
>>> $where = isset($_POST['where']) ? $_POST['where'] : null;
>>>
>>> Like I said, this has worked for years and now it doesn’t.
>>>
>>> I suspect something has changed in the school’s Server, but I don’t know
>> what that may be.
>>>
>>> Any ideas as to why and how to fix it?
>>
>> Have you checked that no output has been generated before these lines
>> are executed? Even a single space could stop the headers from being
>> sent. I suggest you enable error_reporting, and check the respective
>> log file; there you might find something like "Cannot send headers.
>> Headers already sent".
>
> If having passed that check, have you verified the header isn't returned?
> If it is, the browser may not be respecting it, because according to the
> specification[1] "The field value consists of a single absolute URI", and
> the string "amazon.com" does not validate as an absolute URI according to
> the specs for that[2] which states "absoluteURI = scheme ":" *( uchar |
> reserved )".

Good catch! Note, however, that RFC 7231 which obsoletes RFC 2616 has
relaxed this:

Location = URI-reference

Still, `amazon.com` would not be valid according to the spec.

Cheers,
Christoph

> [1] - https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
> [2] - https://www.w3.org/Protocols/HTTP/1.0/spec.html#URI


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tedd Sperling
Re: [PHP] header() not working?
December 13, 2016 05:41AM
> On Dec 8, 2016, at 1:05 PM, Christoph M. Becker <cmbecker69@gmx.de> wrote:
>
> On 08.12.2016 at 18:53, Ryan Pallas wrote:
>
>> On Thu, Dec 8, 2016 at 10:40 AM, Christoph M. Becker <cmbecker69@gmx.de>
>> wrote:
>>
>>> On 08.12.2016 at 17:30, Tedd Sperling wrote:
>>>
>>>> For several years I have had an example of a redirect working on my
>>> college’s Server for my students to review.
>>>>
>>>> The code is simple:
>>>>
>>>> header ("Location:$where");
>>>> exit(0);
>>>>
>>>> -snip-
>
> Good catch! Note, however, that RFC 7231 which obsoletes RFC 2616 has
> relaxed this:
>
> Location = URI-reference
>
> Still, `amazon.com` would not be valid according to the spec.
>
> Cheers,
> Christoph
>
>> [1] - https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
>> [2] - https://www.w3.org/Protocols/HTTP/1.0/spec.html#URI

Thanks.

Good consideration, but that’s not the problem.

Here’s the URL and code, try it yourself:

http://citw.lcc.edu/~sperlt/citw185/examples/redirect.php

Cheers,

tedd

_______________
tedd sperling
tedd@sperling.com






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ryan Pallas
Re: [PHP] header() not working?
December 13, 2016 05:41AM
On Sat, Dec 10, 2016 at 8:22 AM, Tedd Sperling <tedd@sperling.com> wrote:

>
> > On Dec 8, 2016, at 1:05 PM, Christoph M. Becker <cmbecker69@gmx.de>
> wrote:
> >
> > On 08.12.2016 at 18:53, Ryan Pallas wrote:
> >
> >> On Thu, Dec 8, 2016 at 10:40 AM, Christoph M. Becker <cmbecker69@gmx.de
> >
> >> wrote:
> >>
> >>> On 08.12.2016 at 17:30, Tedd Sperling wrote:
> >>>
> >>>> For several years I have had an example of a redirect working on my
> >>> college’s Server for my students to review.
> >>>>
> >>>> The code is simple:
> >>>>
> >>>> header ("Location:$where");
> >>>> exit(0);
> >>>>
> >>>> -snip-
> >
> > Good catch! Note, however, that RFC 7231 which obsoletes RFC 2616 has
> > relaxed this:
> >
> > Location = URI-reference
> >
> > Still, `amazon.com` would not be valid according to the spec.
> >
> > Cheers,
> > Christoph
> >
> >> [1] - https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
> >> [2] - https://www.w3.org/Protocols/HTTP/1.0/spec.html#URI
>
> Thanks.
>
> Good consideration, but that’s not the problem.
>
> Here’s the URL and code, try it yourself:
>
> http://citw.lcc.edu/~sperlt/citw185/examples/redirect.php


You have output before header call. If you turned on error_reporting ike
Chris mentioned, you would see that output had already started.
Specifically this line is output:

print_r($_POST);



>
>
> Cheers,
>
> tedd
>
> _______________
> tedd sperling
> tedd@sperling.com
>
>
>
>
>
>
Tedd Sperling
Re: [PHP] header() not working?
December 13, 2016 05:41AM
> On Dec 10, 2016, at 10:25 AM, Ryan Pallas <derokorian@gmail.com> wrote:
>
> You have output before header call. If you turned on error_reporting ike
> Chris mentioned, you would see that output had already started.
> Specifically this line is output:
>
> print_r($_POST);
>>

Ok, so I took the print_r() out — so now what’s the problem? It still doesn’t work.

The point of all this is it (the code “as-is”) has been working for the last 8 years — so why does it fail now?

Cheers,

tedd

_______________
tedd sperling
tedd@sperling.com






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ryan Pallas
Re: [PHP] header() not working?
December 13, 2016 05:41AM
On Sat, Dec 10, 2016 at 8:29 AM, Tedd Sperling <tedd@sperling.com> wrote:

>
> > On Dec 10, 2016, at 10:25 AM, Ryan Pallas <derokorian@gmail.com> wrote:
> >
> > You have output before header call. If you turned on error_reporting ike
> > Chris mentioned, you would see that output had already started.
> > Specifically this line is output:
> >
> > print_r($_POST);
> >>
>
> Ok, so I took the print_r() out — so now what’s the problem? It still
> doesn’t work.
>
> The point of all this is it (the code “as-is”) has been working for the
> last 8 years — so why does it fail now?
>

It could have worked before it output buffering was configured. See
http://php.net/manual/en/book.outcontrol.php

My guess, is that it is not working now because the header.php file
contains some output. That's a guess considering because I haven't seen
that file.


>
> Cheers,
>
> tedd
>
> _______________
> tedd sperling
> tedd@sperling.com
>
>
>
>
>
>
Andrey Repin
Re: [PHP] header() not working?
December 13, 2016 05:41AM
Greetings, Tedd Sperling.
In reply to Your message dated Saturday, December 10, 2016, 18:29:17,


>> On Dec 10, 2016, at 10:25 AM, Ryan Pallas <derokorian@gmail.com> wrote:
>>
>> You have output before header call. If you turned on error_reporting ike
>> Chris mentioned, you would see that output had already started.
>> Specifically this line is output:
>>
>> print_r($_POST);
>>>

> Ok, so I took the print_r() out — so now what’s the problem? It still doesn’t work.

> The point of all this is it (the code “as-is”) has been working for the
> last 8 years — so why does it fail now?

Set in your relevant php*.ini

error_reporting = -1
log_errors = On
log_errors_max_len = 0

And it will tell you, what goes wrong.
Don't sit on your laps, asking "why does it fail". Debug your code.


--
Sincerely Yours, Andrey Repin <anrdaemon@freemail.ru>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
Re: [PHP] header() not working?
December 13, 2016 05:41AM
December 10 2016 7:23 AM, "Tedd Sperling" <tedd@sperling.com> wrote:
>
> Thanks.
>
> Good consideration, but that’s not the problem.
>
> Here’s the URL and code, try it yourself:
>
> http://citw.lcc.edu/~sperlt/citw185/examples/redirect.php
>
> Cheers,
>
> tedd

Since that is a school server, I'm going to guess that you don't have access to that servers configuration.

I lieu of server configuration access, the simple way of seeing if output is being sent, is to issue "print(headers_sent());" just before your call to header().

Then, if the headers have been sent, I would look at the your phpinfo() output and scan for the current value of output_buffers. More than likely someone modified the server settings (possibly an upgrade?) and this is simply an unforeseen side effect of them doing so.

Hope this helps

--
Jim Lucas
C - 5414085189
H - 5413234219
http://cmsws.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Per Jessen
Re: [PHP] header() not working?
December 13, 2016 05:41AM
Tedd Sperling wrote:

>
>> On Dec 10, 2016, at 10:25 AM, Ryan Pallas <derokorian@gmail.com>
>> wrote:
>>
>> You have output before header call. If you turned on error_reporting
>> ike Chris mentioned, you would see that output had already started.
>> Specifically this line is output:
>>
>> print_r($_POST);
>>>
>
> Ok, so I took the print_r() out — so now what’s the problem? It still
> doesn’t work.
>
> The point of all this is it (the code “as-is”) has been working for
> the last 8 years — so why does it fail now?

The obvious answer is - something changed.

FWIW, your code works for me. Apache 2.2, php 5.3.5, various browsers.

http://www.jessen.ch/tedds


--
Per Jessen, Zürich (5.6°C)
http://www.cloudsuisse.com/ - your owncloud, hosted in Switzerland.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ford, Mike
RE: [PHP] header() not working?
December 13, 2016 11:20AM
What's does include('includes/header.php') do? I'm betting there's output in there! If so, put it inside your

if ($step == 0)

where it belongs anyway. (As does the include of footer.php, incidentally.) There's no point putting anything that might possibly do output in a path that can issue a redirect, so don't.

And, yes, this is almost certainly due to a change in the output buffering in your site's config.



Cheers!

Mike

--
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
110 Queen Square House, 80 Woodhouse Lane,
Leeds Beckett University, Leeds LS2 8NU, United Kingdom
E & S4B: m.ford@leedsbeckett.ac.uk T: +44 113 812 4730


> -----Original Message-----
> From: Tedd Sperling [mailto:tedd@sperling.com]
> Sent: 10 December 2016 15:23
> To: Christoph M. Becker <cmbecker69@gmx.de>
> Cc: Ryan Pallas <derokorian@gmail.com>; php-general General List <php-
> general@lists.php.net>
> Subject: Re: [PHP] header() not working?
>
>
> > On Dec 8, 2016, at 1:05 PM, Christoph M. Becker <cmbecker69@gmx.de>
> wrote:
> >
> > On 08.12.2016 at 18:53, Ryan Pallas wrote:
> >
> >> On Thu, Dec 8, 2016 at 10:40 AM, Christoph M. Becker <cmbecker69@gmx.de>
> >> wrote:
> >>
> >>> On 08.12.2016 at 17:30, Tedd Sperling wrote:
> >>>
> >>>> For several years I have had an example of a redirect working on my
> >>> college’s Server for my students to review.
> >>>>
> >>>> The code is simple:
> >>>>
> >>>> header ("Location:$where");
> >>>> exit(0);
> >>>>
> >>>> -snip-
> >
> > Good catch! Note, however, that RFC 7231 which obsoletes RFC 2616 has
> > relaxed this:
> >
> > Location = URI-reference
> >
> > Still, `amazon.com` would not be valid according to the spec.
> >
> > Cheers,
> > Christoph
> >
> >> [1] - https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
> >> [2] - https://www.w3.org/Protocols/HTTP/1.0/spec.html#URI
>
> Thanks.
>
> Good consideration, but that’s not the problem.
>
> Here’s the URL and code, try it yourself:
>
> http://citw.lcc.edu/~sperlt/citw185/examples/redirect.php
>
> Cheers,
>
> tedd
>
> _______________
> tedd sperling
> tedd@sperling.com
>
>
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

To view the terms under which this email is distributed, please go to:-
http://disclaimer.leedsbeckett.ac.uk/disclaimer/disclaimer.html
Tedd Sperling
Re: [PHP] header() not working?
December 14, 2016 04:00PM
> On Dec 13, 2016, at 5:13 AM, Ford, Mike <M.Ford@leedsbeckett.ac.uk> wrote:
>
> What's does include('includes/header.php') do? I'm betting there's output in there! If so, put it inside your
>
> if ($step == 0)
>
> where it belongs anyway.

> (As does the include of footer.php, incidentally.) There's no point putting anything that might possibly do output in a path that can issue a redirect, so don't.
>
> And, yes, this is almost certainly due to a change in the output buffering in your site's config.
>
> Cheers!
>
> Mike
>

Mike:

Thanks for your comments.

Your comments are interesting and I’ll look into them, but the code did work for the last 8 years.

My reason for starting this thread was to ask people how know the internals of php/server settings to tell me what is happing. Perhaps the init file, or some server directive is not set correctly, or whatever — not knowing, is the reason for me asking.

Cheers,

tedd
_______________
tedd sperling
tedd@sperling.com






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
Re: [PHP] header() not working?
December 14, 2016 04:20PM
On 14.12.2016 at 15:52, Tedd Sperling wrote:

>> On Dec 13, 2016, at 5:13 AM, Ford, Mike <M.Ford@leedsbeckett.ac.uk> wrote:
>>
>> What's does include('includes/header.php') do? I'm betting there's output in there! If so, put it inside your
>>
>> if ($step == 0)
>>
>> where it belongs anyway.
>
>> (As does the include of footer.php, incidentally.) There's no point putting anything that might possibly do output in a path that can issue a redirect, so don't.
>>
>> And, yes, this is almost certainly due to a change in the output buffering in your site's config.
>>
>> Cheers!
>>
>> Mike
>>
>
> Mike:
>
> Thanks for your comments.
>
> Your comments are interesting and I’ll look into them, but the code did work for the last 8 years.
>
> My reason for starting this thread was to ask people how know the internals of php/server settings to tell me what is happing. Perhaps the init file, or some server directive is not set correctly, or whatever — not knowing, is the reason for me asking.
>
> Cheers,
>
> tedd

Okay, I'll try to guess. It is possible, that the output_buffering ini
option[1] has been changed; while formerly it was set to value large
enough to automatically buffer the output before the header() call, now
it is too small to do so, what would explain the script "suddenly" failing.

[1]
<http://php.net/manual/en/outcontrol.configuration.php#ini.output-buffering>;

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tedd Sperling
Re: [PHP] header() not working?
December 14, 2016 04:40PM
> On Dec 14, 2016, at 9:52 AM, Tedd Sperling <tedd@sperling.com> wrote:
>
>> On Dec 13, 2016, at 5:13 AM, Ford, Mike <M.Ford@leedsbeckett.ac.uk> wrote:
>>
>> What's does include('includes/header.php') do? I'm betting there's output in there! If so, put it inside your
>>
>> if ($step == 0)
>>
>> where it belongs anyway.
>
>> (As does the include of footer.php, incidentally.) There's no point putting anything that might possibly do output in a path that can issue a redirect, so don't.
>>

Mike:

Your comment was right-on about the code. I’ve changed it and it works now. You can review here:

http://citw.lcc.edu/~sperlt/citw185/examples/redirect.php

I believe the “change” was in an update to PHP that no longer permitted using “header" as I did. The confusing part for me was that it actually did work before the update.

Even us old dogs can learn — thanks for the lesson.

Cheers,

tedd
_______________
tedd sperling
tedd@sperling.com




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login