18

I have a page that displays various elements even if the id it's calling from the database does not exist or was deleted (which throws up all sorts of ugly errors along with search engines continuing to list non-existent pages).

Can you modify the first part of the page code shown below to send a 404 (or at least to projecterror.php which has 404 headers) if $id does not exist? Many thanks!

<?php
include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
while ($row = mysql_fetch_array($qselect)) { 

The following modification as kindly suggested by Matt Wilson as a result of an original comment by Vivek Goel results in valid entries showing the page correctly but non-existent pages are showing the errors below this modified code:

<?php
include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
if( mysql_num_rows( $qselect ) === 0 )
{
   header("HTTP/1.1 301 Moved Permanently");
   header( 'Location: http://examplesite.domain/errorpage' ) ;
   exit;
}
while ($row = mysql_fetch_array($qselect)) { 

Errors resulting from the above modifications:

Warning: Cannot modify header information - headers already sent by (output started at /home/website/public_html/header1.php:14) in /home/website/public_html/header1.php on line 22 
Warning: Cannot modify header information - headers already sent by (output started at /home/website/public_html/header1.php:14) in /home/website/public_html/header1.php on line 23 Lines 22 and 23 are the two header lines in your example above

Lines 22 and 23 are the two header lines as below:

header("HTTP/1.1 301 Moved Permanently");
header( 'Location: http://examplesite.domain/errorpage' ) ;
Community
  • 1
  • 1
JoeW
  • 578
  • 1
  • 10
  • 29

3 Answers3

48

I have much easier solution for you - it is simple! Just add this command at the very start of the php source:

ob_start();

This will start buffering the output, so that nothing is really output until the PHP script ends (or until you flush the buffer manually) - and also no headers are sent until that time! So you don't need to reorganize your code, just add this line at the very beginning of your code and that's it :-)

Tomas
  • 57,621
  • 49
  • 238
  • 373
2

before sending any thing to view (text in the browser). check if your model that if id is valid or not . if id is not valid redirect to your error page

header("HTTP/1.1 301 Moved Permanently");
header( 'Location: http://examplesite.domain/errorpage' ) ;
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186
  • you are sending some text before putting header. this things will come before sending any view. (any thing to browser ) – Vivek Goel Aug 08 '11 at 05:16
  • Where is the text though? I can't work this out... – JoeW Aug 08 '11 at 08:13
  • is it your full page code ? or partial code – Vivek Goel Aug 08 '11 at 08:14
  • project.php is the main page which has this at the top (header1.php is the code written above): `` – JoeW Aug 08 '11 at 08:45
  • I guess include_once("header1.php"); is sending header and data . – Vivek Goel Aug 08 '11 at 08:51
  • The code from header1.php down to where we ended in the first post is this: `` ...followed by the code in the first post here. – JoeW Aug 08 '11 at 09:05
  • session_start(); should not be called if it is 301 redirect. and are you sure you are not sending any o/p to browser from these files ? – Vivek Goel Aug 08 '11 at 09:19
  • I just tried removing all the included files except for config.php which is needed for the database user/pass etc. - I also removed session_start() which also didn't work. The error continues to fall on the 2x header lines. – JoeW Aug 08 '11 at 09:27
  • there is much easier solution, see my answer – Tomas Aug 08 '11 at 15:33
0

No content should be sent to the browser before using php header() redirection. You can try javascript redirection instead if headers already sent.

if( mysql_num_rows( $qselect ) === 0 )
{
   echo "<script type='text/javascript'>window.location.href='{http://examplesite.domain/errorpage}'</script>";
   exit;
}

But I am not sure javascript redirects are search engine friendly

Withfriendship Hiox
  • 1,287
  • 2
  • 8
  • 5