Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

November 21, 2008

Running PHP Scripts from a Command Line

Another great aspect about PHP is that you don't even need Apache to run your scripts. If you don't need any special GUI or your script takes so long that your page is timing out before it can finish, try running it from the Command Line.

Preparation:
  • Install PHP - Of course you have to install PHP! I felt silly adding this to the list, but I felt I had to.
  • Add the PHP installation directory as a Value in the Environment Variables - To do this, Right click My Computer->Properties->Advanced->Environment Variables. Look at the bottom in the System Variables for the Variable name, Path. Select Path and click Edit. Check to make sure that your install directory is included in the Variable Value path. For example, I should see in my path, C:\PHP. If the is not, add it to the beginning of the Path and separate it with a semi-colon (C:\PHP;). This is necessary so you can just type PHP MyScript.php while you are in the appropriate script directory.


Using the Command Line

This sections is for the complete newbie to the command line.

  • To open the Command Prompt, go START->RUN->type cmd
  • From here, you will need to navigate to where your PHP script you want to run. To change directories, use the cd command. For instance, if I was running a script out of the Default Apache DocumentRoot, I would type the following: cd C:\Apache\Apache2\htdocs [enter]. At this point, you are in the same directory as the script you want to run. Now type: php MyScript.php


  • With this in mind, you could actually call your PHP script much in the same way. After you enter into the Command Prompt at the very beginning type: php C:\Apache\Apache2.2\htdocs\MyScript.php [enter]


  • If your script contains HTML or any other non-PHP elements, they will just be printed to the screen
Conclusion

The reason you added C:\PHP to the environment path is so you didn't have to navigate to the PHP installation directory to access PHP.exe. Using this strategy, you can now build batch file programs that will run your scripts for you. For instance, I wrote a script that will update thousands of records in a MySQL database. The process takes over 10 minutes and I need to run it every day. All I did was created a text file called Update_Data.bat and add one line of text to the file: php C:\Apache\Apache2.2\htdocs\MyUpdate.php Now all I have to do is click Update_Data.bat and walk away without worries of a page time out.

November 20, 2008

How To Install PHP 5.2

NOTE: At this point you should already have Apache 2.2 installed on a Windows PC. At the time of this writing, PHP was on Version PHP 5.2.6

Installing PHP



  1. First things first, you need to download the latest version of PHP at http://www.php.net/downloads.php. This actually can be an overwhelming process for the first time PHP installer. There is 8 download options to choose from, but for now we are going to concentrate on just the first 2. The Zip Package and the Installer.
    • MSI Installer - This is the easiest way to install PHP and it will install, then load nearly every default extension dll available. This is the one I will be explaining.
    • The Zip Package - Download the zip package if you are looking to upgrade to a new minor release version. For instance, if you have PHP 5.2.5 installed, download the Zip Package, rename the Original PHP directory to PHP_old and then name the extracted zip package to PHP and copy it in place of the old version. You will then need to copy the PHP.ini file from the old to the new. This is located in the PHP root.
  2. After you have downloaded the latest MSI version, double click the MSI to start the Install Wizard.
  3. Change the Install directory to C:\PHP if you want to keep it consistent with your Apache installation
  4. If you installed Apache 2.2 earlier, you will need to select the Apache 2.2.x Module when prompted on the Web Server Setup screen
  5. You will then be prompted to locate the Apache Configuration directory. This is where the httpd.conf file is located. If you followed my Apache Installation instructions, it will be located in C:\Apache\Apache2.2\conf. (During the installation of PHP, the httpd.conf file will be updated to include the PHPIniDir and the LoadModule directives)
  6. When prompted to Choose Items to Install, click the box next to Extensions and select Entire feature will be installed on local hard drive
  7. Repeat the same for Extras
  8. Finish going through the Install wizard

CAUTION

If you read this far, you are in luck. Following the above installation instructions will inevitably end in frustration if you don't read this.

When you install PHP to the fullest extent, you will get every possible PHP extension or your hard drive and unfortunately, each one is added to the PHP.ini configuration file. This wouldn't be a problem if they would all load without errors, but this isn't the case. Many extra Extension dll's are loaded that will not run properly without their counter parts installed as well. This causes errors and you are not able to load PHP pages. You need to open the php.ini file which is located in the root directory (C:\PHP) where you installed PHP to and scroll to the bottom of the file comment out the extensions that are listed below. Also, take this time to look over the options available to setup your PHP the way YOU want it. Basically, to use the basic functionality of PHP, you don't even need to load any of the dlls. The extension dlls give you added functionality like the php_mysql.dll which gives you access to the MySQL functions. Below is a list of all the Extensions that need to be commented out with a semi colon (;):

[PHP_OCI8]
;extension=php_oci8.dll
[PHP_PDO_OCI]
;extension=php_pdo_oci.dll
[PHP_PDO_OCI8]
;extension=php_pdo_oci8.dll
[PHP_PDO_PGSQL]
;extension=php_pdo_pgsql.dll
[PHP_PDO_SQLITE]
;extension=php_pdo_sqlite.dll
[PHP_PDO_SQLITE_EXTERNAL]
;extension=php_pdo_sqlite_external.dll
[PHP_PGSQL]
;extension=php_pgsql.dll
[PHP_PSPELL]
;extension=php_pspell.dll
[PHP_SNMP]
;extension=php_snmp.dll
[PHP_SYBASE_CT]
;extension=php_sybase_ct.dll
[PHP_DOMXML]
;extension=php_domxml.dll
[PHP_IBM_DB2]
;extension=php_ibm_db2.dll
[PHP_IFX]
;extension=php_ifx.dll
[PHP_IISFUNC]
;extension=php_iisfunc.dll
[PHP_INGRES2]
;extension=php_ingres2.dll
[PHP_MAXDB]
;extension=php_maxdb.dll
[PHP_MCVE]
;extension=php_mcve.dll
[PHP_NETOOLS]
;extension=php_netools.dll
[PHP_ORACLE]
;extension=php_oracle.dll
[PHP_PDO_IBM]
;extension=php_pdo_ibm.dll
[PHP_PDO_INFORMIX]
;extension=php_pdo_informix.dll
[PHP_SAM]
;extension=php_sam.dll
[PHP_SPL_TYPES]
;extension=php_spl_types.dll
[PHP_THREADS]
;extension=php_threads.dll