LIGHTENING YOUR WORKLOAD WITH INCLUDES The two warning (Web hosting india)

LIGHTENING YOUR WORKLOAD WITH INCLUDES The two warning messages about the missing (or misnamed) file are helpful in a development context, as they tell you exactly what the problem is. In a live website, though, they not only look unprofessional, but also reveal potentially useful information about your site structure to malicious users. It s quite simple to prevent this sort of mess from appearing onscreen. The quick and easy way is to use the PHP error control operator (@), which suppresses error messages associated with the line of code in which it s used. You place the error control operator either at the beginning of the line or directly in front of the function or command that you think might generate an error. So the error messages shown in Figure 4-11 could be eliminated like this: @ include(’includes/randomimage.php’); The error control operator is extremely useful, but without error messages, you re often left with no idea why a script isn t working. Insert it only after you are sure everything s OK. When troubleshooting, the @ mark should be the first thing you remove. Put it back after you have identified the problem. The error control operator affects only the cur- rent line. You need to use it in every line that might generate an error message. A more sophisticated approach involves the following two steps: Always check that a file exists and is readable before attempting to include it. Always check the existence of variables or functions defined in external files before attempting to use them. PHP Solution 4-7 ( Adding a caption to the random image ) implemented both steps: first using file_exists() and is_readable() to check whether an image is accessible, and then using isset() to test whether a variable exists. So the errors in Figure 4-11 could be eliminated like this: $file = ‘includes/randomimage.php’ if (file_exists($file) && is_readable($file)) { include($file); } Storing the name of the include file in a variable avoids the need to retype its pathname three times. It also means you need to correct the spelling mistake in only one place. To check whether a custom-built function has been defined, pass the name of the function as a string to function_exists() like this: if (function_exists(’myFunction’)) {myFunction();} The name of the function being tested must be in quotes and without the final parentheses. function_exists(’myFunction’) // correct function_exists(myFunction()) // wrong 113
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Leave a Reply