Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
The .htaccess File4:52 with Alena Holligan
Especially on a shared server, you don't always have access to modify the php.ini file. Using a .htaccess file will allow you to set error level at a directory level and also show parse errors, which stop a php script from processing.
WARNING! Be careful that your error handling does not get pushed to production.
.htaccess File Settings
php_flag display_startup_errors on php_flag display_errors on php_value error_reporting -1 php_flag html_errors on
How to show hidden files in Mac OS X
The Terminal application is the best way to display hidden files in Mac OS X. The Terminal app enables you to control Mac OS X using the command line to enter Unix commands. We like to think of Terminal as the hood of a car; it allows you to open Mac OS X up and look inside.
Open Terminal (click Go > Utilities and double-click the Terminal app)
Now copy and paste both lines listed below into Terminal one at a time, and press Return after each line:
defaults write com.apple.finder AppleShowAllFiles YES killall Finder
How to show hidden files on Windows
- Open Folder Options by clicking the Start button , clicking Control Panel, clicking Appearance and Personalization, and then clicking Folder Options.
- Click the View tab.
- Under Advanced settings, click Show hidden files, folders, and drives, and then click OK.
An htaccess file allows you to change the configuration of your server 0:00 at a directory level. 0:05 This is often the best choice when on a shared server 0:07 where you don't have access to the php.ini file. 0:10 This will overwrite what's in your php.ini file or other configuration files. 0:14 We can use this to turn on, off, or change the level of errors to display. 0:19 Keep in mind .htaccess files are hidden files and 0:25 can sometimes be tricky to work with. 0:29 If you're not sure how to work with hidden files, check the teachers' notes for 0:31 more information. 0:35 If you are using this method, you should be aware 0:37 that the htaccess file is often used for other things such as redirection. 0:40 It is also another file used right along 0:45 with the rest of the files on your application. 0:48 Because of these concerns you should be careful about 0:51 committing this file to your version control system. 0:54 Most likely, you do not want the same settings on dev and production. 0:57 Let's go into work spaces and see the htaccess file in action. 1:02 One of the things I have included in this project are a couple error pages. 1:07 To start with, let's preview errors1.php. 1:11 Click the preview icon that looks like an eye in the top right corner. 1:14 Preview always takes us to the default page of our project. 1:19 If there is no default page, it gives us a list of pages. 1:24 We can click on errors1.php. 1:28 We'll see that all we get is a blank white page, not very helpful. 1:32 And if we go to errors2.php it gives us a server error. 1:37 Again, no help. 1:41 Let's go into the htaccess file and turn on errors. 1:43 In workspaces we can't edit an htaccess file directly, 1:49 because it doesn't understand the .htaccess as a file extension. 1:53 But if we rename it briefly to htaccess.txt, 1:58 then we can open the file, edit it, end then rename it back. 2:01 Because Workspaces is set up as a development environment, 2:14 it is set to display all errors by default. 2:17 We're using the htaccess file to turn off the errors. 2:20 In the htaccess file that's included in Workspace, you'll see quite a few lines. 2:24 All the lines that start with a pound sign are just comments 2:29 telling you what's going on. 2:33 So there are really only four lines of actual code. 2:34 This is not PHP code. 2:38 The htaccess file is a configuration file for Apache, our web server. 2:40 We'll step through each line. 2:45 The first line tells our server, Apache, to display startup errors. 2:48 Our second line tells our server to display all other errors. 2:53 The third line specifies reporting of all PHP errors. 2:58 And the fourth line turns on HTML markup of errors making them easier to read. 3:03 If you want to go into this in a little more depth check out the teacher's notes. 3:10 We'll change all of the off to on. 3:16 We'll also change error_reporting to minus one. 3:25 That means everything. 3:29 If we save this file we can then rename it. 3:31 Back to .htaccess. 3:40 Now we can refresh our browser to see our parse error. 3:43 It tells us that there's an unexpected closing curly brace at line nine. 3:47 It's expecting a comma or a semicolon. 3:52 Let's go check it out. 3:56 Errors two on line nine. 3:59 Because of the way PHP pretty much ignores whitespace, it doesn't actually generate 4:03 an error until it encounters something that is incorrect. 4:08 So we need to look at the line directly before line nine. 4:11 Since line eight is a blank line, that means we want line seven. 4:16 We can see that we are indeed missing a semicolon. 4:21 Let's fix that. 4:24 Now let's refresh our browser. 4:29 Awesome. 4:32 No more errors. 4:33 We'll fix our other page in the next video. 4:35 If you don't have access to your server this is often your best option. 4:38 Just be careful that your development settings do not get pushed to production. 4:42 This brings us to our third option, your own PHP code. 4:48
You need to sign up for Treehouse in order to download course files.Sign up