Wednesday, March 12, 2008

PHP5, IIS6 and WIN2003

Dammit, I STRUGGLED with this stuff for a coupla days, and no help could be found anywhere online. As always some forums were completely misleading, or never got to a solution, else went completely off topic. PHP.net was no help at all, their only conclusion to this kind of thing is that IIS is crap and we should all be on *nix with Apache.

So here it is, a quick step by step on getting PHP5 installed on a Windows Server running IIS 6. These are steps to follow for a clean install. If you have failed installations and came across this post seeking help, remove the installation completely. By that I mean, remove .php file mappings, ISAPI extensions, registry settings you might have tried, EVERYTHING. Even delete the PHP install folders. Remove anything you may have gone and dumped in your \Windows or \System32 folders. Get your machine/server back to just plain old IIS running the usual .Net, ASP etc. stuff. Preferably reboot the machine for everything to take proper effect.

THEN

1. Download the latest build of PHP from their site: http://www.php.net/downloads.php
For the purposes of this post I am referring to PHP version 5.

2. DON’T download the installer, it’s crap. Download the zip file instead for manual extraction and installation. (Not as painful as it sounds)

3. Once downloaded, extract the contents to a folder like C:\PHP or something. A folder somewhere without spaces in the path is preferred. I’ll continue with the assumption that this will be your working folder.

4. Open the folder you have just extracted the files to, and find the file named php.ini-recommended. Rename this to php.ini and open it.

5. Find the spot with the following line:
extension_dir = "./"
and change it to
extension_dir = "C:\PHP\ext"
This just points PHP to the correct extensions directory.

6. Now we have to set up the environmental variables to help PHP find its way around the server.
Right click "My Computer" –> Properties –> Advanced
Click "Environment Variables" at the bottom








Under "System Variables" find the "Path" variable in the list
Select it and click "Edit"
Edit the value by adding ";C:\PHP" to the END. (including semicolon). Hit OK

Now click the "New" button to add a new System Variable.
Variable Name: "PHPRC"
Variable Value: "C:\PHP"






7. Now open your IIS Management Console.
Right-click the "Web Sites" node (not any particular web site in the list), Click Properties.
Click the "Home Directory" tab, then click "Configuration"
On the "Mappings" tab, click "Add" to create a new mapping.



Click Browse to locate and select the file "C:\PHP\php5isapi.dll"
In the "Extension" field enter "php"
Hit OK to save and close. OK all the way out.

Now Right-click on "Web Service Extension" and click "Add a new web service extension"



Extension name: "PHP ISAPI"
Required Files: Click "Add",
Browse to and select "C:\PHP\php5isapi.dll" and hit OK
Check the box that says "Set extension status to Allowed"
Hit OK

Now IIS and the server is completely configured to run PHP5.

REBOOOOOOOOOT!!!!!
This is needed to let everything take effect, especially the environment variables and ISAPI extensions.

8. Next, create a text file called info.php with the following code in it:

Place the file in your web root and browse to it.

You should get a nice dump of your entire PHP setup in beautiful purple and gray
Only problem here would be the fact that you don’t see any of your extensions mentioned in the file. Most of you will be wondering why PHP can’t make any kind of Database connections using mysql_connect() in particular.

Well this is solved by going back to your php.ini file.
Find the section:

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
A few lines below that you’ll see a list of commented out lines looking like:

;extension=php_abcdef.dll

Simply uncomment (remove preceding ";") to enable the necessary extension. Most of them will be in the C:\PHP\ext directory, double check before enabling it to make sure the files exists else you’ll get errors.

Restart IIS after making php.ini changes;

Start->Run->iisreset

AND THAT’S IT!

Everything should work perfectly fine after that.

Let me know how it all goes.

Tuesday, March 4, 2008

How To Redirect with PHP

You can use a simple PHP script to redirect a user from the page they entered to a different web page. One reason you may want to do this is that the page they are trying to access no longer exists. Using this method, they can be seamlessly transfered to the new page without having to click a link to continue.

Users are redirected quickly and seamlessly
When using the 'Back' button, the user is taken to the last viewed page, not the redirect page
Works on all browsers
Difficulty: Easy
Time Required: 1 Minute
Here's How:

phpheader( 'Location: http://www.yoursite.com/new_page.html' ) ;

?>
Change the code on the redirect page to be simply this. You need to replace the URL above with the URL you wish to direct to.
Be sure that you do not have any text sent to the browser before this, or it will not work. Your safest bet is to simply remove all content from the page but the redirect code.

php//this will NOT work, the browser received the HTML tag before the scriptheader( 'Location: http://www.yoursite.com/new_page.html' ) ;

?>

Tuesday, February 5, 2008

learning PHP

The essentials for learning PHP: Learning a new language (programming or otherwise) can be a bit overwhelming. Many people just don't know where to start and give up before they even begin. Learning PHP is NOT as overwhelming as it might seem, I promise you. Just take it one step at a time, and before you know it you'll be off and running.

1.) Basic Knowledge: The first thing you need, before you start learning PHP, is a basic understanding of HTML. You can switch between PHP and HTML right in the same document. You can even run PHP from an HTML file.

2.) Tools: When creating PHP pages, you can use the same program you use to create your HTML pages. Any plain text editor will do. You will also need an FTP client to transfer files from your computer to your web hosting. If you already have an HTML website you most likely already use an FTP program.


3.) The Basics: Now you can finally get started learning PHP! The first thing you should read is our PHP Basics tutorial. This will take you through creating your first file, using variables, basic math, and basic IF statements (a form of logic).

4.) Learning Loops : Once you have mastered these skills, it is time to learn about loops. Loops repeat the same actions over and over again until a condition is met. There are several different types of loops which are explained in our Learning Loops tutorial.

5.) PHP Functions: Finally, you can learn to write your own custom functions. From here the sky is the limit... With a solid knowledge of these PHP basics, adding PHP functions to your arsenal when you need them will be easy.

Monday, January 28, 2008

How to install PHP in Apache 1.3.x on Microsoft Windows

This Article contains notes and hints specific to Apache 1.3.x installs of PHP on Microsoft Windows systems. There are also instructions and notes for Apache 2 on a separate page.


There are two ways to set up PHP to work with Apache 1.3.x on Windows. One is to use the CGI binary (php.exe for PHP 4 and php-cgi.exe for PHP 5), the other is to use the Apache Module DLL. In either case you need to edit your httpd.conf to configure Apache to work with PHP, and then restart the server.

It is worth noting here that now the SAPI module has been made more stable under Windows, we recommend it's use above the CGI binary, since it is more transparent and secure.

Although there can be a few variations of configuring PHP under Apache, these are simple enough to be used by the newcomer. Please consult the Apache Documentation for further configuration directives.

After changing the configuration file, remember to restart the server, for example, NET STOP APACHE followed by NET START APACHE, if you run Apache as a Windows Service, or use your regular shortcuts.

Note: Remember that when adding path values in the Apache configuration files on Windows, all backslashes such as c:\directory\file.ext must be converted to forward slashes, as c:/directory/file.ext. A trailing slash may also be necessary for directories.

Installing as an Apache module
You should add the following lines to your Apache httpd.conf file:


Example#1 PHP as an Apache 1.3.x module

This assumes PHP is installed to c:\php. Adjust the path if this is not the case.

For PHP 4:

# Add to the end of the LoadModule section # Don't forget to copy this file from the sapi directory! LoadModule php4_module "C:/php/php4apache.dll" # Add to the end of the AddModule section AddModule mod_php4.c For PHP 5:

# Add to the end of the LoadModule section LoadModule php5_module "C:/php/php5apache.dll" # Add to the end of the AddModule section AddModule mod_php5.c For both:

# Add this line inside the conditional brace AddType application/x-httpd-php .php # For syntax highlighted .phps files, also add AddType application/x-httpd-php-source .phps
Installing as a CGI binary
If you unzipped the PHP package to C:\php\ as described in the Manual Installation Steps section, you need to insert these lines to your Apache configuration file to set up the CGI binary:

Example#2 PHP and Apache 1.3.x as CGI

ScriptAlias /php/ "c:/php/" AddType application/x-httpd-php .php # For PHP 4 Action application/x-httpd-php "/php/php.exe" # For PHP 5 Action application/x-httpd-php "/php/php-cgi.exe" # specify the directory where php.ini is SetEnv PHPRC C:/php Note that the second line in the list above can be found in the actual versions of httpd.conf, but it is commented out. Remember also to substitute the c:/php/ for your actual path to PHP.

Warning

By using the CGI setup, your server is open to several possible attacks. Please read our CGI security section to learn how to defend yourself from those attacks.

If you would like to present PHP source files syntax highlighted, there is no such convenient option as with the module version of PHP. If you chose to configure Apache to use PHP as a CGI binary, you will need to use the highlight_file() function. To do this simply create a PHP script file and add this code: .

Are you ready to become a PHP web developer?

Are you ready to become a PHP web developer? Are you ready to spend few hours and to get in touch with PHP?If you are then read the rest of this tutorial and you will not regret.
What is PHP?PHP is an open source server side scripting language.PHP is an scripting language that is used to create dynamic HTML page before uit is sent to a client.We’re talking about working with templates.Generating content with PHP client can’t see the code we used to create something, clientsees only HTML code -> result of our script.


PHP is one of the most used scripting technologies that are in use today.In PHP is a lot of same functions as in some others scripting languages like C or PERL.This means that you can make one process with differnt functions
.


Echo ‘phpstudent.blogspot.com - introduction to php’;
?>

and

printr(’phpstudent.blogspot.com - introduction to php’);
?>

These two functions will give the same result:.phpstudent.blogspot.com - introduction to phpt
One more important thing is that PHP is rich with functions that allow us to manipulate with different content types likesome graphics, loading .NET modules, work with XML…etc.PHP vs.

ASP

First difference between those two scripting technologies is that ASP isn’t a scripting language all by himself. It’s a group of connected objectswhich we can get with VB or JavaScript.PHP vs.

PERL

Main difference between those two languages is that PHP is mainly used for web programming while PERL has wider use….