Single-Page Password Protection

Warning: This article/tutorial is more the 45 days old. As such the information contained within could be, by now, out of date. Please read all information to make sure that this article/tutorial will work with your current version of the Operating System

First let’s build a skeleton.



Private Page


First, we will introduce the $password variable. Set this to anything you like. For the tutorial, we will use “phpmac”.

We will then display a password form if it has not already been submitted.



Private Page

Enter the Password:


So now we have $password set and a form being displayed if it has not yet been submitted. Now, we will add an else to the conditional and inside that else and a second conditional that will compare the user’s input to $password.



Private Page

Enter the Password:

Secret content!


And that’s it. If you would like, you may also display the password form again if it is incorrect. This is shown below:



Private Page

Enter the Password:

Secret content!

Enter the Password:


Writing Form Data to a MySQL Database using PHP

Alert!: Warning: This article/tutorial is more the 45 days old. As such the information contained within could be, by now, out of date. Please read all information to make sure that this article/tutorial will work with your current version of the Operating System

In this tutorial, we will make a form and then submit what is entered to a MySQL database, and learn how to carry values between pages.

Let’s make a skeleton:


<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>

</body>
</html>

Now, we need to make our form!
We will leave the action blank because it is the same as the page itself. The method is post.


<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<form action=”” method=”post”>
Name: <input type=”text” name=”name”><br>
Favorite Color: <input type=”text” name=”color”><br>
<input type=”submit” name=”submit” value=”Submit!”>
</form>
</body>
</html>

Ok, so we now have our form set up. Now we have to learn conditionals in PHP. This is where the real power of PHP comes into play. The basic conditional follows this format:


if ($variable == “string”) {
echo “foo”;
} else {
echo “bar”;
}

The above code tells PHP to find the value of $variable, and compare it to “string”. If the value of $variable is indeed “string”, then the script will do whatever is between the following brackets. In this case, it will print “foo”. Otherwise, it will print “bar”.

You can also stick something between the if and the else. This is an “elseif”.


if ($variable == “string”) {
echo “Foo”;
} elseif ($variable == “secondstring”) {
echo “foobar”;
} else {
echo “bar”;
}

The elseif works like a second resort. If $variable does not equal “string”, then it will see if the value of $variable is instead equal to “secondstring”. If it does, then it executes the code between the brackets. If it doesn’t, then it goes to the else. You can have an unlimited number of elseifs in a conditional.

Here are some basic things that you can use as tests in your conditionals (the code between the parentheses in the if() part):

FYI $var == “string” – This tests if the variable matches the string exactly
$var == 1 – This tests if the variable matches the number exactly
$var != “string” – This tests of the variable does NOT match the string
$var != 1 – This tests of the variable does NOT match the number
isset($var) – this tests if the variable exists and contains a value.
!isset($var) – This tests if the variable does NOT exist
$var – This tests if the variable is set to a “true” value (1 or true)
!$var – This tests if the variable is NOT set to a “true” value (1 or true)

In conditionals, you can end and recommence the PHP code between the brackets as you like. Example:


<?php
$foo = “potato”;

if (isset($foo)) {
?>
This variable exists.
<?php
} else {
?>
This variable does <i>not</i> exist.
<?php
}
?>

This would print “This variable exists.” because of the fact that $foo is set to “potato”. If we hadn’t set it at the beginning, then the script would print “This variable does not exist.”

Now, we will move on to $_POST global variables.
When a PHP script is the action of a form with a “post” method, the inputted data is set as $_POST variables.
For example, if this was our form:


<form action=”send.php” method=”post”>
<input type=”text” name=”cheese”><br>
<input type=”submit” name=”submit” value=”Submit”>
</form>

then send.php would automatically have TWO variables set.

These are:
$_POST[‘cheese’] and $_POST[‘submit’].

The $_POST global variable follows this format:
$_POST[‘inputname’]

When a submit button is clicked, its $_POST variable is automatically set. Remember how we test that with a conditional?
Let’s assume I entered “swiss” in the field named “cheese” and I clicked submit. We will make the action, send.php, test to see whether or not the form was submitted. Then, we will print what I entered in the cheese field.


<?php
if (isset($_POST[‘submit’])) {
echo $_POST[‘cheese’];
}
?>

Ok, so now we will put this to use in our main page.
We will use a !isset conditional to display the form only if it has not been submitted already.


<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST[‘submit’])) {
?>
<form action=”” method=”post”>
Name: <input type=”text” name=”name”><br>
Favorite Color: <input type=”text” name=”color”><br>
<input type=”submit” name=”submit” value=”Submit!”>
</form>
<?php
}
?>
</body>
</html>

So now the form is only displayed if it has not been submitted already.

We will now begin to learn the act of submitting our form data to our MySQL database.

Let’s start by setting our database host, username, password, and name as variables at the top.
I will be using the following as my database information:
Host: localhost
Username: lefteh
Password: 1234
Name: MyDB

We’ll set the variables before anything else, just for clarity’s sake.


<?php
$db_host = “localhost”;
$db_user = “lefteh”;
$db_pwd = “1234”;
$db_name = “MyDB”;
?>
<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST[‘submit’])) {
?>
<form action=”” method=”post”>
Name: <input type=”text” name=”name”><br>
Favorite Color: <input type=”text” name=”color”><br>
<input type=”submit” name=”submit” value=”Submit!”>
</form>
<?php
}
?>
</body>
</html>

Now, how to connect to a MySQL database:


mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);

This code connects to your database using the first three variables, and then it selects the database that is the name of your fourth variable.

Let’s add that to our page.


<?php
$db_host = “localhost”;
$db_user = “lefteh”;
$db_pwd = “1234”;
$db_name = “MyDB”;
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST[‘submit’])) {
?>
<form action=”” method=”post”>
Name: <input type=”text” name=”name”><br>
Favorite Color: <input type=”text” name=”color”><br>
<input type=”submit” name=”submit” value=”Submit!”>
</form>
<?php
}
?>
</body>
</html>

Now we have a page that is connected to our database and displays a form if the same has not yet been submitted.
But what if it has been submitted?

Here, we will add an “else” clause.


<?php
$db_host = “localhost”;
$db_user = “lefteh”;
$db_pwd = “1234”;
$db_name = “MyDB”;
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST[‘submit’])) {
?>
<form action=”” method=”post”>
Name: <input type=”text” name=”name”><br>
Favorite Color: <input type=”text” name=”color”><br>
<input type=”submit” name=”submit” value=”Submit!”>
</form>
<?php
} else {

}
?>
</body>
</html>

So now, we need to do two things:
1. Write the inputted data into the database
2. Display a success message

Writing data to a MySQL database in PHP is actually quite easy:


mysql_query(“INSERT INTO `tablename` (fieldOne, fieldTwo) VALUES (‘$varone’, ‘$vartwo’)”);

That’s all! Let’s implement it in our code. We will assume that our database table name is “colors” and the fields are named “name” and “favoriteColor”. Remember our $_POST variables? We’ll set those to basic variables for simplicity’s sake and use them as the input variables. Some versions of browsers, servers, and PHP automatically do this. Just to be on the safe side, we will manually do it.


<?php
$db_host = “localhost”;
$db_user = “lefteh”;
$db_pwd = “1234”;
$db_name = “MyDB”;
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST[‘submit’])) {
?>
<form action=”” method=”post”>
Name: <input type=”text” name=”name”><br>
Favorite Color: <input type=”text” name=”color”><br>
<input type=”submit” name=”submit” value=”Submit!”>
</form>
<?php
} else {
$name = $_POST[‘name’];
$color = $_POST[‘color’];
mysql_query(“INSERT INTO `colors` (name, favoriteColor) VALUES (‘$name’, ‘$color’)”);
}
?>
</body>
</html>

Good!

Now all we need to do is add a little success message:


<?php
$db_host = “localhost”;
$db_user = “lefteh”;
$db_pwd = “1234”;
$db_name = “MyDB”;
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST[‘submit’])) {
?>
<form action=”” method=”post”>
Name: <input type=”text” name=”name”><br>
Favorite Color: <input type=”text” name=”color”><br>
<input type=”submit” name=”submit” value=”Submit!”>
</form>
<?php
} else {
$name = $_POST[‘name’];
$color = $_POST[‘color’];
mysql_query(“INSERT INTO `colors` (name, favoriteColor) VALUES (‘$name’, ‘$color’)”);
echo “Success! Your favourite colour has been added!”;
}
?>
</body>
</html>

And that’s all there is to it!

Building and Installing Apache 2 and PHP 5.1.1 on Mac OS X 10.4.3 Tiger

By: James Pelow (jamesnp) Posted: 29-Nov-05, 07:49:40 Rated: General/Experienced

Alert!: Warning: This article/tutorial is more the 45 days old. As such the information contained within could be, by now, out of date. Please read all information to make sure that this article/tutorial will work with your current version of the Operating System

 

FYI Usual stuff… Full Developers’ Tool installation is required as well as up to date system. At this time it means 10.4.3 WITH ALL latest Security Updates.

For anyone that has been wishing to switch to Apache 2 on their Mac, this is the time to do it. PHP 5 has also recently been updated to 5.1.1 and this tutorial describes in detail, the best method to both build and install these programmes on your Mac.

Since Apple don’t include Apache 2 with OS X yet I have felt it better to keep Apache 2 in its own directory, and to keep PHP for Apache 2, etc. confined the that directory too.

Step 1: Downloading Apache 2----------The first step, as in any build like this, is to download the source and uncompress it. To do this we open the Terminal and enter the following commands. FYI: the % represents the Command Line Prompt and should not be entered

% curl -O http://apache.mirrors.esat.net/httpd/httpd-2.0.55.tar.gz
% gnutar -xzf httpd-2.0.55.tar.gz

Step 2: Building and Installing Apache 2----------I’ve decided to install to a new directory which we will make /apache2. It’s nice and convenient. To do this enter the following commands (sudo is required as the directory doesn’t already exist):

% cd httpd-2.0.55
% sudo ./configure \
% –prefix=/apache2 \
% –enable-module=most \
% –enable-shared=max
% sudo make
% sudo make install

Step 3: Building and Installing PHP----------Building and Installing PHP with Apache 2 is fairly simple. We want to install it into the same directory as Apache… just for sanity’s sake.

% curl -O http://ie.php.net/distributions/php-5.1.1.tar.gz
% gnutar -xzf php-5.1.1.tar.gz
% cd php-5.1.1
% sudo mkdir /apache2/php

Once all that’s done, we will configure and compile PHP.

% ./configure \
% –prefix=/apache2/php \
% –with-zlib \
% –with-xml \
% –with-gd \ (optional, requires jpeg + png)
% –with-jpeg-dir=/usr/local \ (optional)
% –with-png-dir=/usr/local \ (optional)
% –with-mysql=/usr/local/mysql \ (optional)
% –with-apxs2=/apache2/bin/apxs
% make
% sudo make install

The lines marked “Optional” are optional. lib jpeg and png are to enable jpeg and png support in GD Lib – you will need LibJPEG and LibPNG installed previously to enable GD. The MySQL line is if you need MySQL. You will need MySQL installed and running.

If you want a PHP.ini file then enter this. It’s not required though.

% sudo cp php.ini-dist /usr/local/lib/php.ini

Step 4: Configuring Apache----------The next bit is to configure Apache to load PHP files properly. Add it below the file’s current contents. The httpd.conf file is located at /apache2/conf/httpd.conf

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.php

Also, You now have a choice. By default your new document root will be /apache2/htdocs. If you want to change this back to the Apple default of /Library/WebServer/Documents you need to open the httpd.conf file (/apache2/conf/httpd.conf) and change the following:

<Directory “/apache2/htdocs”>

to

<Directory “/Library/WebServer/Documents”>

Similarly, Apache 2 will now point to the UNIX standard user directory. Which means if you type http://::1/~user/ it will point to the ~/public_html directory. In Mac OS X the usual standard is ~/Sites. If you wish to change this you need to access the httpd.conf file once again and change

UserDir public_html

to

UserDir Sites

Step 5: Starting and Stopping Apache 2----------If all went well you should now have a working Apache 2 installation. However, this will not work with the Web Sharing option in System Preferences. Before you continue, please make sure the option in System Preferences is set to “Off”.

To start Apache 2:

sudo /apache2/bin/apachectl start

To stop Apache 2:

sudo /apache2/bin/apachectl stop

You will need to switch this on everytime your restart your Mac. Or, you can use another PHPmac tutorial to enable Apache 2 as the default OS X server. Additional Useful Apache2 Config Settings.

Hope all goes well… if, however, you hit a wall or you need help, however trivial, please contact support. Click Here. Please note; a PHPmac login is required.

-James Pelow
PHPmac


Some frequent Issues:

Q: I recieve the following error message while running the Apache configure command:

Creating Makefile in src
+ configured for Darwin platform
Error: could not find any of these C compilers
anywhere in your PATH: gcc cc acc c89
Configure terminated

A: You need to install the Apple supplied Developers’ Tools. These come on a CD in your Mac OS X box or in the Applications folder if you’ve just bought your Mac recently

Q: I received an error message regarding the absence of LibXML2

A: Building and Installing libXML2 (Required for PHP 5)

libXML2 is now required by PHP. The building of libXML2 is releativly simple, but it takes for ever and a day to compile. So be prepared to go and make yourself a coffee and maybe dinner. Most people should have this, it was included in an Apple Security update a while back. However, many people have been encountering this problem so I have added directions here.

First step is to download the source then Untar it. We will make a directory to do this:

% cd ~/Desktop
% mkdir libxml2build
% cd libxml2build
% curl -O ftp://ftp.gnome.org/pub/GNOME/sources/libxml2/2.6/libxml2-2.6.7.tar.gz
% gnutar -xzf libxml2-2.6.7.tar.gz
% cd libxml2-2.6.7

We are now in the libxml2-2.6.7 source directory. From here it’s a simple compile and install.

% ./configure
% make
% sudo make install
% sudo ranlib /usr/local/lib/libxml2.a

libXML2 is now installed. Go Back and try configure PHP again, with the line –with-libxml-dir=/usr/local in your PHP configuration command.

Building and Installing Apache 2 and PHP 5.0.5 on Mac OS X 10.4 Tiger

Alert!: Warning: This article/tutorial is more the 45 days old. As such the information contained within could be, by now, out of date. Please read all information to make sure that this article/tutorial will work with your current version of the Operating System

 

FYI Usual stuff… Full Developers’ Tool installation is required as well as up to date system. At this time it means 10.4.2 WITH ALL latest Security Updates.

For anyone that has been wishing to switch to Apache 2 on their Mac, this is the time to do it. PHP 5 has also recently been updated and this tutorial describes in detail, the best method to both build and install these programmes on your Mac.

Since Apple don’t include Apache 2 with OS X yet I have felt it better to keep Apache 2 in its own directory, and to keep PHP for Apache 2, etc. confined the that directory too.

Step 1: Downloading Apache 2----------The first step, as in any build like this, is to download the source and uncompress it. To do this we open the Terminal and enter the following commands. FYI: the % represents the Command Line Prompt and should not be entered

% curl -O http://apache.mirrors.esat.net/httpd/httpd-2.0.54.tar.gz
% gnutar -xzf httpd-2.0.54.tar.gz

Step 2: Building and Installing Apache 2----------I’ve decided to install to a new directory which we will make /apache2. It’s nice and convenient. To do this enter the following commands (sudo is required as the directory doesn’t already exist):

% cd httpd-2.0.54
% sudo ./configure \
% –prefix=/apache2 \
% –enable-module=most \
% –enable-shared=max
% sudo make
% sudo make install

Step 3: Building and Installing PHP----------Building and Installing PHP with Apache 2 is fairly simple. We want to install it into the same directory as Apache… just for sanity’s sake.

% curl -O http://ie.php.net/distributions/php-5.0.5.tar.gz
% gnutar -xzf php-5.0.5.tar.gz
% cd php-5.0.5
% sudo mkdir /apache2/php

Once all that’s done, we will configure and compile PHP.

% ./configure \
% –prefix=/apache2/php \
% –with-zlib \
% –with-xml \
% –with-gd \ (optional, requires jpeg + png)
% –with-jpeg-dir=/usr/local \ (optional)
% –with-png-dir=/usr/local \ (optional)
% –with-mysql=/usr/local/mysql \ (optional)
% –with-apxs2=/apache2/bin/apxs
% make
% sudo make install

The lines marked “Optional” are optional. lib jpeg and png are to enable jpeg and png support in GD Lib – you will need LibJPEG and LibPNG installed previously to enable GD. The MySQL line is if you need MySQL. You will need MySQL installed and running.

If you want a PHP.ini file then enter this. It’s not required though.

% sudo cp php.ini-dist /usr/local/lib/php.ini

Step 4: Configuring Apache----------The next bit is to configure Apache to load PHP files properly. Add it below the file’s current contents. The httpd.conf file is located at /apache2/conf/httpd.conf

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.php

Also, You now have a choice. By default your new document root will be /apache2/htdocs. If you want to change this back to the Apple default of /Library/WebServer/Documents you need to open the httpd.conf file (/apache2/conf/httpd.conf) and change the following:

<Directory “/apache2/htdocs”>

to

<Directory “/Library/WebServer/Documents”>

Similarly, Apache 2 will now point to the UNIX standard user directory. Which means if you type http://::1/~user/ it will point to the ~/public_html directory. In Mac OS X the usual standard is ~/Sites. If you wish to change this you need to access the httpd.conf file once again and change

UserDir public_html

to

UserDir Sites

Step 5: Starting and Stopping Apache 2----------If all went well you should now have a working Apache 2 installation. However, this will not work with the Web Sharing option in System Preferences. Before you continue, please make sure the option in System Preferences is set to “Off”.

To start Apache 2:

sudo /apache2/bin/apachectl start

To stop Apache 2:

sudo /apache2/bin/apachectl stop

You will need to switch this on everytime your restart your Mac. Or, you can use another PHPmac tutorial to enable Apache 2 as the default OS X server. Additional Useful Apache2 Config Settings.

Hope all goes well… if, however, you hit a wall or you need help, however trivial, please contact support. Click Here. Please note; a PHPmac login is required.

-James Pelow
PHPmac


Some frequent Issues:

Q: I recieve the following error message while running the Apache configure command:

Creating Makefile in src
+ configured for Darwin platform
Error: could not find any of these C compilers
anywhere in your PATH: gcc cc acc c89
Configure terminated

A: You need to install the Apple supplied Developers’ Tools. These come on a CD in your Mac OS X box or in the Applications folder if you’ve just bought your Mac recently

Q: I received an error message regarding the absence of LibXML2

A: Building and Installing libXML2 (Required for PHP 5)

libXML2 is now required by PHP. The building of libXML2 is releativly simple, but it takes for ever and a day to compile. So be prepared to go and make yourself a coffee and maybe dinner. Most people should have this, it was included in an Apple Security update a while back. However, many people have been encountering this problem so I have added directions here.

First step is to download the source then Untar it. We will make a directory to do this:

% cd ~/Desktop
% mkdir libxml2build
% cd libxml2build
% curl -O ftp://ftp.gnome.org/pub/GNOME/sources/libxml2/2.6/libxml2-2.6.7.tar.gz
% gnutar -xzf libxml2-2.6.7.tar.gz
% cd libxml2-2.6.7

We are now in the libxml2-2.6.7 source directory. From here it’s a simple compile and install.

% ./configure
% make
% sudo make install
% sudo ranlib /usr/local/lib/libxml2.a

libXML2 is now installed. Go Back and try configure PHP again, with the line –with-libxml-dir=/usr/local in your PHP configuration command.

Enabling Apple’s Supplied PHP in OS X 10.4 Tiger

Alert!: Attention: Cet article/tutorial est ‰gé de plus de 45 jours. Ainsi les informations qu’il contient peuvent être, maintenant, dépassé. Merci de lire toutes les informations qu’il contient pour vous assurez que cet article fonctionnera bien sur votre système

The first thing that many will want to do after they install their shiny new copy of OS X 10.4 Tiger, will no doubt be to restore PHP to their WebServer. This is the quickest, easiest way, that anyone can manage that end.

This tutorial demonstrates how to activate Apple’s PHP, as supplied, out of the box. This is a very quick and easy method that will suit beginners, and will keep more advanced users happy while they wait for the energy to build and install PHP from source.

1. Step One
Locate the “Terminal” application. It comes with every OS X installation and can be found in the Utilities folder, in the Applications folder on your Mac OS X main drive. Open this application.

2. Step Two
Once in the terminal you will be greeted with something like the following text:

Last login: Sat May 7 00:28:29 on ttyp1
Welcome to Darwin!
ibook:~ username$

The $ represents the prompt, or the place that you, the user, types your input. To enter a command; type it in, and press Enter.

3. Step Three
Enter the following commands (not including the $. Remember, they represent the prompt) one line at a time, pressing Enter after each line:

$ cd /etc/httpd
$ sudo pico httpd.conf

4. Step Four
In the last command, we opened the Apache Web Server configuration file in a text editor called “Pico”. You will need to enter your password. We now want to edit this file to enable PHP.

Press Control-w on your keyboard, then enter “php” and press Enter. This will search for the first occurrence of “php” in the Apache configuration file.
You should come to the following line.

# LoadModule php4_module libexec/httpd/libphp4.so

Remove the # (hash sign) from the beginning of the line. Press Control-w again, and then Enter. Repeat this until you reach the following line:

# AddModule mod_php4.c

Once again, remove the # (hash sign) from the start of the line.

5. Step Five
We have now made all necessary changes to the file. Now we must save these changes. Press Control-o (the letter, not zero) and then Enter. The file is now saved. Press Control-x to exit Pico.

6. Step Six
The last step is simply to restart Apache, so that the changes we made to the configuration file take effect. You can simply use the “Sharing” System Preference Pane to restart Web Sharing. Or you can enter the following command in the terminal:

$ sudo apachectl restart

You may need to reenter your password.

That’s it, PHP will now work as part of Apache.

If you’re having problems you can use PHPmac support to get free one-on-one support via email.

*Requires Login

Building and Installing Apache 2.0.54 and PHP 5.0.4 on Mac OS X 10.3.8

By: James Pelow (jamesnp) Posted: 13-Apr-05, 01:52:52 Rated: General/Experienced

Alert!: Warning: This article/tutorial is more the 45 days old. As such the information contained within could be, by now, out of date. Please read all information to make sure that this article/tutorial will work with your current version of the Operating System
FYI Usual stuff… Developers’ Tools are required as well as up to date system. In this case that means 10.3.8 WITH ALL latest Security Updates.

For anyone that has been wishing to switch to Apache 2 on their Mac, this is the time to do it. PHP 5 has also recently been updated and this tutorial describes in detail, the best method to both build and install these programmes on your Mac.

Since Apple don’t include Apache 2 with OS X yet I have felt it better to keep Apache 2 in its own directory, and to keep PHP for Apache 2, etc. confined the that directory too.

Step 1: Downloading Apache 2----------The first step, as in any build like this, is to download the source and uncompress it. To do this we open the Terminal and enter the following commands. FYI: the % represents the Command Line Prompt and should not be entered

% curl -O http://apache.mirrors.esat.net/httpd/httpd-2.0.54.tar.gz
% gnutar -xzf httpd-2.0.54.tar.gz

Step 2: Building and Installing Apache 2----------I’ve decided to install to a new directory which we will make /apache2. It’s nice and convenient. To do this enter the following commands (sudo is required as the directory doesn’t already exist):

% cd httpd-2.0.54
% sudo ./configure \
% –prefix=/apache2 \
% –enable-module=most \
% –enable-shared=max
% sudo make
% sudo make install

Step 3: Building and Installing PHP----------Building and Installing PHP with Apache 2 is fairly simple. We want to install it into the same directory as Apache… just for sanity’s sake.

% curl -O http://ie.php.net/distributions/php-5.0.4.tar.gz
% gnutar -xzf php-5.0.4.tar.gz
% cd php-5.0.4
% sudo mkdir /apache2/php

Once all that’s done, we will configure and compile PHP.

% ./configure \
% –prefix=/apache2/php \
% –with-zlib \
% –with-xml \
% –with-gd \ (optional)
% –with-jpeg-dir=/usr/local \ (optional)
% –with-png-dir=/usr/local \ (optional)
% –with-mysql=/usr/local/mysql \ (optional)
% –with-apxs2=/apache2/bin/apxs
% make
% sudo make install

The lines marked “Optional” are optional. gd, jpeg and png are to enable GD Lib – you will need LibJPEG and LibPNG installed previously. The MySQL line is if you need MySQL. You will need MySQL installed and running.

If you want a PHP.ini file then enter this. It’s not required though.

% sudo cp php.ini-dist /usr/local/lib/php.ini

Step 4: Configuring Apache----------The next bit is to configure Apache to load PHP files properly. Add it below the file’s current contents. The httpd.conf file is located at /apache2/conf/httpd.conf

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.php

Also, You now have a choice. By default your new document root will be /apache2/htdocs. If you want to change this back to the Apple default of /Library/WebServer/Documents you need to open the httpd.conf file (/apache2/conf/httpd.conf) and change the following:

<Directory “/apache2/htdocs”>

to

<Directory “/Library/WebServer/Documents”>

Similarly, Apache 2 will now point to the UNIX standard user directory. Which means if you type http://::1/~user/ it will point to the ~/public_html directory. In Mac OS X the usual standard is ~/Sites. If you wish to change this you need to access the httpd.conf file once again and change

UserDir public_html

to

UserDir Sites

Step 5: Starting and Stopping Apache 2----------If all went well you should now have a working Apache 2 installation. However, this will not work with the Web Sharing option in System Preferences. Before you continue, please make sure the option in System Preferences is set to “Off”.

To start Apache 2:

sudo /apache2/bin/apachectl start

To stop Apache 2:

sudo /apache2/bin/apachectl stop

You will need to switch this on everytime your restart your Mac. Or, you can use another PHPmac tutorial to enable Apache 2 as the default OS X server. Additional Useful Apache2 Config Settings.

Hope all goes well… if, however, you hit a wall or you need help, however trivial please contact support. Click Here. Please not a PHPmac login is required.

-James Pelow
PHPmac


Some frequent Issues:

Q: I recieve the following error message while running the Apache configure command:

Creating Makefile in src
+ configured for Darwin platform
Error: could not find any of these C compilers
anywhere in your PATH: gcc cc acc c89
Configure terminated

A: You need to install the Apple supplied Developers’ Tools. These come on a CD in your Mac OS X box or in the Applications folder if you’ve just bought your Mac recently

Q: I received an error message regarding the absence of LibXML2

A: Building and Installing libXML2 (Required for PHP 5)

libXML2 is now required by PHP. The building of libXML2 is releativly simple, but it takes for ever and a day to compile. So be prepared to go and make yourself a coffee and maybe dinner. Most people should have this, it was included in an Apple Security update a while back. However, many people have been encountering this problem so I have added directions here.

First step is to download the source then Untar it. We will make a directory to do this:

% cd ~/Desktop
% mkdir libxml2build
% cd libxml2build
% curl -O ftp://ftp.gnome.org/pub/GNOME/sources/libxml2/2.6/libxml2-2.6.7.tar.gz
% gnutar -xzf libxml2-2.6.7.tar.gz
% cd libxml2-2.6.7

We are now in the libxml2-2.6.7 source directory. From here it’s a simple compile and install.

% ./configure
% make
% sudo make install
% sudo ranlib /usr/local/lib/libxml2.a

libXML2 is now installed. Go Back and try configure PHP again, with the line –with-libxml-dir=/usr/local in your PHP configuration command.

Additional Useful Apache2 Config Settings

Warning: This article/tutorial is more the 45 days old. As such the information contained within could be, by now, out of date. Please read all information to make sure that this article/tutorial will work with your current version of the Operating System

After completing your install of apache2 as per the phpmac.com tutorial, here’s a couple of additional tips to help make it as Mac-compatible as possible.

These edits all take place in your apache2 config file, most likely located in /apache2/conf/httpd.conf.

Default User
The default user for OS X should be www instead of nobody. Search for:

User nobody
Group nobody

and change to:

User www
Group www

.htaccess Security Settings per Apple
Apple now has a rather longer .htaccess denial section (as of the 12-02-04 Security Update). You can duplicate it by searching for the line

# The following lines prevent .htaccess files from being viewed by

and replacing the uncommented Files directive below it with the following three directives


Order allow,deny
Deny from all
Satisfy All


Order allow,deny
Deny from all
Satisfy All


Order allow,deny
Deny from all
Satisfy All

The cgi-bin Directory
Additionally, if you’re planning to keep using the /Library/WebServer folder, don’t forget to change your cgi-bin folder settings. Search for the ScriptAlias line and make it look like this

ScriptAlias /cgi-bin/ “/Library/WebServer/CGI-Executables/”

Using the System Prefs Sharing Panel to Control Apache2
Finally, if you want to be able to use your System Preferences Sharing pane to start and stop your apache2 install, there are a couple of things to do. First is to find the PidFile section and make it look like this

#
# PidFile: The file in which the server should record its process
# identification number when it starts.
#
#
PidFile /private/var/run/httpd.pid
#

Note that not only has the PidFile path been changed to be in the /private/var/run folder, but also, the IfModule lines have been commented out.

At this point, all necessary modifications have been made to the apache2 conf file–we just need to link up the apachectl for apache2 to replace the one that is installed by default by Apple. So save your httpd.conf file and in Terminal do the following.

cd /usr/sbin/;
mv apachectl apachectl-1.3;
ln -s /apache2/bin/apachectl apachectl;

Basically, this renames the old Apple apachectl to be apachectl-1.3 (and you can even start and stop the original Apple apachectl by using that command). Then it makes a symbolic link from our apache2 folder into the /usr/sbin folder. Now when you start and stop Web Sharing from the System Preferences panel, you should be starting and stopping apache2.

This last tip was gleaned from MacOSXHints.com, by the way.

Hope these tips helps others!

Building and Installing PHP 4.3.10 with Apache 1.3.31 and GD Support

By: James Pelow (jamesnp) Posted: 23-Dec-04, 07:18:19 Rated: General/Experienced

FYI This article has been tested on Mac OS X 10.3.1 to 10.3.7. No support will be given to people using any other version. However it is quite possible that it will work on different versions.

MAC OS X SERVER IS NOT SUPPORTED

PANTHER USERS: It is vital that you have UP TO DATE Developers’ Tools installed. This means installing PANTHER developer tools rather then Jaguar ones.

As with all PHPmac tutorials the Apple supplied Developers’ Tools need to be installed. These are on the Developers’ Tools CD that came in your Mac OS X box.

 

Part 1: Building and Installing Libpng and Libjpeg

FYI Please Note: If you have previously built and installed libjpeg and libpng or you do not need image manipulation support then you need not follow this part. Skip to Part 2: Building and Install Apache

To start we must download the libjpeg and libpng source distributions. To do this enter the following in the terminal. (Tip: The % represents the terminal prompt and should not be considered part of the command). In the following commands, We first create a directory called gdbuild to keep the build organised. We then download the files to this directory. And finally we untar (unstuff) these files.

% cd ~/Desktop
% mkdir gdbuild
% cd gdbuild
% curl -O ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz
% curl -O http://heanet.dl.sourceforge.net/sourceforge/png-mng/libpng-1.2.5.tar.gz
% gnutar xzf jpegsrc.v6b.tar.gz
% gnutar xzf libpng-1.2.5.tar.gz

Once all that has been completed we need to actually build libjpeg. In the following we basically configure the installer, then compile (make) the code so that it becomes a binary. And we then install that binary and activate it.

% cd jpeg-6b
% sudo mkdir -p /usr/local/include
% sudo mkdir -p /usr/local/bin
% sudo mkdir -p /usr/local/man
% sudo mkdir -p /usr/local/lib
% sudo mkdir /usr/local/man/man1

The above commands create directories that are missing in Mac OS X. Now we will configure, build and install the installation:

% ./configure
% sudo make install
% sudo make install-lib
% sudo ranlib /usr/local/lib/libjpeg.a
% cd ..

After libjpeg installs we move onto libpng. The method is quite similar but with a big exception, we need to edit the makefile so that the build completes.

% cd libpng-1.2.5
% cp scripts/makefile.macosx ./Makefile
% pico Makefile

The previous command opens Makefile in Pico, which is a UNIX command line text editor. In Pico we must make a few changes. The first involves changing the commenting (the # signs) on the following:

# Where the zlib library and include files are located
#ZLIBLIB=/usr/local/lib
#ZLIBINC=/usr/local/include
ZLIBLIB=../zlib
ZLIBINC=../zlib

This is quite near the top. Change the commenting so that it becomes:

# Where the zlib library and include files are located
ZLIBLIB=/usr/local/lib
ZLIBINC=/usr/local/include
#ZLIBLIB=../zlib
#ZLIBINC=../zlib

Then do a search (Control-W) for

LDFLAGS=-L. -L$(ZLIBLIB) -lpng -lz -current_version $(PNGVER)

change this to:

LDFLAGS=-L. -L$(ZLIBLIB) -lpng -lz

Save the changes by pressing Control-O and then Enter/Return. Exit Pico by pressing Control-x

% make
% sudo make install
% sudo ranlib /usr/local/lib/libpng.a
% cd ..

Once this completes you have built and installed libpng and libjpeg. These two libraries are required to open, edit and create png and jpeg files using GD library, which is now bundled in PHP.

Part 2: Building and Installing Apache Web Server

FYI Please Note: If you have previously built and installed Apache 1.3.31 then you need not follow this part. Skip to Part 3: Building and Install PHP

We need to create a directory to work in on the desktop and then download Apache source distribution to that directory. Following this we untar (unstuff) the file. Create a new terminal window (Apple-N) and then enter the following commands:

cd ~/Desktop
% mkdir apachebuild
% cd apachebuild
% curl -O http://apache.mirrors.esat.net/httpd/apache_1.3.31.tar.gz
% gnutar -xzf apache_1.3.31.tar.gz
% cd apache_1.3.31

We are now in the Apache source code directory. We can now configure, make and install Apache. Enter the following commands (Enter one line at a time):

% ./configure \
–enable-module=most \
–enable-shared=max
% make
% sudo make install

Apache has now been installed. The final step is to build PHP.

Part 3: Building and Installing PHP

Like Apache, we need to create a directory to work in on the desktop and then download the PHP source distribution to that directory. Following this we untar (unstuff) the file. Create a new terminal window (Apple-N) and then enter the following commands:

cd ~/Desktop
% mkdir phpbuild
% cd phpbuild
% curl -O http://ie.php.net/distributions/php-4.3.10.tar.gz
% gnutar -xzf php-4.3.10.tar.gz
% cd php-4.3.10

We are now going to Configure, build and install PHP. Enter the follow commands (One Line at a time) (You can omit the calls to gd, jpeg and png if you decided not to install them in step 1)

% ./configure \
–with-xml \
–with-zlib \
–with-gd \
–with-jpeg-dir=/usr/local \
–with-png-dir=/usr/local \
–with-mysql=/usr/local/mysql \ (optional, required if you need MySQL)
–with-apxs=/usr/sbin/apxs
% make
% sudo make install

If you want a PHP configuration file (php.ini) (Not Required):

% sudo cp php.ini-dist /usr/local/lib/php.ini

If you have only used the PHP that came with OS X before:
You may need to comment (place a # at the start of the line) the following lines in your Apache Configuration (/etc/httpd/httpd.conf) file:

LoadModule hfs_apple_module libexec/httpd/mod_hfs_apple.so
AddModule mod_hfs_apple.c

That’s it. It’s all done. All you need to do now is restart Apache and all should work.

sudo apachectl restart

If you wish you can also delete the build directories from the desktop!

If you are having trouble have a look through the FAQ below. If you are still having trouble click here* to use the PHPmac support service.

-James Pelow
Webmaster, PHPmac

*You must be logged in to avail of this service.


Some frequent Issues:

Q: I recieve the following error message while running the Apache configure command:

Creating Makefile in src
+ configured for Darwin platform
Error: could not find any of these C compilers
anywhere in your PATH: gcc cc acc c89
Configure terminated

A: You need to install the Apple supplied Developers’ Tools. These come on a CD in your Mac OS X box.

Q: I recieve the following error message when starting Apache:

Syntax error on line 240 of /etc/httpd/httpd.conf:
API module structure `hfs_apple_module’ in file
/usr/libexec/httpd/mod_hfs_apple.so is garbled – perhaps this is not an Apache module DSO?


A: Enter the following command in the terminal:

sudo pico /etc/httpd/httpd.conf

Then Press Cntrl – W and type hfs. Place a # in front of the line it finds, then press Cntrl W again and repeat the process.

Q: I recieve the following error message when starting Apache:

Syntax error on line 240 of /etc/httpd/httpd.conf:
API module structure `mod_rendezvous_apple’ in file
/usr/libexec/httpd/mod_rendezvous_apple.so is garbled – perhaps this is not an Apache module DSO?


A: Enter the following command in the terminal:

sudo pico /etc/httpd/httpd.conf

Then Press Cntrl – W and type rendezvous. Place a # in front of the line it finds, then press Cntrl W again and repeat the process.

Q: It’s not working. I just see my PHP code when I look at the page in my browser
A: Open the Apache Configuration file (/etc/httpd/httpd.conf) in your favourite Text Editor (In these examples I have used Pico)

% sudo pico /etc/httpd/httpd.conf

Now add these lines to the very bottom of the file:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Q: The configure is not working. It says something about APXS
A: Did you install Apache as well, as directed in the tutorial? Apple’s supplied Apache will not do.

Building and Installing MaxMind GeoIP

Attention: Cet article/tutorial est ‰gé de plus de 45 jours. Ainsi les informations qu’il contient peuvent être, maintenant, dépassé. Merci de lire toutes les informations qu’il contient pour vous assurez que cet article fonctionnera bien sur votre système

 

FYI Mac OS X 10.3.4 is required will all relevant updates installed
Developers’ Tools are, as always, required for this tutorial
A version of Apache 1.3.x with APXS enabled is also necessary. (several PHPmac tutorials available)
Apache 2 version of mod_geoip is available, however, the method of installing this is outside the scope of this tutorial.

Step 1: Installing MaxMind GeoIP API----------The first step of this tutorial is a fairly straight foward build of the GeoIP API. The source is available free under the GPL

(% Represents the command line prompt)

% curl -O http://www.maxmind.com/download/geoip/api/c/GeoIP-1.3.4.tar.gz
% gnutar -xzf GeoIP-1.3.4.tar.gz
% cd GeoIP-1.3.4
% ./configure
% make
% make check
% sudo make install

Step 2: Installing mod_geoip for Apache----------Downloading and untarring are the first steps

% curl -O http://www.maxmind.com/download/geoip/api/mod_geoip/mod_geoip_1.2.4.tar.gz
% gnutar -xzf mod_geoip_1.2.4.tar.gz
% cd mod_geoip_1.2.4

Building the actual module is once again fairly simple. This step requires that you have built Apache previously with APXS support

sudo apxs -cia -I/usr/local/include -L/usr/local/lib -lGeoIP mod_geoip.c

Once this has completed you need to add the following lines to the end of your Apache httpd.conf file.

<IfModule mod_geoip.c>
# If GeoIP is turned on, turn it on
GeoIPEnable On
</IfModule>

The paths to the conf file is usually /etc/httpd/httpd.conf. Explaining how to change your httpd.conf file is not within the scope of this tutorial.
Restart Apache:

sudo apachectl restart

Step 3: Using in your PHP web applications----------Probably the easiest part of the entire process is using this in everyday use. Basically, the module provides the country information, all the hard work is done by the module itself. To access the country information in PHP you can use the following script.

<?PHP
$country_code = apache_note(“GEOIP_COUNTRY_CODE”);
$country_name = apache_note(“GEOIP_COUNTRY_NAME”);
?>

That’s it – all done. You can now call on this in all your PHP scripts. This is very effective method of automatically selecting Download Mirrors, providing location specific content, etc. for your visitors.

A word of warning: this method of determining geographical location can sometimes yield incorrect results. For example, if a multinational company has recently expanded to a new country and has taken IP addresses with it. This happened to me and I kept getting British mirrors on some websites when I wanted Irish ones. The moral of the story is to use this to make an initial “guess” – but always provide your users with a way to manually switch countries.

I have also written a little script that plots the location that GeoIP gives to a map of the world. It also checks the IP against the RIPE IP whois, or ARIN for US IPs. You can download the source here Requires GD and GeoIP (obviously)!

If you have any problems don’t hesitate to contact the support team. Click Here. A login is required.

Enjoy,

Setting up mod_gzip in Apache

Warning: This article/tutorial is more the 45 days old. As such the information contained within could be, by now, out of date. Please read all information to make sure that this article/tutorial will work with your current version of the Operating System

FYI In response to feedback from “jeanloui”, several changes have been made to this article.

Overview:
The mod_gzip module for Apache will compress the pages as it sends them. Of course, it will only do this if the browser that your audience is using is compatible. Before you do this tutorial, run your website through PipeBoost to see how fast it currently is.

Installation:

1. Download the latest version of mod_gzip from SourceForge.

2. De-compress it.

3. Using TextEdit, or your favourite editor, make the following changes to the Makefile in the folder.
Change:

APXS?=/usr/local/sbin/apxs

To:

APXS?=/usr/sbin/apxs

Change:

$(APXS) -Wc,-Wall,-O3,-fomit-frame-pointer,-pipe -c mod_gzip.c mod_gzip_debug.c mod_gzip_compress.c -o mod_gzip.so

To:

$(APXS) -Wc,-Wall,-O3,-fomit-frame-pointer,-pipe -c mod_gzip.c mod_gzip_debug.c mod_gzip_compress.c

4. Save the file.

5. Run “make” in the Terminal in the directory.

6. Run “sudo make install”, entering your password when requested.

7. Run “sudo pico /etc/httpd/httpd.conf”

8. Uncomment, that is remove the # from the front of (if it is not already done), these lines:

LoadModule gzip_module libexec/httpd/mod_gzip.so
AddModule mod_gzip.c
AddEncoding x-compress Z
AddEncoding x-gzip gz tgz

9. Still in pico, add these lines to the end of the file:


mod_gzip_on Yes
mod_gzip_can_negotiate Yes
mod_gzip_static_suffix .gz
AddEncoding gzip .gz
mod_gzip_update_static No
mod_gzip_command_version ‘/mod_gzip_status’
mod_gzip_temp_dir /var/mod_gzip_temp_dir
mod_gzip_keep_workfiles No
mod_gzip_minimum_file_size 500
mod_gzip_maximum_file_size 500000
mod_gzip_maximum_inmem_size 60000
mod_gzip_min_http 1000
mod_gzip_handle_methods GET POST
mod_gzip_item_exclude reqheader “User-agent: Mozilla/4.0[678]”
mod_gzip_item_include file .html$
mod_gzip_item_include file .shtml$
mod_gzip_item_include file .htm$
mod_gzip_item_include file .shtm$
mod_gzip_item_include file .php$
mod_gzip_item_include file .phtml$
mod_gzip_item_exclude file .js$
mod_gzip_item_exclude file .css$
mod_gzip_item_include file .pl$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/html$
mod_gzip_item_include mime ^text/plain$
mod_gzip_item_include mime ^httpd/unix-directory$
mod_gzip_item_exclude mime ^image/
mod_gzip_dechunk Yes
mod_gzip_add_header_count Yes
mod_gzip_send_vary Yes

10. Save the file with Control-O.

11. Run “cd /etc”. Then “ls php*” to see if there is a file called “php.ini”. If there is only “php.ini.default” then run “sudo cp php.ini.default php.ini”.

12. Run “sudo pico php.ini”. Look for:
Change:

zlib.output_compression = On

To:

zlib.output_compression = On
zlib.output_compression_level=5

13. Save the file with Control-O.

14. Restart Apache with “sudo apachectl restart”

15. Test your website again with PipeBoost and notice the difference!

Credit for this article is due to to Christoper Holland at Exordium

John Lunney marked-up and made slight changes to it.