Thanks very much for that. Amending preg_replace_all to preg_replace solved the problem when DEBUG_MODE is set to false in app_config.php. (preg_replace_all does have a sort of function it appears but in this case the author has admitted it was a typo). When DEBUG_MODE is set to true, I get my old friend
Cannot modify header information
! Now, I'm familiar with some of the causes of this but I can't see how to solve it here.
I'm happy to repost a separate query but as I suspect could be solved by your most useful information on htmlentities etc, I've taken the liberty of posting here first.
The full error is
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ptahmeuk_phil'@'localhost' (using password: YES) in /home/ptahmeuk/public_html/exercises/connect.php on line 6
(I would expect this - I've seeded an error in connect.php).
Warning: Cannot modify header information - headers already sent by (output started at /home/ptahmeuk/public_html/exercises/connect.php:6) in /home/ptahmeuk/public_html/exercises/scripts/app_config.php on line 36
The relavent files are.
1)connect.php
Code: Select all
<?php
require_once 'scripts/app_config.php';
// DATABASE_PASSWORD "foo"
mysql_connect(DATABASE_HOST, DATABASE_USERNAME, "foo")
or handle_error("there was a problem connecting to the database that holds the information we need to get you conected. ",
mysql_error());
// DATABASE_PASSWORD
echo "<p>Connected to MySQL!</p>";
mysql_select_db(DATABASE_NAME)
or die("<p>Error selecting the database " . DATABASE_NAME . mysql_error() . "</p>");
echo "<p>Connected to MySQL, using database " . DATABASE_NAME . ".</p>";
$result = mysql_query("SHOW TABLES;");
if (!$result) {
die("<p>Error in listing tables: " . mysql_error() . "</p>");
}
echo "<p>Tables in database:</p>";
echo "<ul>";
while ($row = mysql_fetch_row($result)) {
echo "<li>Table: {$row[0]}</li>";
}
echo "</ul>";
?>
2. app_config.php
Code: Select all
<?php
//Setup debug mode
define("DEBUG_MODE", true);
// Site root
define("SITE_ROOT", "/exercises/");
// Database connection constants
define("DATABASE_HOST", "");
define("DATABASE_USERNAME", "");
define("DATABASE_PASSWORD", "");
define("DATABASE_NAME", "");
// Error reporting
if (DEBUG_MODE) {
error_reporting(E_ALL);
} else {
// Turn off all error reporting
error_reporting(0);
}
function debug_print($message) {
if (DEBUG_MODE) {
echo $message;
}
}
function handle_error ($user_error_message, $system_error_message) {
$query = array(
"error_message" => $user_error_message,
"system_error_message" => $system_error_message
);
header("Location: " . SITE_ROOT . "scripts/show_error.php?" . http_build_query($query));
exit();
}
?>
3. show_error.php
Code: Select all
<?php
require_once 'app_config.php';
if (isset($_REQUEST['error_message']))
{
$error_message =
preg_replace("/\\\\/", '',
$_REQUEST['error_message']);
} else {
$error_message = "something went wrong, and that's " .
"how you ended up here.";
}
if
(isset($_REQUEST['system_error_message']
)) {
$system_error_message =
preg_replace("/\\\\/", '',
$_REQUEST['system_error_message']); }
else {
$system_error_message = "No system-level error message was reported.";
}
?>
<html>
<head>
<link href="../css/phpMM.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header"><h1>PHP & MySQL: The Missing Manual</h1></div>
<div id="example">Example 4-1</div>
<div id="example">Uh oh....sorry!</div>
<div id="content">
<h1>We're really sorry...</h1>
<p><img src="../images/error.jpg"
class="error" />....but something's gone wrong. Apparently, <?php echo htmlentities ($error_message); ?></span></p><p> Don't worry, though, we've been notified that there's a problem , and we take these things seriously. In fact, if you want to contact us to find out more about what's happened or if you have any concerns, just <a href="mailto:info@ptah.me.uk">email us</a> and we'll be happy to get back to you</p>
<p>In the meantime, if you want to go back to the page that caused the problem, jou can do that <a href="javascript:history.go(-1);">by clicking here.</a> If the same problem occurs, though, you may want to come back a bit later. We bet we'll have things sorted out by then. Thanks again ... we'll see you soon. And again, we're really sorry for the inconvenience.</p>
</div>
</body>
<?php
debug_print("<hr />");
debug_print("<p>The following system-level message was received:
<b>(" . htmlentities($system_error_message) . "</b></p>");
?>
You will see I've used some of the changes you suggested. stripslashes I've heard of but would like to research further.
Regards
Philip