a problem with refreshing the page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
siyaco
Forum Commoner
Posts: 26
Joined: Mon Feb 05, 2007 1:36 am
Location: Kurdistan Mountains

a problem with refreshing the page

Post by siyaco »

hi to everybody
i wrote a simple code for a quest book. it shows the 8/1page messages and at the bottom a form to write the new message. the problem is this;
when somebody writes a new message and it appears on mysite.com/page.php?page=1
and then if the user wants to refresh the page this warning appears;
confirm
the page you are trying to view contains POST DATA. if you resend the data any action the form carried out will be repeated
i controlled it by a if condition not to write the same data. but still this warning appears.

how can i control these not to resend the same data when it is refreshed. for example i see that non forums behaves like this. you submit your message, and refresh several times but there is no warning or problem.

thanks all in advance
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

user header() to redirect them to the page after the post completes
siyaco
Forum Commoner
Posts: 26
Joined: Mon Feb 05, 2007 1:36 am
Location: Kurdistan Mountains

Post by siyaco »

Burrito wrote:user header() to redirect them to the page after the post completes
thanks, i will look now. i hope i will understand :)
siyaco
Forum Commoner
Posts: 26
Joined: Mon Feb 05, 2007 1:36 am
Location: Kurdistan Mountains

Post by siyaco »

i am sorry but i did not understand well :oops: , may be because of my poor english. if you explain it for me i will be happy. a small reminder;

the user writes a new message and it appears on .mysite.com/mes.php?page=1
the user refreshes a warning appears. i want this;
when user refreshes none warning appears
thank again
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

after they post their message do this:

Code: Select all

// deal with posting stuff
mysql_query("INSERT INTO `table` ...");
header("Location: mes.php?page=1");
siyaco
Forum Commoner
Posts: 26
Joined: Mon Feb 05, 2007 1:36 am
Location: Kurdistan Mountains

Post by siyaco »

i tried it in localhost
for the first page it is shown on ..index1.php
the code is
if($user!=NULL && $message!=NULL)
{

$query = "INSERT INTO forum1 VALUES('', '".$user."', '".$date."', '".$time."', '".$message."')";
$result1 = mysql_query($query) or die('yazida bir hata oldu, ayni mesaji tekrarlamis olabilirsiniz');
header("Location: index1.php");
}
but it gives this error when the message is submitted
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\projeler\ziyaretci defteri\index1.php:11) in C:\wamp\www\projeler\ziyaretci defteri\index1.php on line 60
thanks
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you can't output anything (even whitespace) above your call to the header() function.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

..and use a full url, no half-assing :P
siyaco
Forum Commoner
Posts: 26
Joined: Mon Feb 05, 2007 1:36 am
Location: Kurdistan Mountains

Post by siyaco »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[quote="Burrito"]you can't output anything (even whitespace) above your call to the [url=http://php.net/function.header]header()[/url] function.[/quote]

may be, i am wasting your time but i did not understand really 

[quote]..and use a full url, no half-assing [/quote]

i did it like this;

Code: Select all

if($user!=NULL && $message!=NULL)
    {
    
   $query = "INSERT INTO forum1 VALUES('', '".$user."', '".$date."', '".$time."', '".$message."')";
   $result1 = mysql_query($query) or die('there is a fault');
     header("Location: http://localhost/projeler/ziyaretci_defteri/index1.php");
    }
but still gives this error
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\projeler\ziyaretci_defteri\index1.php:11) in C:\wamp\www\projeler\ziyaretci_defteri\index1.php on line 62
thank all in advance


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Burrito wrote:you can't output anything (even whitespace) above your call to the header() function.
siyaco
Forum Commoner
Posts: 26
Joined: Mon Feb 05, 2007 1:36 am
Location: Kurdistan Mountains

Post by siyaco »

i hope i don't waste your time but still i have not solved my problem. i read the php.net and i saw a warning below;
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.
as i understand; if there is a form to send an output to the page, the header function gives error. so, it gives when i browse. and i put it in the code, which a form exists. is it the problem, did i understand correct and how can i solve my problem?

thanks all in advance
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

It means you cannot output anything at all to the browser before calling that header function

this will fail (text is being output to the browser by the PHP print statement, before calling header())

Code: Select all

<?php
  print "some text going to the browser";
  header("location: index.php");
?>
this will fail (html tags have already been sent to the browser, before calling header())

Code: Select all

<html>
<head>
<?php
  header("location: index.php);
?>
</head>
<body>
</body>
</html>
this will fail

Code: Select all

                <------- there is a blank line at the very top of the script, this outputs to the browser, so headers 
                            already sent
<?php
  header("location: index.php);
?>
this is okay

Code: Select all

<?php
  $Query = "INSERT INTO blah blah";
  $Result = mysql_query($Query);

  //do other processing that does not involve writing output to the browser

  header("location: index.php");
?>
So in short no text whatsoever can be sent to the browser before calling the header("location: index.php").
siyaco
Forum Commoner
Posts: 26
Joined: Mon Feb 05, 2007 1:36 am
Location: Kurdistan Mountains

Post by siyaco »

thanks for your explanations with examples.
so, i can't put header() into my code, because there are html tags, and other output keys like echo .
i think i have to find a new solution which i write about it above

thanks all in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Adjust your code such that all the logic is processed before presentation specific code needs to be executed.
Post Reply