By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

to redirect to another page

P: n/a
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

Jul 17 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
so***@mic.gov.eg wrote:
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


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, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #2

P: n/a
>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


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

Jul 17 '05 #3

P: n/a
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

Jul 17 '05 #4

P: n/a


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

Jul 17 '05 #5

P: n/a
so***@mic.gov.eg wrote:
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


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

Regards,
Matthias
Jul 17 '05 #6

P: n/a
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

Jul 17 '05 #7

P: n/a
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();
?>

Jul 17 '05 #8

P: n/a
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
<so***@mic.gov.eg> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Jul 17 '05 #9

P: n/a
Ged Robinson wrote upside-down:
print("<meta http-equiv=\"refresh\" content=\"3;URL=$url\">");


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

--
Jock
Jul 17 '05 #10

P: n/a
.oO(Ged Robinson)
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\">");
The correct way for redirects is sending an appropriate status code back
the browser. Additionally the HTTP referrer is unreliable.
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 ???


You should read this:

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

Micha
Jul 17 '05 #11

P: n/a
Thanks for that Micha.

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

G

"Michael Fesser" <ne*****@gmx.net> wrote in message
news:gj********************************@4ax.com...
.oO(Ged Robinson)
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\">");
The correct way for redirects is sending an appropriate status code back
the browser. Additionally the HTTP referrer is unreliable.
I don't understand enough to know whether using this method can be dangerousor not recommended for some reason. Maybe not all browsers support it

???
You should read this:

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

Micha

Jul 17 '05 #12

P: n/a
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.

Jul 17 '05 #13

P: n/a
please i need any help urgently.

Jul 17 '05 #14

P: n/a
In article <11*********************@f14g2000cwb.googlegroups. com>,
"Soha" <so***@mic.gov.eg> wrote:
please i need any help urgently.


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

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

Jul 17 '05 #15

P: n/a
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

Jul 17 '05 #16

P: n/a
.oO(Soha)
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


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

Micha
Jul 17 '05 #17

P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>,
"Soha" <so***@mic.gov.eg> wrote:
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


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...

Jul 17 '05 #18

P: n/a
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.

Jul 17 '05 #19

P: n/a
.oO(Soha)
i haven't "echo'ed some sort of output" but i have used the fwrite()
function and this is the problem.
fwrite() shouldn't be the problem, it writes to local disk and doesn't
cause a server response.
i tried to use the ob_start() and
ob_end_flush() but still it didn't work.
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 ...')?
please need help as soon as
possible and for more information i have post my code in a previouse
message.


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
Jul 17 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.