to redirect to another page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • soham@mic.gov.eg

    #1

    to redirect to another page

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

    header("Locatio n:http://".$_SERVER['HTTP_HOST'].dirname($_SERV ER['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\direc t.php:9) in E:\tryphp\direc t.php on
    line 70

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

  • Jan Pieter Kunst

    #2
    Re: to redirect to another page

    soham@mic.gov.e g wrote:[color=blue]
    > I want to redirect to another file, isn't this the right code to do so
    >
    > header("Locatio n:http://".$_SERVER['HTTP_HOST'].dirname($_SERV ER['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\direc t.php:9) in E:\tryphp\direc t.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" = @.

    Comment

    • Gordon Burditt

      #3
      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("Locati on:http://".$_SERVER['HTTP_HOST'].dirname($_SERV ER['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\direc t.php:9) in E:\tryphp\direc t.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

      Comment

      • soham@mic.gov.eg

        #4
        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

        Comment

        • Amit

          #5
          Re: to redirect to another page



          please check any echo or print statement(funct ion 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:


          Comment

          • Matthias Esken

            #6
            Re: to redirect to another page

            soham@mic.gov.e g 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

            Comment

            • iMedia

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

              Comment

              • soham@mic.gov.eg

                #8
                Re: to redirect to another page

                This is my code:

                <?php
                $file_name=$_GE T['filen'];
                $vd_flag=$_GET['vd'];
                $count_file=rea lpath("./count.txt");
                $file_array=fil e($count_file);
                $num=count($fil e_array);
                $found=0;

                for ($i=0;$i<$num;$ i++){
                if (strpos($file_a rray[$i], $file_name)===f alse)
                //echo 'Not found';
                else{
                list($filen,$vi ew,$down)=split ("\t",$file_arr ay[$i]);
                $view=(int)$vie w;
                $down=(int)$dow n;
                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("Locatio n:http://".$_SERVER['HTTP_HOST'].dirname($_SERV ER['PHP_SELF'])."/direct.php?file n=$file_name&vd =$vd_flag");
                exit();
                }
                }// write the array to the file
                fclose($fileh);
                header("Locatio n:http://".$_SERVER['HTTP_HOST'].dirname($_SERV ER['PHP_SELF'])."/images/$file_name");
                exit();
                ?>

                Comment

                • Ged Robinson

                  #9
                  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_REFE RER;
                  print("<meta http-equiv=\"refresh \" content=\"3;URL =$url\">");

                  Your url would be
                  "http://".$_SERVER['HTTP_HOST'].dirname($_SERV ER['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.goo glegroups.com.. .[color=blue]
                  > I want to redirect to another file, isn't this the right code to do so
                  >
                  >[/color]
                  header("Locatio n:http://".$_SERVER['HTTP_HOST'].dirname($_SERV ER['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\direc t.php:9) in E:\tryphp\direc t.php on
                  > line 70
                  >
                  > what's wrong in the code coz i don't understand the erorr
                  >[/color]


                  Comment

                  • John Dunlop

                    #10
                    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

                    Comment

                    • Michael Fesser

                      #11
                      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_REF ERER;
                      >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

                      Comment

                      • Ged Robinson

                        #12
                        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.ne t> wrote in message
                        news:gjtmr09ine rc4md0pkkgpicfp ap8u8nmml@4ax.c om...[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_REF ERER;
                        > >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]


                        Comment

                        • soham@mic.gov.eg

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

                          Comment

                          • Soha

                            #14
                            Re: to redirect to another page

                            please i need any help urgently.

                            Comment

                            • vilain@spamcop.net

                              #15
                              Re: to redirect to another page

                              In article <1102934873.648 283.16080@f14g2 000cwb.googlegr oups.com>,
                              "Soha" <soham@mic.gov. eg> wrote:
                              [color=blue]
                              > please i need any help urgently.[/color]



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



                              Comment

                              Working...