Archive for category ‘PHP’

2010
24
May
Category: Mac, PHP

I use MAMP daily. I love MAMP. I recently discovered how truly awesome MAMP Pro is too. But that’s a story for another time. I recently upgraded from MAMP 1.8.4 to 1.9. When I got MAMP 1.9 installed I started the servers. Apache came up but MySQL didn’t want to. I tried shutting down both servers and starting it back up again, which didn’t work.

Thankfully, there is a pretty simple fix. To begin with, make sure MAMP is shut down. Next, open Terminal. To open Terminal either open Spotlight (Cmd+Space) and type “terminal” or you can find Terminal in the /Applications/Utilities/ folder.

Once you get Terminal opened, type the following:

ps aux | grep mysql 
lsof -i 
killall -9 mysqld

That’s it. Start up MAMP and MySQL will come back up just like normal. Another option would be to simply change the port number MySQL is using in MAMP. Restarting your computer may work too but I did’t try that.

2010
06
Mar
Category: Mac, PHP, Website

This took me three days to figure out so hopefully this saves someone else a little bit of time.

The most common use of .htaccess files is removing file extensions, such as index.php, from a website URL. They are much ore powerful than that though. I’m not going to go into the details about what .htaccess files can do, shouldn’t be used for, best practices, or anything else like that. I’m going to show you how to get .htaccess files set up and working with MAMP.

The only requirements are that you have a Mac (that’s the first ‘M’ in MAMP) and the MAMP software already installed. I am using the latest version. MAMP will install everything else you need (Apache, MySQL, and PHP which is ‘AMP’ in MAMP) to get up and running so you can start making your own sites locally.

First and foremost, the location of your website files in MAMP need to go in /Applications/MAMP/htdocs/ That may sound like common sense but that’s what took the longest for me to figure out. I had my files in /Applications/MAMP/bin/mamp/ which is where all of the files for the start page is located. I just made a folder inside there and was going to the site at http://localhost:8888/MAMP/myfolder No matter what I did, the .htaccess file would not work at all from within that directory. So make sure your websites files are in /Applications/MAMP/htdocs/ or a folder within.

The next thing you need to know is ‘htaccess’ isn’t the file extension and there isn’t a file name on the file. The dot at the front typically means it is a hidden file on Unix based operating systems (which the Mac is). So you will need to show the hidden files on your machine. There are a few ways to do this. One of the easiest ways is to use a Dashboard widget. Hidden Files Widget is a great widget for this. If the hidden files are hidden, the button will say ‘Show’. If the hidden files are visible, the button will say ‘Hide’. It can’t get any more simple that that. Install the widget then click the ‘Show’ button. Sometimes the desktop icons won’t come back automatically. If they don’t come back after a minute, click on the Finder icon in the dock and they should come back. Now, go to wherever your website files are located. If there is a .htaccess file in the folder, skip the next paragraph. If it is not there, read the next paragraph.

Here is the catch. It’s tough to create a .htaccess file because the Mac thinks ‘htaccess’ is the file extension but you didn’t give the file a name. So it won’t just let you do it. Wordpress creates a .htaccess file for you when you set up a blog locally. I believe Drupal does the same thing but I’m not 100% positive on that. Other applications possibly do as well. If you aren’t installing a CMS like that, you can always create a custom one and download it. Below is a very basic example of a .htaccess file that I typically use. Once you have a .htaccess file, put it in the root of where your website will live.

The .htaccess file can be opened with TextEdit. You can open it with Dreamweaver or almost anything you want. Here is an example of a typical .htaccess file

1
2
3
4
5
6
7
8
9
10
11
# BEGIN My_Website
<IfModule mod_rewrite.c>
	RewriteEngine On
	#RewriteBase /
	RewriteBase /mywebsite/
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule . /index.php [L]
	#RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
# END My_Website

The hash marks (#) are comments. I’ll talk a little about the second and third commenta in a second.

If you are developing a site locally, your files may not be in the root of the /Applications/MAMP/htdocs/ folder since you may be working on more than one website. That is what the RewriteBase is for. It specifies where the root of your website is in accordance with the root of the domain or local root. In this example, my website and it’s files are in a folder called mywebsite which is inside my /Applications/MAMP/htdocs/ folder. Since /Applications/MAMP/htdocs/ is my root folder, I don’t have to put that in the path. I just have to put the path from the root folder. If you notice, on the line above that (line 4), I have commented out that line with a hash. When you upload the files to your website, uncomment this line (line 4) and comment out the line after it (line 5). That way the .htaccess file works when you get it on the actual site. If you are not uploading the files to the root of your website, set the path accordingly.

The third comment (line 9) is a variation of the line above it (line 8). Both work pretty much the same. You don’t have to have the first and last comment lines. You can delete them and it won’t effect anything.

That is literally all I had to do to get the site working. I saw sites that wanted me to edit httpd.conf files and php.ini files and all kinds of files under the sun. I didn’t have to though. If you do need to mess with those files, there are a couple pretty good walkthroughs to help you out here and here.

2009
20
Oct
Category: CodeIgniter, PHP

I was playing with CodeIgniter routing at work. I wanted to have a catch-all route but I needed to exclude a certain controller, or controllers, from that catch-all route. As an example, I wanted to have http://www.example.com/(ANYTHING) route to the “page” controller. http://www.example.com/policies/shipping would route to the “policies” controller and then to the “ship” method. From there the “page” controller would split up all of the segments.

I started out with this:

$route['policies/shipping'] = "policies/ship";
$route['(:any)'] = "page";

The “policies/ship” route would be caught by the “policies” controller and everything else would be caught by the “page” controller. Except the “page” controller was getting everything.

I needed to exclude certain controllers from the catch-all route. After a Google search, I came up with nothing. So I started working on it myself. Here is what I cam up with:

$route['policies/shipping'] = "policies/ship";
$route['^(?!policies|controllerA|controllerB)\S*'] = "page";

So far it seems to work perfectly. Hopefully this helps someone else out.

2009
13
Oct
Category: Mac, PHP

I am using MAMP for local PHP development. It’s alright. It has a tendency to not want to cooperate sometimes. Like this morning. I loaded up MAMP and Apache started just fine but MySQL didn’t want to do anything.

If you are getting an error similar to “Error: Could not connect to MySQL server!”, rather than having to reinstall MAMP (which sometimes doesn’t work), here’s a fix:

  1. Quit MAMP
  2. Open Terminal (Applications/Utilities/Terminal.app)
  3. Type “killall -9 mysqld” (without the quotes)
  4. Start MAMP

It should be working now. If not, you may have to sudo the command (“sudo killall -9 mysqld”)

Another option is to go to Appliacations/MAMP/db/mysql/ and delete anything is is NOT a folder. I didn’t have to use this option so I can’t vouch for it myself. According to forum postings it works though.

2009
26
Sep
Category: Misc, PHP

I have been playing with Codeigniter a lot lately. I was in the middle of working on something when MAMP gave me a 403 error.

The error was “403 Forbidden You don’t have permission to access / on this server” Navigating to any folder would give me the 403 error as well, even http://localhost:8888/MAMP/?language=English.

This was a pretty simple issue to correct. Not simple enough at the time though

  1. Stop all MAMP servers (Apache and MySQL Server)
  2. Navigate to Applications/MAMP/conf/apache
  3. Open httpd.conf
  4. You can probably open it with TextEdit. I opened it with Dreamweaver. Just make sure TextEdit is editing it in plain text format and not rich text format.
  5. Find the following section of code (on mine it was on line 378):
    <Directory />
    Options Indexes FollowSymLinks
    AllowOverride All
    </Directory>
  6. Change “AllowOverride All” to “AllowOverride None
  7. Save the file
  8. Start MAMP and test the pages.

If that doesn’t work for you, you can always Google it. The MAMP forums have a lot of posts about it.