Good day! I make a site. On one page I made a redirect to another page using header. I get a warning:

Warning: Cannot modify header information - headers already sent by (output started at S:\home\localhost\www\web_shop\admin_header.php:97) in S:\home\localhost\www\web_shop\add_good.php on line 71 

In the admin_header.php file, I have the following code fragment (I quote code from 96 lines):

 <ul> <?php $result = List_categories(""); while ($row = $result->fetch_assoc()) { echo '<li><a href="admin.php?cat='.$row['id_cat'].'">'.$row['Name_cat'].'</a></li>'; } ?> </ul> 

And in the file add_good.php on line 71 the following code begins:

 header("http://localhost/web_shop/admin.php"); exit(); 

The structure of the site is as follows: The config file (in which the session opens on the first line) connects to admin_header.php. The last file in turn connects to the admin.php and add_good .php files. What am I doing wrong? Where is the mistake?

  • 3
    Firstly, the header is wrong, secondly, the header must be sent to any output. so they wrote to you, this one <<ul> 'is output earlier than header () - zb'
  • But how can I redirect to another page in my case? Is it really impossible to go to another page from the middle of the page? - IntegralAL

3 answers 3

Correctly send the header as follows:

 header("Location: <адрес переадресации>"); 

I read from one source that you can use it only if nothing was displayed on the page before using it. The presence of even one space on the page will lead to an error.

The same rule applies to the setcookie () and session_start () functions. Using JS, you can use redirection from any position of the code, regardless of whether something is displayed on the screen or not. Here is the short code in JS:

 echo '<script type="text/javascript"> window.location = "admin.php" </script>'; 

And forwarding using php methods can only be done before outputting anything. In another case, there are two options: use the ob_start() and ob_end_flush() functions or HTML methods.

  • one
    It is always cheaper to just send a redirect header than to give the user a full page, albeit in a very truncated form. - Indifferent
  • through means js earned !!! Hooray! Echo code '<script type = "text / javascript"> window.location = "admin.php" </ script>'; suitable for solving problems when redirects come from the middle of the page, when the page and headings have already appeared. - IntegralAL

Add to the top of the admin_header.php file

 <?php $result = List_categories(""); $list = ''; while ($row = $result->fetch_assoc()) { $list .= '<li><a href="admin.php?cat='.$row['id_cat'].'">'.$row['Name_cat'].'</a></li>'; } ?> 

This part with 96 lines

 <ul> <?php $result = List_categories(""); while ($row = $result->fetch_assoc()) { echo '<li><a href="admin.php?cat='.$row['id_cat'].'">'.$row['Name_cat'].'</a></li>'; } ?> </ul> 

replace with

 <ul> <?=$list;> </ul> 

and, of course, correctly send an http header (man http://php.net/manual/ru/function.header.php). In the add_good.php file

 header("http://localhost/web_shop/admin.php"); 

replaced by

 header('Location: http://localhost/web_shop/admin.php'); 
  • This option will not work. The fact is that I still have a lot of similar code in admin_header.php and in add_good.php itself before the page is redirected. We'll have to redo all the code because of this ... Some other option is needed. - IntegralAL
 <?php ob_start(); 

at the beginning of the main script

 ob_end_flush(); 

in the end.

And redirect the header to die ();

 die(header('Location: ' . URL)); 

A crutch, but better than redirecting with js.

  • and than redirection by means of js is bad? You also look like the code does not look ice. - IntegralAL
  • it all worked. Thank you for another version of work forwarding. Although it is still a crutch too. - IntegralAL