Hi All,

I am trying to sort out some custom error pages for my site and wondered if there was a way of adapting the .htaccess file used in plain HTML? Would I have to create a separate page for each error? Or if using a database, would I be able to create one error.php page, that would display the relevant information according to the error encountered?

Is there an alternative to .htaccess?

I have an .htaccess file like this

ErrorDocument 400 http://localhost/errors.php?errorType=400
ErrorDocument 401 http://localhost/errors.php?errorType=401
ErrorDocument 403 http://localhost/errors.php?errorType=403
ErrorDocument 404 http://localhost/errors.php?errorType=404
ErrorDocument 500 http://localhost/errors.php?errorType=500
ErrorDocument 501 http://localhost/errors.php?errorType=501
ErrorDocument 502 http://localhost/errors.php?errorType=502
ErrorDocument 503 http://localhost/errors.php?errorType=503

and this PHP in my error page document

<?php 

$colname_rsErrors = "1";
if (isset($_POST['errorType'])) {
  $colname_rsErrors = (get_magic_quotes_gpc()) ? $_POST['errorType'] : addslashes($_POST['errorType']);
}

mysql_select_db($database_ftc, $ftc);
$query_rsErrors = sprintf("SELECT * FROM ftc_errors WHERE errorType =%s", $colname_rsErrors);
$rsErrors = mysql_query($query_rsErrors, $ftc) or die(mysql_error());
$row_rsErrors = mysql_fetch_assoc($rsErrors);
$totalRows_rsErrors = mysql_num_rows($rsErrors);
?>

I am sure it can be done, but being rather new to PHP I am getting stumped!

Thanks

Phil
www.12csv.com

    ee12csvt wrote:

    Would I have to create a separate page for each error?

    You wouldn't have to, no. The .htaccess sample you posted should work fine - have you tried it?

    ee12csvt wrote:

    Is there an alternative to .htaccess?

    Well.. the IIS webserver has its own GUI that allows you to configure error document pages... other than that, I don't think so (other than modifying your webserver's config file).

    Other than that... there are a few suggestions I would make, such as using %d instead of %s in your sprintf() format. Also, you shouldn't use addslashes() when dealing with SQL queries. The first thing you should ever do when working with PHP is make sure magic_quotes is disabled (not just in code - I mean actually disable it in PHP's config file as well). Then, use [man]mysql_real_escape_string/man when sanitizing data for SQL queries. Also, is there a reason you use mysql_num_rows()? Seems like errorType should be a numeric column with a PRIMARY (or UNIQUE at least) key, in which case you'd either get 1 or 0 rows, so there'd be no point in counting them.

    Finally, you never output anything in your PHP error page, so I'm not sure what the goal is exactly.

      Hi bradgrafelman,

      Thanks for the reply, I have tried the .htaccess could and it wouldn't reproduce anything.

      I used DreamweaverMX to create the site, the output page includes this text

      Unfortunately an error has occurred when you have attempted to visit our site. You have probably not done anything wrong, it may be a server error, or the page you are looking for has been moved.<br><br>
        Please try again, if this page reappears, contact us using the 'Contact Form' to report the problem. Some of the possible problems are listed below.
        <br><br><h1><?php echo $row_rsError['errorTitle']; ?></h1><br>
        <?php echo $row_rsError['errorText']; ?>
      	<br><br>
      <div id="return"><a href="#top" onmouseover="window.status='Click here to return to the top of the page';return true" onmouseout="window.status='';return true">Return to top of this page.</a></div>

      I have changed the PHP in the header to this

      <?php require_once('Connections/ftc.php'); ?>
      <?php
      mysql_select_db($database_ftc, $ftc);
      $query_rsError = "SELECT * FROM ftc_errors WHERE errorType = '%d'";
      $rsError = mysql_query($query_rsError, $ftc) or die(mysql_error());
      $row_rsError = mysql_fetch_assoc($rsError);
      $totalRows_rsError = mysql_num_rows($rsError);
      ?>

      I have tried to reproduce the error page again on my apache server and it seems to work, but a '404' error does not show the information that I have stored in the database in the entry for errorType '404', etc.

      I am pretty new to PHP/MySQL and I am sure that I am doing something wrong that is probably so simple :-(

        Write a Reply...