Jump to content

Fluoresce

Members
  • Posts

    279
  • Joined

  • Last visited

Everything posted by Fluoresce

  1. Any feedback? How does it look? What do you think of the layout, the colour scheme, the general idea? I know the look needs to be improved. It looks a bit amateur. Working on it.
  2. Well, after months of hard work, I think that my website's finally finished. All it needs is lots of high-quality content. Constructing the website has been very educational. I've had to relearn so much HTML, CSS, Photoshop, PHP and MySQL, much with the help of you guys. I've also had to learn lots of stuff that I didn't know, much of it concerning mod_rewrite, htaccess, php.ini, analytics, HTTP, error logging, and spam prevention. It's been fun! So what do you guys think of the finished product? The link is in my signature. Please give it to me straight.
  3. Okay, here's what I've got now (and I'm quite proud of myself!). This goes at the top of the page, above the HTML: <?php // Create a function that throws an exception. function handle_mysql_error($err) { throw new Exception($err); } // Code to try. If it fails, it will be caught by the 'catch' block. try { $conn = mysql_connect("localhost", "user", "password") or handle_mysql_error("Connection failed! "); mysql_select_db("database") or handle_mysql_error("Select database failed! "); // Select data from database. $sql = "SELECT * FROM `table`"; $query = mysql_query($sql, $conn) or handle_mysql_error("Query failed! "); } catch(exception $e) { // Set appropriate header for bots. header('HTTP/1.1 503 Service Temporarily Unavailable'); // Initialise user-friendly message to be displayed in body for user. $userErrorMessage = "<p>Sorry, this service is temporarily unavailable. The webmaster has been notified.</p>"; // Log the error in the error log file. trigger_error($e->getMessage() . mysql_error(), E_USER_WARNING); // Send yourself an email. error_log($e->getMessage() . "Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personalemail@example.com", "From: help@mysite.com"); } ?> And this goes in the body of the HTML: if(isset($userErrorMessage)) { echo $userErrorMessage; } else { while($row = mysql_fetch_assoc($query)) { // Do whatever. } } One problem remains: If there's an error and the site's very popular, I will be sent thousands of e-mails in a short period of time! Apart from that, it's quite cool, right? I've checked it and it works perfectly.
  4. Thanks! Here's what I've got so far: function mysql_error_handling(){ throw new Exception(); } try { $con = mysql_connect('localhost','user','password') or mysql_error_handling("Connection failed! "); mysql_select_db('database') or mysql_error_handling("Select database failed! "); $query = mysql_query('SELECT * FROM `table`', $con) or mysql_error_handling("Query failed! "); } catch(exception $e) { // Display user-friendly message to user. echo "<p>Sorry, an error has occurred. The webmaster has been notified.</p>"; // Log error in file. trigger_error($e->getMessage() . mysql_error(), E_USER_WARNING); // Send yourself an email. Make sure that ignore_repeated_errors // is set to On to prevent multiple e-mails on popular website. error_log($e->getMessage() . date() . $_SERVER['REQUEST_URI'], 1, "myemail@example.com"); } // Continue rest of script. For example: while($results = mysql_fetch_assoc($query)) { // Do something. } How does that look? You said that I shouldn't send myself an e-mail if there's a MySQL error because, if my website's popular, I'll receive 1,000s of e-mails. Can't I prevent this by simply setting ignore_repeated_errors in my php.ini file to On?
  5. $conn = mysql_connect("localhost", "", ""); if(!$conn) { // Send yourself an e-mail. error_log("MySQL connection failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); // Redirect user to error page. header("HTTP/1.1 503 Service Temporarily Unavailable"); header("Location: mysql-error.php"); die(); } $selectdb = mysql_select_db(""); if(!$selectdb) { // Send yourself an e-mail. error_log("MySQL select database failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); // Redirect user to error page. header("HTTP/1.1 503 Service Temporarily Unavailable"); header("Location: mysql-error.php"); die(); } $query = mysql_query("", $conn); if(!$query) { // Send yourself an e-mail. error_log("MySQL failed failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); // Redirect user to error page. header("HTTP/1.1 503 Service Temporarily Unavailable"); header("Location: mysql-error.php"); die(); } That seems over the top, too. How do you guys handle MySQL errors?
  6. Thanks! That looks like some advanced stuff! I will learn how to do it. For now, shall I just use this? (Note that my site's new, so I don't have much traffic. The prospect of receiving lots of e-mails in the event of an error is currently non-existent.) $conn = mysql_connect("localhost", "", ""); if(!$conn) { // Send yourself an e-mail. error_log("MySQL connection failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); // Redirect user to error page. header("HTTP/1.1 503 Service Temporarily Unavailable"); header("Location: mysql-error.php"); die(); } $selectdb = mysql_select_db(""); if(!$selectdb) { // Send yourself an e-mail. error_log("MySQL select database failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); // Redirect user to error page. header("HTTP/1.1 503 Service Temporarily Unavailable"); header("Location: mysql-error.php"); die(); } $query = mysql_query("", $conn); if(!$query) { // Send yourself an e-mail. error_log("MySQL failed failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); // Redirect user to error page. header("HTTP/1.1 503 Service Temporarily Unavailable"); header("Location: mysql-error.php"); die(); }
  7. How do you do that? An outline would be very much appreciated. Sounds good! Trying to learn how to do this now. Thanks for warning me about redirecting and e-mailing.
  8. I am currently handling MySQL errors like this: $conn = mysql_connect("", "", ""); if(!$conn) { // Present message to user. echo "<p>Sorry, there has been an error. The webmaster has been informed.</p>"; // Send yourself an e-mail. error_log("MySQL connection failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); } else { $selectdb = mysql_select_db(""); if(!$selectdb) { // Present message to user. echo "<p>Sorry, there has been an error. The webmaster has been informed.</p>"; // Send yourself an e-mail. error_log("MySQL selectdb failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); } else { $query = mysql_query("", $conn); if(!$query) { // Present message to user. echo "<p>Sorry, there has been an error. The webmaster has been informed.</p>"; // Send yourself an e-mail. error_log("MySQL query failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); } else { // Rest of code. } } } The code works. If there's an error with mysql_connect(), mysql_select_db() or mysql_query(), I am sent an e-mail and the user is presented with a message. If the error is with mysql_connect(), the error is also logged. However, the code looks very unprofessional and over the top. It makes my scripts look complicated. What's the best way to deal with MySQL errors? Is this better? $conn = mysql_connect("localhost", "", ""); if(!$conn) { // Send yourself an e-mail. error_log("MySQL connection failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); // Redirect user to error page. header("Location: mysql-error.php"); die(); } $selectdb = mysql_select_db(""); if(!$selectdb) { // Send yourself an e-mail. error_log("MySQL select database failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); // Redirect user to error page. header("Location: mysql-error.php"); die(); } $query = mysql_query("", $conn); if(!$query) { // Send yourself an e-mail. error_log("MySQL failed failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", "From: help@mysite.com"); // Redirect user to error page. header("Location: mysql-error.php"); die(); }
  9. Thanks, but it's not working: PHP Notice: Undefined variable: error_text in ... The email is sent, but the string (error message) that I enter into my_error_log() doesn't appear in the email. I tried putting $error_text = ''; both inside and outside the function. Although the notice goes away when I try it inside the function, the error message that I enter into my_error_log() still doesn't appear in the email.
  10. Good stuff! Okay, here's what I want to do. If a MySQL connection or query fails in the future, I want a user-friendly message to be presented to the user, a log to be made of the error, and an e-mail to be sent to me. Here are my php.ini settings: error_reporting = E_ALL display_errors = Off log_errors = On error_log = [nameoffile] Here's what I've got so far: // Prepare headers for e-mail $headers = "From: help@mysite.com\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; // Connect to MySQL $con = mysql_connect("", "", ""); if(!$con) { echo "<p>User-friendly message for user.</p>"; error_log("MySQL connection failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@whatever.com", $headers); } else { // Rest of code } The code works. The user-friendly message is presented to the user, a log is made of the error, and an e-mail is sent to me specifying the data and time of the error and the file in which the error occurred. Does it look okay to you guys? How would you do it?
  11. Okay, I'm confused now. I don't know how I should be handling possible future mysql_connect() errors. Here's what I've just discovered ... If I use the following code and the connection fails, the user is presented with a MySQL error message (even though display_errors is turned off), the script dies (so the user only gets half a page), and 4 warnings are logged (1 for the failure to connect, 2 for the failure to select the database, and 1 for the failure of the MySQL query). $con = mysql_connect("localhost", "", ""); // Rest of code On the other hand, if I use the following code and the connection fails, the user is presented with a custom message, the script doesn't die (so the user gets a full page), and 1 warning is logged (for the failure to connect). $conn = mysql_connect("localhost", "", ""); if(!$conn) { echo "<p>Custom error message for user.</p>"; } else { // Rest of code } I haven't had to use trigger_error() at all. Is that the best way to deal with possible future connection failures? I ask because even the PHPFreaks website says to use this: mysql_connect("localhost", "", "") or trigger_error("Connection failed! " . mysql_error()); // Rest of code If the second bit of code is the best way to deal with future connection problems, my question is, should I use the same if/else logic for mysql_select_db() and mysql_query() as well? Wouldn't my scripts then be packed with if/else conditionals? And is it all even necessary? What's the chance that the connection, database selection or query will fail?
  12. Here are the php.ini settings for my production site: error_reporting = E_ALL display_errors = Off log_errors = On error_log = error_log I've just done a bit of newbie experimentation. An error is logged if mysql_connect() fails even without trigger_error(). In other words, for logging, the trigger_error() part of the following code is not necessary. $conn = mysqli_connect(...) or trigger_error('Failed to connect to MySQL', E_USER_WARNING); If you use the code above, two warnings will be logged, one by the system and one by your use of trigger_error(). Am I mistaken? I guess this means that trigger_error() is to be used when errors aren't automatically thrown, and whatever error constant you use depends on what you think is appropriate. Is that correct?
  13. What I don't understand is which constant I am supposed to use, and when. It doesn't tell you in the manual. I don't want my scripts to die and present half a page to users, so I know that I shouldn't use E_USER_ERROR. What, then, should I use if mysql_connect(), mysql_select_db() or mysql_query() fail? Should I use E_USER_WARNING, or should I leave the default E_USER_NOTICE? Does it really matter? For example, should I do this for MySQL queries? $query = mysql_query("Some query"); if(!$query) { echo "Message to user."; trigger_error("Error!", E_USER_WARNING); // Message to be logged }
  14. I thank you all very much! Since different variations of preg_match() were presented and I don't understand that function yet, I decided to use this: if(substr($requestURI, 0, 10) == '/articles/') { // Do something }
  15. Thanks. I've already done that. This is something different. How can I get the code to execute no matter what appears after /articles/?
  16. $requestURI = $_SERVER['REQUEST_URI']; if($requestURI == '/articles/1') { // do something } I want the above code to be executed if the page URI is /articles/1, /articles/2, /articles/3, etc. Does PHP have a character placeholder to put in place of "1" so that the code will execute no matter what number appears after /articles/? I've checked online but couldn't find one.
  17. Thanks, but I've already checked that stuff out. It's quite confusing for me. Do you mean if error_reporting is turned on? Can you explain how to use the predefined constants (e.g., E_USER_ERROR), please?
  18. Whenever there's a PHP or MySQL error on my production site, I want the error to be logged in a file. I have found a file called error_log in my site's root folder containing previous PHP and MySQL errors, so error logging is already working. But I want to understand how and why it's working. I checked my php.ini file: error_reporting = E_ALL & ~E_NOTICE display_errors = On display_startup_errors = Off ; Log errors into a log file (server-specific log, stderr, or error_log (below)) ; As stated above, you're strongly advised to use error logging in place of ; error displaying on production web sites. log_errors = On; ; Set maximum length of log_errors. In error_log information about the source is ; added. The default is 1024 and 0 allows to not apply any maximum length at all. log_errors = On; ; Log errors to specified file. error_log = error_log; ; Log errors to syslog (Event Log on NT, not valid in Windows 95). error_log = error_log; Question 1 How can there be two each of log_errors = On and error_log = error_log? Question 2 Will errors be logged in the error_log file no matter what, or do I have to use die()/trigger_error() in my code? In other words, if my database is down, will all of these lines log errors? mysql_query("SELECT * FROM `table` WHERE date IS NULL"); mysql_query("SELECT * FROM `table` WHERE date IS NULL") or die(mysql_error()); mysql_query("SELECT * FROM `table` WHERE date IS NULL") or trigger_error(mysql_error()); Question 3 If I put E_USER_ERROR in trigger_error(), will only that type of errors be logged?
  19. I'm confused. There's lots of conflicting advice here. I don't know anything about logs or errors. This is interesting! Questions 1. I have raw access logs in cPanel. Are these the logs that you guys are talking about, or are there other, PHP-related logs? 2. If I use trigger_error(), will the error show up in the raw access logs? 3. Some of you have said to keep die(). Wouldn't this terminate the script and present only half the page to the user? 4. How exactly do I make sure that the error is logged and the user is presented with a clear message on a complete page?
  20. And if I should have if(!$con) { echo "<p>Error! Could not connect to the database. Reason: " . mysql_error() . "</p>"; } else { // Rest of code } after every mysql_connect(), shouldn't I also have it after every mysql_select_db() and mysql_query()?
  21. I see. Okay, so should I have something like this: $con = mysql_connect("localhost", "", ""); if(!$con) { echo "<p>Error! Could not connect to the database. Reason: " . mysql_error() . "</p>"; } else { // Rest of code } Or, since the connection works during testing, should I just have this: $con = mysql_connect("localhost", "", ""); // Rest of code
  22. Sorry, silly question! I actually tried selecting a row that doesn't exist before asking that question. There was no error, which is why I asked it.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.