Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 17th, 2005, 10:57 AM
soham@mic.gov.eg
Guest
 
Posts: n/a
Default to redirect to another page

I want to redirect to another file, isn't this the right code to do so

header("Location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/images/$file_name");

the interpreter give me this erorr instead of redirecting

Warning: Cannot modify header information - headers already sent by
(output started at E:\tryphp\direct.php:9) in E:\tryphp\direct.php on
line 70

what's wrong in the code coz i don't understand the erorr

  #2  
Old July 17th, 2005, 10:57 AM
Jan Pieter Kunst
Guest
 
Posts: n/a
Default Re: to redirect to another page

soham@mic.gov.eg wrote:[color=blue]
> I want to redirect to another file, isn't this the right code to do so
>
> header("Location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/images/$file_name");
>
> the interpreter give me this erorr instead of redirecting
>
> Warning: Cannot modify header information - headers already sent by
> (output started at E:\tryphp\direct.php:9) in E:\tryphp\direct.php on
> line 70
>
> what's wrong in the code coz i don't understand the erorr
>[/color]

Probably nothing wrong with the code. 'headers already sent' errors are
usually caused by whitespace that is already sent to the browser before
the header(), for example a space or return before the '<?php' start tag.

JP

--
Sorry, <devnull@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
  #3  
Old July 17th, 2005, 10:57 AM
Gordon Burditt
Guest
 
Posts: n/a
Default Re: to redirect to another page

>I want to redirect to another file, isn't this the right code to do so[color=blue]
>
>header("Location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/images/$file_name");
>
>the interpreter give me this erorr instead of redirecting
>
>Warning: Cannot modify header information - headers already sent by
>(output started at E:\tryphp\direct.php:9) in E:\tryphp\direct.php on
>line 70
>
>what's wrong in the code coz i don't understand the erorr[/color]

Send the headers BEFORE anything else. Don't send anything else
(like "<HTML>", or DOCTYPE, or even a blank line) before that.
Error or warning messages output to the page can also cause trouble.

What's on line 9 of direct.php?

Gordon L. Burditt

  #4  
Old July 17th, 2005, 10:57 AM
soham@mic.gov.eg
Guest
 
Posts: n/a
Default Re: to redirect to another page

on line 9 there was "<?php" and i removed all the "<HTML>", or DOCTYPE
echo() but now i got the same erorr msg but where i write in a file
"fwrite($fileh,$file_array[$i]);" and this part is very important and i
can't remove it. how to solve this problem

  #5  
Old July 17th, 2005, 10:57 AM
Amit
Guest
 
Posts: n/a
Default Re: to redirect to another page



please check any echo or print statement(function that send contents to
the broswer) in current file or include files.

if this do not resolve your problem use output buffer functions to
control output.
usefule output buffer functions : ob_start(), ob_end_flush().
see also:
http://groups-beta.google.com/group/php_programming

  #6  
Old July 17th, 2005, 10:57 AM
Matthias Esken
Guest
 
Posts: n/a
Default Re: to redirect to another page

soham@mic.gov.eg wrote:
[color=blue]
> on line 9 there was "<?php" and i removed all the "<HTML>", or DOCTYPE
> echo() but now i got the same erorr msg but where i write in a file
> "fwrite($fileh,$file_array[$i]);" and this part is very important and i
> can't remove it. how to solve this problem[/color]

Give us the first lines of your code up to the row where you use
header().

Regards,
Matthias
  #7  
Old July 17th, 2005, 10:57 AM
iMedia
Guest
 
Posts: n/a
Default Re: to redirect to another page

If you are having allot of trouble with this, try buffering the output
by using:
"ob_start()" and "ob_flush()"...
See ref: http://us2.php.net/manual/en/function.ob-start.php

  #8  
Old July 17th, 2005, 10:58 AM
soham@mic.gov.eg
Guest
 
Posts: n/a
Default Re: to redirect to another page

This is my code:

<?php
$file_name=$_GET['filen'];
$vd_flag=$_GET['vd'];
$count_file=realpath("./count.txt");
$file_array=file($count_file);
$num=count($file_array);
$found=0;

for ($i=0;$i<$num;$i++){
if (strpos($file_array[$i], $file_name)===false)
//echo 'Not found';
else{
list($filen,$view,$down)=split("\t",$file_array[$i]);
$view=(int)$view;
$down=(int)$down;
if ($vd_flag==1){
$viewc=++$view;
$downc=$down;
}//if view is choosen
elseif ($vd_flag==2){
$viewc=$view;
$downc=++$down;
}//if download is choosen
$file_array[$i]=$file_name."\t".$viewc."\t".$downc."\n";
$found=1;
break;
}//end if file name found in the array
}//end for loop
if ($found==0){
if ($vd_flag==1){
$viewc=1;
$downc="0";
}//if view is choosen
elseif ($vd_flag==2){
$viewc="0";
$downc=1;
}//if download is choosen
$file_array[$num]=$file_name."\t".$viewc."\t".$downc."\n";
}//file name not found
$numi=++$num;
if ($fileh=fopen($count_file,"w")){
if (flock($fileh,2)){
for ($i=0;$i<$numi;$i++){
fwrite($fileh,$file_array[$i]);
}//end for loop
}
else{
header("Location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/direct.php?filen=$file_name&vd=$vd_flag");
exit();
}
}// write the array to the file
fclose($fileh);
header("Location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/images/$file_name");
exit();
?>

  #9  
Old July 17th, 2005, 10:58 AM
Ged Robinson
Guest
 
Posts: n/a
Default Re: to redirect to another page

I had trouble with this error, then I saw someone recommend using another
way to perform the redirection, for example, like this....

$url=$HTTP_REFERER;
print("<meta http-equiv=\"refresh\" content=\"3;URL=$url\">");

Your url would be
"http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/images/$file
_name" of course.

If you replace the 3 with a 0 the redirection occurs straight away.

I don't understand enough to know whether using this method can be dangerous
or not recommended for some reason. Maybe not all browsers support it ???

This is what I use anyway. It avoids the "headers already sent" error
you're seeing.

Anyone got any comments on it ??

Ged


<soham@mic.gov.eg> wrote in message
news:1102682022.674813.185140@z14g2000cwz.googlegr oups.com...[color=blue]
> I want to redirect to another file, isn't this the right code to do so
>
>[/color]
header("Location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']
)."/images/$file_name");[color=blue]
>
> the interpreter give me this erorr instead of redirecting
>
> Warning: Cannot modify header information - headers already sent by
> (output started at E:\tryphp\direct.php:9) in E:\tryphp\direct.php on
> line 70
>
> what's wrong in the code coz i don't understand the erorr
>[/color]


  #10  
Old July 17th, 2005, 10:58 AM
John Dunlop
Guest
 
Posts: n/a
Default Re: to redirect to another page

Ged Robinson wrote upside-down:
[color=blue]
> print("<meta http-equiv=\"refresh\" content=\"3;URL=$url\">");[/color]

Groups-beta (spit) will tell you why the META refresh hack
is usually inferior; it is no substitute for a proper
redirection.

--
Jock
  #11  
Old July 17th, 2005, 10:58 AM
Michael Fesser
Guest
 
Posts: n/a
Default Re: to redirect to another page

.oO(Ged Robinson)
[color=blue]
>I had trouble with this error, then I saw someone recommend using another
>way to perform the redirection, for example, like this....
>
>$url=$HTTP_REFERER;
>print("<meta http-equiv=\"refresh\" content=\"3;URL=$url\">");[/color]

The correct way for redirects is sending an appropriate status code back
the browser. Additionally the HTTP referrer is unreliable.
[color=blue]
>I don't understand enough to know whether using this method can be dangerous
>or not recommended for some reason. Maybe not all browsers support it ???[/color]

You should read this:

Use standard redirects: don't break the back button!
http://www.w3.org/QA/Tips/reback

Micha
  #12  
Old July 17th, 2005, 10:58 AM
Ged Robinson
Guest
 
Posts: n/a
Default Re: to redirect to another page

Thanks for that Micha.

Now I understand why it's not a good technique.

G

"Michael Fesser" <netizen@gmx.net> wrote in message
news:gjtmr09inerc4md0pkkgpicfpap8u8nmml@4ax.com...[color=blue]
> .oO(Ged Robinson)
>[color=green]
> >I had trouble with this error, then I saw someone recommend using another
> >way to perform the redirection, for example, like this....
> >
> >$url=$HTTP_REFERER;
> >print("<meta http-equiv=\"refresh\" content=\"3;URL=$url\">");[/color]
>
> The correct way for redirects is sending an appropriate status code back
> the browser. Additionally the HTTP referrer is unreliable.
>[color=green]
> >I don't understand enough to know whether using this method can be[/color][/color]
dangerous[color=blue][color=green]
> >or not recommended for some reason. Maybe not all browsers support it[/color][/color]
???[color=blue]
>
> You should read this:
>
> Use standard redirects: don't break the back button!
> http://www.w3.org/QA/Tips/reback
>
> Micha[/color]


  #13  
Old July 17th, 2005, 10:58 AM
soham@mic.gov.eg
Guest
 
Posts: n/a
Default Re: to redirect to another page

i didn't want to use the meta method but could any one tell me how to
do a redirect. now my problem lies in the fwrite() function when i run
the script it give me a warning on that part where i try to write in a
file. i tried to use ob_start() and ob_end_flush() but it didn't work.
please i need any help urgently.

  #14  
Old July 17th, 2005, 10:59 AM
Soha
Guest
 
Posts: n/a
Default Re: to redirect to another page

please i need any help urgently.

  #15  
Old July 17th, 2005, 10:59 AM
vilain@spamcop.net
Guest
 
Posts: n/a
Default Re: to redirect to another page

In article <1102934873.648283.16080@f14g2000cwb.googlegroups. com>,
"Soha" <soham@mic.gov.eg> wrote:
[color=blue]
> please i need any help urgently.[/color]

http://us2.php.net/manual/en/function.header.php

--
DeeDee, don't press that button! DeeDee! NO! Dee...



  #16  
Old July 17th, 2005, 11:00 AM
Soha
Guest
 
Posts: n/a
Default Re: to redirect to another page

i'm already using the header() function but i have a problem when i try
to write in the file before using the header() function. i tried to use
ob_start() and ob_end_flush() but still the script return the same
error
Warning: Cannot modify header information - headers already sent
by(output started at E:\tryphp\direct.php:9) in E:\tryphp\direct.php on
line 70

  #17  
Old July 17th, 2005, 11:00 AM
Michael Fesser
Guest
 
Posts: n/a
Default Re: to redirect to another page

.oO(Soha)
[color=blue]
>i'm already using the header() function but i have a problem when i try
>to write in the file before using the header() function. i tried to use
>ob_start() and ob_end_flush() but still the script return the same
>error
>Warning: Cannot modify header information - headers already sent
>by(output started at E:\tryphp\direct.php:9) in E:\tryphp\direct.php on
>line 70[/color]

Upload a copy of the script with the extension .phps and post a link to
it.

Micha
  #18  
Old July 17th, 2005, 11:00 AM
vilain@spamcop.net
Guest
 
Posts: n/a
Default Re: to redirect to another page

In article <1102950738.939643.259400@z14g2000cwz.googlegroups .com>,
"Soha" <soham@mic.gov.eg> wrote:
[color=blue]
> i'm already using the header() function but i have a problem when i try
> to write in the file before using the header() function. i tried to use
> ob_start() and ob_end_flush() but still the script return the same
> error
> Warning: Cannot modify header information - headers already sent
> by(output started at E:\tryphp\direct.php:9) in E:\tryphp\direct.php on
> line 70[/color]

I'll bet somewhere in your script _before_ you call the header()
function, you've echo'ed some sort of output. That sends a header.
Make sure header() is the first thing you call.

--
DeeDee, don't press that button! DeeDee! NO! Dee...



  #19  
Old July 17th, 2005, 11:01 AM
Soha
Guest
 
Posts: n/a
Default Re: to redirect to another page

i haven't "echo'ed some sort of output" but i have used the fwrite()
function and this is the problem. i tried to use the ob_start() and
ob_end_flush() but still it didn't work. please need help as soon as
possible and for more information i have post my code in a previouse
message.

  #20  
Old July 17th, 2005, 11:01 AM
Michael Fesser
Guest
 
Posts: n/a
Default Re: to redirect to another page

.oO(Soha)
[color=blue]
>i haven't "echo'ed some sort of output" but i have used the fwrite()
>function and this is the problem.[/color]

fwrite() shouldn't be the problem, it writes to local disk and doesn't
cause a server response.
[color=blue]
>i tried to use the ob_start() and
>ob_end_flush() but still it didn't work.[/color]

Where did you add ob_start()? It should be one of the first things in a
script. What is on the line the interpreter complains about ('output
startet at ...')?
[color=blue]
>please need help as soon as
>possible and for more information i have post my code in a previouse
>message.[/color]

Posted code doesn't show if there's something (blanks, new lines) before
an opening <?php in your file, which could cause the server to send its
headers.

Micha
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles