The php.ini File6:04 with Alena Holligan
When possible, it's usually best to modify the php.ini file on your webserver. This will allow you to turn on all errors and display in development, while locking down the display of errors on production. Using the php.ini file will also allow you to see parse errors that will stop a php script from processing.
Modify you php.ini file
- While your server is running, go to the phpinfo file in a browser I'm using http://localhost/phpinfo.php
Search for “php.ini”. You should see something like “Configuration File (php.ini) Path”. Examples:
You can open the php.ini file in a text editor or in a terminal. Copy the path and open a terminal window.
MAC: Type sudo vi and then paste the path with php.ini at the end
sudo vi /Applications/MAMP/bin/php/php7.0.0/conf/php.ini W
Windows: Open notepad first then choose File > Open or copy the path and open a terminal window, example
Search for error_reporting:
MAC: Type a forward slash to search /error_reporting
Windows: Edit > Find, error_reporting
- error_reporting: Set the error reporting level. The default value is: “E_ALL & ~E_NOTICE,” meaning this this would should all error levels except E_NOTICE. For a development server you probably want to turn everything on by just using E_ALL.
- display_errors: Set it to “ON” (default is “OFF”)if errors are to be printed to the screen.
- log_errors: This indicates whether or not error logging should be done; default value is “on.”
- error_log string: Sets the name of the file where script errors should be logged
There are a couple additional settings that will give you more information or control.
- display_startup_errors: For debugging, you can also turn on errors that occur during PHP's startup sequence
- html_errors: You can also control HOW your errors as displayed. The new format for HTML errors produces clickable messages that direct the user to a page describing the error, or function causing the error. This is on by default but you can turn it off
After saving the file, you must restart the servers for the changes to take affect.
If you have a development server where you test your site before 0:00 launching it to the world you should configure your development server to show 0:03 all your PHP errors. 0:08 However a production server that's live for 0:10 the world to see should never display PHP system errors to the screen. 0:12 There are more secure and helpful ways to inform your site's visitors of a problem. 0:18 For example, you can catch an error or 0:24 exception and display a message to the user. 0:26 For more information on catching exceptions, check out the teacher's notes. 0:30 By setting up your development server to display errors, 0:35 you'll have a consistent setup for all of your projects as you work on them. 0:38 The first thing you need to know is where your php.ini file is located. 0:43 There are some standard locations for this file. 0:47 But there is an easy way to find out for sure. 0:50 The PHP info function will get the PHP environment's details for 0:54 the specific project you're running. 0:59 I've included a phpinfo.php file in the downloads. 1:01 But you can put the call to the PHP info function in any PHP file you want. 1:06 Let's take a look at our setting. 1:11 Here's what our PHP info file looks like. 1:13 I set this up to run on the local host. 1:16 While your server's running go to the PHP info file in a browser. 1:20 I'm using localhost/phpinfo.php. 1:24 Do a search for php.ini. 1:30 You should see something like Configuration File (php.ini) Path. 1:33 My path is Applications/MAMP/bin/php/php7.0.0/conf. 1:39 You can open the php.ini file in a text editor. 1:47 But I prefer to use the terminal. 1:51 I'm gonna copy this path so i can paste it into a terminal window. 1:53 We'll type sudo vi, then paste the path, 1:59 and finish with php.ini. 2:04 You'll have to type in your password for this computer. 2:07 To search in vi, type a forward slash and then the search term. 2:12 /error_reporting =. 2:16 The lines starting with the semicolon are all comments. 2:23 You can see our error reporting here. 2:27 By default, it's set to E_ALL & ~E_NOTICE, 2:30 meaning that this would show all error levels except E_NOTICE. 2:34 For a development server, 2:40 you probably want to turn everything on by just using E_ALL. 2:41 We type I to start editing the line. 2:48 We can then delete all this and hit Escape. 2:52 If we scroll down a little bit you'll see that display errors is off by default. 2:57 We wanna turn that on for our development server. 3:03 Again, I, Delete, On, Escape. 3:07 Going down a little more, we see our display start up errors. 3:16 For debugging, 3:22 you can turn on the errors that occur during PHP startup sequence as well. 3:22 So let's do that. 3:27 I to edit, Delete, 3:31 On, Escape. 3:35 Logging errors. 3:38 By default, our logging errors is turned on. 3:41 But now let's search for error log string. 3:45 /error_log =. 3:48 And we can see by default our error log is stored in 3:56 Applications/MAMP/logs/php_error.log. 4:00 You could change this if you want. 4:04 There's one additional setting I wanna show, HTML errors. 4:08 By default HTML errors is turned on. 4:16 HTML errors controls how your errors are displayed. 4:21 The format for HTML errors produces clickable messages 4:25 that direct the user to a page describing the error or function causing the error. 4:28 This is on by default, but you can turn it off. 4:34 To save this file we hit :w. 4:38 And then to quit, :q. 4:43 After saving the file, you must restart the servers for 4:47 the changes to take effect. 4:50 Using the php.ini file allows us to set different configurations for 4:58 different environments. 5:02 Remember, we want to show the most errors in a development environment, and 5:04 suppress errors in a production environment. 5:09 Error reporting itself should be turned on, but 5:13 you should turn off display errors on production. 5:16 You could also have separate error reporting for QA environments. 5:20 Maybe you want to suppress notices there so 5:25 they don't interfere with what people see. 5:28 However you choose to set up your environments, placing your configuration 5:30 settings in the php.ini file keeps them separate from your code. 5:34 However, depending on your server, you may not have access to the php.ini file. 5:39 Most shared hosting accounts don't give you access to this file. 5:45 In that case, you'll need to use one of the other options. 5:49 Workspaces is set up as a development environment to display all errors. 5:53 However, you can use one of the other two options 5:58 to override these settings as well. 6:01
You need to sign up for Treehouse in order to download course files.Sign up