About C#/.NET programming, BlogEngine usage, and Software testing
Posts tagged php
PHP – PEAR installation get warning: date(): It is not safe to rely on the system’s timezone settings.
Apr 17th
The Issue:
While I am install the PEAR for PHP 5.3.2 version with display_errors settings on, I get following error,
===========================================================
Warning: date(): It is not safe to rely on the system’s timezone settings. You a
re *required* to use the date.timezone setting or the date_default_timezone_set(
) function. In case you used any of those methods and you are still getting this
How to installing PHPUnit 3.4
Apr 15th
PHPUnit 3.4 requires PHP 5.1.4 (or later) but PHP 5.3.2 (or later) is highly recommended. It should be installed using the PEAR Installer. This installer is the backbone of PEAR, which provides a distribution system for PHP packages, and is shipped with every release of PHP since version 4.3.0.
The PEAR channel (pear.phpunit.de) that is used to distribute PHPUnit needs to be registered with the local PEAR environment. Furthermore, a component that PHPUnit depends upon is hosted on the Symfony Components PEAR channel (pear.symfony-project.com).
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com
This has to be done only once. Now the PEAR Installer can be used to install packages from the PHPUnit channel:
pear install phpunit/PHPUnit
After the installation you can find the PHPUnit source files inside your local PEAR directory; the path is usually /usr/lib/php/PHPUnit.
Although using the PEAR Installer is the only supported way to install PHPUnit, you can install PHPUnit manually. For manual installation, do the following:
-
Download a release archive from
http://pear.phpunit.de/get/and extract it to a directory that is listed in theinclude_pathof yourphp.iniconfiguration file. -
Prepare the
phpunitscript:-
Rename the
phpunit.phpscript tophpunit. -
Replace the
@php_bin@string in it with the path to your PHP command-line interpreter (usually/usr/bin/php). -
Copy it to a directory that is in your path and make it executable (
chmod +x phpunit).
-
-
Prepare the
PHPUnit/Util/PHP.phpscript:-
Replace the
@php_bin@string in it with the path to your PHP command-line interpreter (usually/usr/bin/php).
-
How to Install and Configure PHP 5 to Run with Apache on Windows
Apr 15th
How to Install and Configure PHP 5 to Run with Apache on Windows
by Christopher Heng, thesitewizard.com
Many web developers want to run Apache and PHP on their own computer since it allows them to easily test their scripts and programs before they put them “live” on the Internet. This article gives a step by step guide on how you can install and configure PHP5 to work together with the Apache HTTP Server on Windows. The procedure has been tested to work on both Windows XP and Vista.
If you have not already installed Apache on your machine, check out one of the guides listed below. This “How To” guide assumes that you have already completed installing Apache.
-
If you are using Apache 1.3.x, see the guide How to Install the Apache Web Server 1.x on Windows.
-
If you plan to use one of the Apache 2 or 2.2 web servers on Windows XP, see the tutorial How to Install and Configure Apache 2 on Windows instead.
-
If you are using Apache 2.2 on Windows Vista, please read How to Install Apache 2.2 on Windows Vista.
Note: those planning to install PHP 4 on Apache 1.x should read my article How to Install and Configure PHP4 to Run with Apache on Windows instead.
Steps to Setting Up PHP 5
-
Download PHP 5
Before you begin, get a copy of PHP 5 from the PHP download page. In particular, download the VC6 thread-safe zip package from the “Windows Binaries” section — that is, don’t get the installer. For example, select the package labelled “PHP 5.2.5 zip package” if 5.2.5 is the current version.
[Update: note that I have not tested the procedure below with any of the PHP 5.3 versions, only with 5.2.5, which was the latest version at the time I originally wrote this. In theory, the procedure should work with later 5.2 versions as well. I'm not sure about 5.3 though. A version jump from 5.2 to 5.3 usually means bigger changes than simple bug fixes. If you want to be sure the procedure below will work, just get the latest of the 5.2 series.]
-
Install PHP 5
Create a folder on your hard disk for PHP. I suggest “c:php” although you can use other names if you wish. Personally though, I prefer to avoid names with spaces in it, like “c:Program Filesphp” to avoid potential problems with programs that cannot handle such things. I will assume that you used c:php in this tutorial.
Extract all the files from the zip package into that folder. To do that simply double-click the zip file to open it, and drag all the files and folders to c:php.
-
Upgraders: Remove the Old PHP.INI File from Your Windows Directory
If you are upgrading to PHP 5 from an older version, go to your windows directory, typically c:windows, and delete any php.ini file that you have previously placed there.
-
Configuring PHP
Go to the c:php folder and make a copy of the file “php.ini-recommended”. Name the new file “php.ini”. That is, you should now have a file “c:phpphp.ini”, identical in content with “c:phpphp.ini-recommended”.
Note: if you are using Apache 1, you should either move the php.ini file to your windows directory, “C:Windows” on most systems, or configure your PATH environment variable to include “c:php”. If you don’t know how to do the latter, just move the php.ini file to the “c:windows” folder. You do not have to do this if you are using Apache 2, since we will include a directive later in the Apache 2 configuration file to specify the location of the php.ini file.
Use an ASCII text editor (such as Notepad, which can be found in the Accessories folder of your Start menu) to open “php.ini”. You may need to make the following changes to the file, depending on your requirements:
-
Enable Short Open Tags
Search for the line that reads:
short_open_tag = OffIf short_open_tag is set to “off”, tags like “" will not be recognised as the start tag for a PHP script. In such a case, to begin a PHP script, you will need to code your script with an opening tag like "commercial web hosts that support PHP have no issues with your scripts using “" as the open tag. To fix this, change it to the following:
short_open_tag = On -
Magic Quotes
By default, input data is not escaped with backslashes. That is, if your visitors enter an inverted comma (single quote) into your web form, the script will receive that unadorned inverted comma (single quote). This is for the most part desirable unless you special requirements. If you want your input data to have the backslash (“”) prefix, such as, for example, to mimic your web host’s settings, search for the following:
magic_quotes_gpc = Offand replace it with:
magic_quotes_gpc = OnDo not do this unless your web host has this setting as well. Even with the setting of “Off”, you can still use the addslashes() function in PHP to add the slashes for the specific pieces of data that need them.
-
Register Globals
A number of older scripts assume that all data sent by a form will automatically have a PHP variable of the same name. For example, if your form has an input field with a name of “something”, older PHP scripts assume that the PHP processor will automatically create a variable called $something that contains the value set in that field.
If you are running such scripts, you will need to look for the following field:
register_globals = Offand change it to the following:
register_globals = OnWARNING: Do NOT do this unless you have third party scripts that need it. When writing new scripts, it’s best to always code with the assumption that the register_globals item is set to “Off”.
-
Display Errors
On a “live” website, you typically want errors in your script to be silently logged to a PHP error file. On your own local machine, however, while you are testing and debugging a PHP script, it is probably more convenient to have error messages sent to the browser window when they appear. This way, you won’t miss errors if you forget to check the error log file.
If you want PHP to display error messages in your browser window, look for the following:
display_errors = OffAnd change it to:
display_errors = OnThis value should always be set to “Off” for a “live” website.
-
Session Path
If your script uses sessions, look for the following line:
;session.save_path = “/tmp”The
session.save_pathsets the folder where PHP saves its session files. Since “/tmp” does not exist on Windows, you will need to set it to a directory that does. One way is to create a folder called (say) “c:tmp” (the way you created c:php earlier), and point this setting to that folder. If you do that, change the line to the following:session.save_path = “c:tmp”Notice that in addition to changing the path, I also removed the semi-colon (“;”) prefix from the line.
Alternatively, you can find out the current TEMP folder on your computer and use that. Or create a “tmp” folder in your PHP directory, like “c:phptmp” and set the configuration file accordingly. The possibilities are endless. If you can’t decide, just create “c:tmp” and do as I said above.
-
SMTP Server
If your script uses the mail() function, and you want the function to successfully send mail on your local machine, look for the following section:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25; For Win32 only.
;sendmail_from = me@example.comChange it to point to your SMTP server and email account. For example, if your SMTP server is “mail.example.com” and your email address is “youremail@example.com”, change the code to:
[mail function]
SMTP = mail.example.com
smtp_port = 25
sendmail_from = youremail@example.comNote that after you do this, when your script tries to use the mail() function, you will need to be connected to your ISP for the function to succeed. If you do not modify the above lines and attempt to use mail() in your script, the function will return a fail code, and display (or log) the error (depending on how you configure php.ini to handle errors).
(Note that in Apache 1.x, the smtp_port line may not be present. If so, don’t include it.)
-
How to Configure Apache for PHP 5
There are two ways to set up Apache to use PHP: the first is to configure it to load the PHP interpreter as an Apache module. The second is to configure it to run the interpreter as a CGI binary. I will supply information for how you can accomplish both, but you should only implement one of these methods. Choose the module method if your web host also installed PHP as an Apache module, and use the CGI method if they have implemented it to run as a CGI binary.
-
Running PHP 5 as an Apache Module
To configure Apache to load PHP as a module to parse your PHP scripts, use an ASCII text editor to open the Apache configuration file, “httpd.conf”. If you use Apache 1.x, the file is found in “c:Program FilesApache GroupApacheconf”. Apache 2.0.x users can find it in “C:Program FilesApache GroupApache2conf” while Apache 2.2.x users can find it in “C:Program FilesApache Software FoundationApache2.2conf”. Basically, it’s in the “conf” folder of wherever you installed Apache.
Search for the section of the file that has a series of “LoadModule” statements. Statements prefixed by the hash “#” sign are regarded as having been commented out.
If you are using Apache 1.x, add the following line after all the LoadModule statements:
LoadModule php5_module “c:/php/php5apache.dll”If you are using Apache 2.0.x, add the following line after all the LoadModule statements:
LoadModule php5_module “c:/php/php5apache2.dll”If you are using Apache 2.2.x, add the following line instead:
LoadModule php5_module “c:/php/php5apache2_2.dll”Note carefully the use of the forward slash character (“/”) instead of the traditional Windows backslash (“”). This is not a typographical error.
If you are using Apache 1.x, search for the series of “AddModule” statements, and add the following line after all of them. You do not have to do this in any of the Apache 2 series of web servers.
AddModule mod_php5.cNext, search for “AddType” in the file, and add the following line after the last “AddType” statement. Do this no matter which version of Apache you are using. For Apache 2.2.x, you can find the “AddType” lines in the
section. Add the line just before the closing for that section.AddType application/x-httpd-php .phpIf you need to support other file types, like “.phtml”, simply add them to the list, like this:
AddType application/x-httpd-php .phtmlFinally, for those using one of the Apache 2 versions, you will need to indicate the location of your PHP ini file. Add the following line to the end of your httpd.conf file.
PHPIniDir “c:/php”Of course if you used a different directory for your PHP installation, you will need to change “c:/php” to that path. Remember to use the forward slash (“/”) here again.
If you are using Apache 1, you will have already placed your php.ini file in either the Windows directory or somewhere in your PATH, so PHP should be able to find it by itself. You can of course do the same if you are using Apache 2, but I find modifying the Apache configuration file a better solution than cluttering your c:windows directory or your PATH variable.
-
Running PHP 5 as a CGI Binary
If you have configured PHP 5 to run as an Apache module, skip forward to the next section. This section is for those who want to configure PHP to run as a CGI binary.
The procedure is the same whether you are using the Apache 1.x series or one of the 2.x series.
Search for the portion of your Apache configuration file which has the ScriptAlias section. Add the line from the box below immediately after the ScriptAlias line for “cgi-bin”. If you use Apache 2.2.x, make sure that the line goes before the closing for that
section. Note that if you installed PHP elsewhere, such as “c:Program Filesphp”, you should substitute the appropriate path in place of “c:/php/” (for example, “c:/Program Files/php/”). Observe carefully that I used forward slashes (“/”) instead of the usual Windows backslashes (“”) below. You will need to do the same.
ScriptAlias /php/ “c:/php/”Apache needs to be configured for the PHP MIME type. Search for the “AddType” comment block explaining its use, and add the AddType line in the box below after it. For Apache 2.2.x, you can find the AddType lines in the
section. Add the following line just before the closing for that section.AddType application/x-httpd-php .phpAs in the case of running PHP as an Apache module, you can add whatever extensions you want Apache to recognise as PHP scripts, such as:
AddType application/x-httpd-php .phtmlNext, you will need to tell the server to execute the PHP executable each time it encounters a PHP script. Add the following somewhere in the file, such as after the comment block explaining “Action”. If you use Apache 2.2.x, you can simply add it immediately after your “AddType” statement above; there’s no “Action” comment block in Apache 2.2.x.
Action application/x-httpd-php “/php/php-cgi.exe”Note: the “/php/” portion will be recognised as a ScriptAlias, a sort of macro which will be expanded to “c:/php/” (or “c:/Program Files/php/” if you installed PHP there) by Apache. In other words, don’t put “c:/php/php.exe” or “c:/Program Files/php/php.exe” in that directive, put “/php/php-cgi.exe”.
If you are using Apache 2.2.x, look for the following section in the httpd.conf file:
AllowOverride None
Options None
Order allow,deny
Allow from allAdd the following lines immediately after the section you just found.
AllowOverride None
Options None
Order allow,deny
Allow from all -
Configuring the Default Index Page
This section applies to all users, whether you are using PHP as a module or as a CGI binary.
If you create a file index.php, and want Apache to load it as the directory index page for your website, you will have to add another line to the “httpd.conf” file. To do this, look for the line in the file that begins with “DirectoryIndex” and add “index.php” to the list of files on that line. For example, if the line used to be:
DirectoryIndex index.htmlchange it to:
DirectoryIndex index.php index.htmlThe next time you access your web server with just a directory name, like “localhost” or “localhost/directory/”, Apache will send whatever your index.php script outputs, or if index.php is not available, the contents of index.html.
Restart the Apache Web Server
Restart your Apache server. This is needed because Apache needs to read the new configuration directives for PHP that you have placed into the httpd.conf file. The Apache 2.2 server can be restarted by doubleclicking the Apache Service Monitor system tray icon, and when the window appears, clicking the “Restart” button.
Testing Your PHP Installation
Create a PHP file with the following line:
Save the file as “test.php” or any other name that you fancy, but with the “.php” extension, into your Apache htdocs directory. If you are using Notepad, remember to save as “test.php” with the quotes, or the software will add a “.txt” extension behind your back.
Open your browser and access the file by typing “localhost/test.php” into your browser’s address bar. Do not open the file directly on the hard disk – you’ll only see the words you typed in earlier. You need to use the above URL so that the browser will try to access your Apache web server, which in turn runs PHP to interpret your script.
If all goes well, you should see a pageful of information about your PHP setup. Congratulations – you have successfully installed PHP and configured Apache to work with it. You can upload this same file, test.php, to your web host and run it there to see how your web host has set up his PHP, so that you can mimic it on your own machine.
If for some reason it does not work, check to see whether your PHP setup or your Apache setup is causing the problem. To do this, open a Command Prompt window (found in the “Accessories” folder of your “Start” menu) and run php-cgi.exe on test.php with a command line like “c:phpphp-cgi test.php” (without the quotes).
If invoking PHP from the command line causes a large HTML file with all the PHP configuration information to be displayed, then your PHP set up is fine. The problem probably lies with your Apache configuration. Make sure that you have restarted the Apache server after making configuration changes. Verify that you have configured Apache correctly by looking over, again, the instructions on this page and the steps given in How to Install and Configure Apache 1.x for Windows (for Apache 1.x users) or How to Install and Configure Apache 2 on Windows (for Apache 2.x users).
Learning PHP
The complete PHP reference manual can be obtained from the php website. You can refer to it online or download the entire set of HTML files for reference offline. As its name implies, it is a reference manual only. For tutorials, check out the PHP tutorials at thesitewizard.com. If you are new to writing PHP scripts, the following chapters may interest you:
- Writing Your First PHP Script gets you on your feet quickly with writing a useful and functional PHP script.
- Form Validation, Disabling Browser Caching, Embedding HTML Code in PHP introduces more features of PHP in a practical, usable way.
- How to Prevent Email Injection in Your PHP Form-to-Mail Scripts deals with how to avoid a security hole in PHP scripts that use the mail() function.
Have fun!
What is LAMP and Why LAMP
Mar 7th
Why LAMP
LAMP can significantly reduce or eliminate traditional IT costs for hardware acquisition and software license and applications maintenance costs. Very little infrastructure is required; LAMP is accessed through the internet from any web browser or web-enabled phone. As a service subscriber rather than software licensee, costly software upgrades that are generally required by the traditional ERP vendors every 2 to 3 years, are eliminated. There are no business disruptions during the no-cost LAMP upgrades. Subscribers can realize significant savings from LAMP. First time ERP clients avoid costly software and implementation costs, both initially and recurring, and can realize full benefits from an accelerated installation without having any infrastructure other than access to the internet from a web browser. Traditional legacy ERP customers can realize significant cost savings by reducing infrastructure, licenses, maintenance and application support within 12 months of implementation, an ROI that can be measured in months, not years. No ongoing expensive consulting and systems integration support is required. IMPART provides configuration and conversion on a fixed fee basis. IMPART doesn’t leave those other consulting, partners, behind that seemingly never goes away.
History and Pedigree
LAMP is among a select few enterprise software products developed entirely with open source software and protocols, resulting in lower costs to IMPART and customers alike. Built from the ground-up for SaaS delivery with multi-tenant (MT) architecture – it is not a rewrite or revamp of a single tenant (ST) system. LAMP has over 100 man-years of development during its five years of development effort to-date. Our software and services are built on over 30 years of experience in software design, development and delivery. The MT architecture allows IMPART to achieve an improved cost structure while maximizing the value to our customers by including improved and new business logic through rapid deployment of LAMP upgrades to the entire user base without upgrade cost or business interruption.
L A M P
The acronym LAMP refers to a solution stack of software programs, commonly free software programs, used together to run dynamic Web sites or servers:
Linux, (referring to OS kernel)
Apache, the Web server
MySQL, the database management system (or database server)
PHP (Sometimes Perl or Python), the programming language
L A M P Advantages
A Culture of Cooperation
The open source community and its culture of knowledge- and resource-sharing accelerates problem-solving. Community knowledgebases and libraries of sample application code help compress development time by enabling convenient reuse and adaptation.
Low Overhead
The compact LAMP component stack simplifies deployment and reduces processing overhead. Very tight integration between PHP and Apache, for instance, eliminates the need for application server software and in many instances eliminates an entire physical server tier.
Platform Portability
Because LAMP runs on a wide range of hardware platforms, users have maximum flexibility in deployment and server infrastructure design decisions. Of particular value is the option to deploy on clusters or grids of affordable x86-based servers. These utility computing architectures provide an optimized combination of efficient resource utilization, high availability, versatility and instant scalability.
Security and Stability
The LAMP server stack has a lower bug density — the number of bugs per thousand lines of code — than a baseline of 32 open source projects analyzed, according to a 2006 study by Coverity, a maker of code — analysis tools.
Over the years Saturn has developed standards, frameworks and reusable components for LAMP development. This helps Saturn to deliver economic and efficient solutions based on the LAMP stack.
PECL vs PEAR in PHP
Feb 21st
What the difference between Pecl and Pear?
——————————————————————————–
Pecl is a repository of PHP extensions, while PEAR is a repository of PHP classes.
Extensions are written in C and included into PHP, while classes are written in PHP and included into your code.
Cruise Control as PHPUnderControl vs. Hudson for PHP
Feb 20th
The Question:
I’m using PHPUnderControl (a Cruise Control plugin for PHP) as the CI server for PHP development. I use Ant to build my project. Main used features of PHPUC are
- PHPUnit test review
- PHPCS review
- Diagrams, PHP PMD, PHPCPD and some misc. thing.
What are the pros of Hudson vs. PHPUC? I heard it’s development is faster and more widely used day by day.
Answer 1:
We switched from PHPUC to Hudson about a year ago and have certainly enjoyed it. One of the main advantages is how easy it is to create and edit projects since it is all done through a nice web UI, no messing about in XML config files. You also don’t have to restart Hudson after changing settings, so I found it a lot easier and quicker to iterate on a project.
Another thing that really impressed me about Hudson was how many problems it can detect and point out for you. For example, more than once with PHPUC the build stopped working in such a way that the phpunit XML was no longer generated, but PHPUC happily used the old results for days or weeks until we realized. Hudson pointed this out right away saying that the log file had not been touched since the build started! When setting up projects too in the web UI, it will tell you if directories / SCM locations don’t exist as you type, and offer google-esque suggestions as to what you meant, spotting problems before you even save the configuration! Also if building periodically with a cron entry like “* 4 * * *” it will say, “did you really mean every minute of 4, or do you mean ’0 4 * * *’?”
That said, while the community and plugin ecosystem is rich, Hudson is also more generic than PHPUC so it isn’t as tightly integrated into PHP projects, but can happily publish code coverage and phpdoc (and any HTML report) reports via plugins.
Also, don’t forget that if a solution (PHPUC) isn’t broken, there may not be a need to fix it. However you can easily evaluate Hudson for initial impressions by downloading the .war file and running “java -jar hudson.war”, and you’ll be running an instance in under a minute.
Overall I feel it has made our builds and deployments more robust and repeatable, catching errors faster, and also speeding up many tasks (it can install slaves for itself via SSH and distribute jobs or run them concurrently!)
Answer 2:
Hudson as a nice Ui and can be configured 99% of the time through the webui. Last time I looked at cruise control, you had to sniff around in various xml files.
phpUnderControl – Continuous Integration for PHP
Feb 20th
phpUnderControl – Continuous Integration for PHP
phpUnderControl is an addon application for the continuous integration tool CruiseControl, which integrates some of the best PHP development tools. This project aims to make your first steps with CruiseControl and PHP as easy as possible. Therefore phpUnderControl comes with a command line tool that performs all modifications to an existing CruiseControl installation.
How-To (and How-Not-To) on Web Scraping
Feb 1st
Original Url: http://matthewturland.com/2008/03/12/scraping-html-with-dom/
A friend of mine who shall remain nameless pointed a post out to me on the PHP DZone web site recently. Noting that the article’s content was misinformed at best and downright ignorant at worst, even when examining it sheerly from the author’s knowledge of PHP as a language, this friend asked that I set the author straight.
I gladly obliged with a comment on the post, having become somewhat of an authority on the application topic myself. As much of an unorthodox practice as web scraping may be, there are some methodologies for it that are obviously better than others. The aforementioned post illustrates a lot of the ones to avoid, and my arguments against them.
Later, I randomly encountered a post on the blog at xml.lt on the topic of web scraping using the DOM extension. This article showcases recommended practices and reasoned arguments against bad (and unfortunately common) alternatives. The author comes across as being significantly more informed on both the language and the application in the article’s content and code examples.
If you’re looking for references on topic of web scraping with PHP, there’s always the article I wrote for the December 2007 issue of php|architect magazine, of which you can still purchase an electronic copy in PDF format. At some point, I also hope to write a short book on the subject. Until then, if you have related questions, you can generally reach me in the #phpc channel on Freenode, under the nick Elazar. I’m always glad to give out advice on web scraping and PHP, as I’m sure my good friend Jared Folkins (who is also my “Little Sis” from the PHPWomen php, web scraping
PHP session_start – Cannot send session cache limiter – headers already sent
Sep 21st
Issue:
Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at E:\php\code\admin.php:1) in E:\php\code\logolist\adminlogo.php on line 2