PHP Tips and Code Examples


See all

Guide to Getting PHP 5 to work with IIS with Custom Identity

Posted by: david on Saturday/November 29/2014 - 10:15 PM
PHP Code Tips

When you want PHP running in Microsoft Internet Information Services (IIS) to be able to read/write files on a Windows network share, you could run into a myriad of errors and problems. Hopefully this article will help you get everything working smoothly.

Here what we want is to have IIS run as a Domain user who has the proper access to the network share.

  1. After you have installed PHP (in ISAPI mode), go to the IIS Manager, Application Pools, right-click on DefaultAppPool (or the name of your application pool), and select Properties.
  2. Go to the Identity....

Adding the DBF dBase (or Any Custom) Extension in Linux (Red Hat, CentOS, or Fedora)

Posted by: david on Saturday/November 29/2014 - 02:58 PM
PHP Code Tips
The previous article explained how to convert and import data from DBF files into MySQL. It is actually simple enough to enable the dBase extension for PHP for Windows by just by uncommenting out a line in php.ini, but if you are using Red Hat/CenOS/Fedora, this is not as simple.

There is no RPM file that you can simply install to enable the dBase extension, so you have to recompile and build the PHP Linux RPM. Here are the steps to do this (note that this can work with other extensions that you can't find the....


Import/Convert DBF files to MySQL Using PHP

Posted by: david on Tuesday/November 25/2014 - 09:53 PM
PHP Code Tips

If you have been presented with the task of having to import a DBase/DBF - here is a two step way to approach the problem. First the MySql table structure needs to be created, and then the DBF is then read and inserted into the table. Step 1 involves the conversion the output from the "disp stru" command in FoxPro into a MySQL create table statement. Consider a example DBF file called "addresses.dbf"- and the "DISP STRU" of the file looks like this:
1 HNAME1 Character 30
2 ADDRESS Character 30
3 EXTRA Character 30
4 CITY Character 28
5 STATE Character 2
6 ZIP Character 10
The....


Read the Entire Article 1 Review/Comment

Windows Maximum Connections Limit Solution Using PHP

Posted by: david on Tuesday/November 25/2014 - 05:33 PM
PHP Code Tips

If you are using Windows 8, 7, or XP Pro as a file server and often run into problems where users can't connect to because the 20 or 10 maximum simultaneous connections have been reached (or even fewer connections for the Home versions of the OS) - here is a quick and simple solution via PHP. The script basically uses the command "net session", parses the output and looks for idle connections and disconnects them- in this case if they've been idle for a second or don't have any....

Cron for Windows Using PHP

Posted by: david on Tuesday/November 25/2014 - 02:30 PM
PHP Code Tips
You can easily create your own PHP cron-like script for Windows to emulate the functionality of Linux. The basic gist of it is you create a database table (or you can simply create a text file) that will hold the information about the application to execute- i.e. command, switch, executed flag column, date, & duration. I propose a 2 script solution: cron.php & cron_exec.php.



Cron.php would basically just call cron_exec.php every designated X seconds (or minutes, e.g.):

<?php //Runs cron_exec.php every XX seconds; put in Windows startup folder 

$dir = "c:\\php"; 
$cmd = "c:\\php\\php -q " .....
      

Printing from PHP directly to a PCL printer (Linux)

Posted by: david on Tuesday/November 25/2014 - 01:09 PM
PHP Code Tips


Let's say that you've built a web application with neat HTML tables & forms, and you want your users to be able to click on a button and send the page directly to a PCL printer from your app. Or you want the web application to automatically print the html form to a PCL printer. Here is how to do it with PHP:



  1. Output the html to a temporary file, e.g.

    fopen($tmp_file, "w+") or die ("Print error: Can't write file $tmp_file.");

  2. convert the html file into a postscript file....

Read the Entire Article 2 Reviews/Comments

Warning: mysql_connect() system error: 32 Fix

Posted by: david on Friday/April 01/2011 - 08:53 PM
PHP Code Tips
I have a PHP script that is executed by cron on one of our Linux servers. As part of its task, the script will run some queries from local MySQL database. But lately I've been seeing this error message (which is emailed to me) from the script:

Warning: mysql_connect(): Lost connection to MySQL server at 'sending authentication information', system error: 32 in /php/my-php-script.php

It's baffling because I don't get the error message all the time, just occasionally. When I execute the script from the Linux command line, it runs, no problem, no error....


PHP Convert Text Date to MySQL Date

Posted by: david on Wednesday/June 30/2010 - 07:49 PM
PHP Code Tips
In PHP, let's say you want to convert the text string "July 1, 2010" to the MySQL date format, you would use two functions- strftime() and strtotime().

strftime() takes 2 arguments- first is the date format, and for MySQL our format would be: '%Y-%m-%d'. The second argument is the integer timestamp- that's where the "magical" function strtotime() comes in. It will take a date text string and convert it into the timestamp integer value we need.

Thus, to convert "July 1, 2010" to "2010-07-01", the PHP code would be:

$mysql_date = strftime('%Y-%m-%d', strtotime('July....