For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > September 2004 > Header Help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Header Help
DaRemedy

2004-09-26, 3:55 pm

Hiya, just need help with PHP headers.

Right, I have an index php page which has the following code within a header
redirect:

<?php
if ( empty($_GET['month']) )
if (empty($_GET['year']) )
{
$month = date(n);
$year = date(Y);
header("Location: index.php?month=$month&year=$year");
}
?>

On the same index.php page, I have a shoutbox (include file) which inputs
the content of the shoutbox form into a database. After the user fills out
the form, I want the index page to refresh again because if I leave it as it
is and a user inputs info into the form, if they press refresh, the form
resubmits thus recording two entries into the shoutbox, or however many
times refresh is pressed.

When I add a header redirect into the shoutbox include file, once the user
clicks on send, index.php throws a headers cannot be sent error message.

Can anyone help?

Cheers.


Janwillem Borleffs

2004-09-26, 8:55 pm

DaRemedy wrote:
> Hiya, just need help with PHP headers.
>


Multi-posting to at least three different news groups, it looks like you
need some help posting to usenet properly to...

If you want to post to more than 1 newsgroup, use the Newsgroups: field to
compose a comma-seperated list of target news groups. This procedure is
called "Cross-posting" and prevents individual threads on the same topic.
See the headers of this reply.

> When I add a header redirect into the shoutbox include file, once the
> user clicks on send, index.php throws a headers cannot be sent error
> message.
>


To quote the manual page at http://www.php.net/header:

"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. It is a
very common error to read code with include(), or
require(), functions, or another file access function,
and have spaces or empty lines that are output before
header() is called. The same problem exists when using
a single PHP/HTML file."


JW



DaRemedy

2004-09-26, 8:55 pm

I do apologise for cross posting.

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:415729d9$0$61750$abc4f4c3@news.euronet.nl...
> DaRemedy wrote:
>
> Multi-posting to at least three different news groups, it looks like you
> need some help posting to usenet properly to...
>
> If you want to post to more than 1 newsgroup, use the Newsgroups: field to
> compose a comma-seperated list of target news groups. This procedure is
> called "Cross-posting" and prevents individual threads on the same topic.
> See the headers of this reply.
>
>
> To quote the manual page at http://www.php.net/header:
>
> "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. It is a
> very common error to read code with include(), or
> require(), functions, or another file access function,
> and have spaces or empty lines that are output before
> header() is called. The same problem exists when using
> a single PHP/HTML file."
>
>
> JW
>
>
>



DaRemedy

2004-09-26, 8:55 pm

Even if I add the headers before any html output and making sure that there
is no whitespace, the header still doesn't work, is it because the header o
the index.php page is already being called? how do I get around it?

Many thanks.

"DaRemedy" <demonnet@btinternet.com.nospam> wrote in message
news:cj7e8l$49r$1@titan.btinternet.com...
> I do apologise for cross posting.
>
> "Janwillem Borleffs" <jw@jwscripts.com> wrote in message
> news:415729d9$0$61750$abc4f4c3@news.euronet.nl...
to[color=darkred]
topic.[color=darkred]
>
>



J.O. Aho

2004-09-27, 3:55 am

DaRemedy wrote:
> Even if I add the headers before any html output and making sure that there
> is no whitespace, the header still doesn't work, is it because the header o
> the index.php page is already being called? how do I get around it?


You have to be sure there aren't any whitespaces or html output on files that
are included too.

If you have more tha one header in the same file and they try to do simlare
things you will run into a conflict (think the first one is the one that will
be executed).
Sippy

2004-09-27, 8:55 am

Cheers for that,

I managed to get it sorted using an 'if statement'. I made is so that if the
shoutbox form is sent then to go to back index.php otherwise go to
index.php?month=$month&year=$year. Telling the form to go back to index.php
would call the month and year variables anyway, using:
<?php
(if form is sent do this) {
header("Location: index.php");
} else {
if ( empty($_GET['month']) )
if (empty($_GET['year']) )
{
$month = date(n);
$year = date(Y);
header("Location: index.php?month=$month&year=$year");
}
?>

Cheers for the help guys, really do appreciate it.

"J.O. Aho" <user@example.net> wrote in message
news:2rpg9cF1d0h92U1@uni-berlin.de...
> DaRemedy wrote:
>
> You have to be sure there aren't any whitespaces or html output on files
> that are included too.
>
> If you have more tha one header in the same file and they try to do
> simlare things you will run into a conflict (think the first one is the
> one that will be executed).



Michael Fesser

2004-09-27, 3:57 pm

.oO(Sippy)

One more thing:

>header("Location: index.php?month=$month&year=$year");


The Location-header requires an absolute address, including the scheme
(http://) and hostname.

Micha
DaRemedy

2004-09-27, 8:56 pm

Thanks Michael,

Is it absolutely nessecary to use an absolute address? because the code i
posted in an earlier post seems to work fine without any problems.

What I would really need to do is to change the header redirect slightly to
redirect to the same page that the shoutbox was filled in from, e.g. the
shoutbox was filled in from, lets say, the about.php page, so I would want
the shoutbox to go back to that particular page after the form is submitted.
Would I need to use $_SERVER['PHP_SELF'];

Cheers.

Sippy.

"Michael Fesser" <netizen@gmx.net> wrote in message
news:fapgl0p1a6ss3i74prbfacvv9cak8i36cj@
4ax.com...
> .oO(Sippy)
>
> One more thing:
>
>
> The Location-header requires an absolute address, including the scheme
> (http://) and hostname.
>
> Micha



Michael Fesser

2004-09-27, 8:56 pm

.oO(DaRemedy)

>Is it absolutely nessecary to use an absolute address? because the code i
>posted in an earlier post seems to work fine without any problems.


It works in most browsers (doesn't necessarily mean it'll work in all),
but a full address is required by the HTTP specification (RFC 2616):

"The field value consists of a single absolute URI."

In the PHP manual there's a little example on how to add scheme and
hostname automatically to build an absolute address from a relative one
for an RFC-compliant Location-header.

http://www.php.net/header

>What I would really need to do is to change the header redirect slightly to
>redirect to the same page that the shoutbox was filled in from, e.g. the
>shoutbox was filled in from, lets say, the about.php page, so I would want
>the shoutbox to go back to that particular page after the form is submitted.
>Would I need to use $_SERVER['PHP_SELF'];


Yep.

Micha
DaRemedy

2004-09-27, 8:56 pm

Cheers matey, I really do appreciate your help.

"Michael Fesser" <netizen@gmx.net> wrote in message
news:1ntgl0l368vrqt06t56r5qd5coneu8k0sm@
4ax.com...
> .oO(DaRemedy)
>
>
> It works in most browsers (doesn't necessarily mean it'll work in all),
> but a full address is required by the HTTP specification (RFC 2616):
>
> "The field value consists of a single absolute URI."
>
> In the PHP manual there's a little example on how to add scheme and
> hostname automatically to build an absolute address from a relative one
> for an RFC-compliant Location-header.
>
> http://www.php.net/header
>
to[color=darkred]
want[color=darkred]
submitted.[color=darkred]
>
> Yep.
>
> Micha



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com