Welcome to PHP4IT
Main Menu
· Main Page
· Account Settings
· Forum
· Recommend Us
· Contact

Search


Solutions Categories
· All topics
· Database Import and Conversion with PHP (Feb 07, 2006)
· PHP Security (Mar 28, 2008)
· Printing (Jan 05, 2006)
· Windows PHP Solutions (Apr 04, 2008)


PHP4IT RSS Feed
Add the PHP4IT RSS Feed to your favorite RSS news reader!

  

Windows PHP Solutions



See all

Guide to Getting PHP 5 to work with IIS 6 with custom identity

Posted by: David on Friday, April 04, 2008 - 02:50 PM
Windows Php 
When you want PHP running in 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. 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. Go to the Identity tab and type in the User name and Password for the Domain user. Don't forget to prefix the username with the Domain. For instance (and for our example's sake):

MY_DOMAIN\my-user

Click OK and you will be asked to confirm the password again.

Now you'll want to make sure that you go to the server that has the target directory share and make sure all the permissions are set so that that MY_DOMAIN\my-user has the proper read or read/write permissions to that share.

Now back on the IIS server, go to Administrative Tools, Computer Management, expand Local Users and
Groups, go to Groups, and double-click on IIS_WPG (IIS Worker Process Group). You must add MY_DOMAIN\my-user to the group or else you may end up with the "Service Unavailable" message when you try to load the website.

Next go to Administrative Tools, Local Policies, and User Rights Assignment. Add MY_DOMAIN\my-user to the following policies of "Access this computer from the network", "Act as part of the operating system", and "Log on as a service".

Now go to the Properties of your website ("Default Web Site"), go to the Directory Security tab, and click on the Edit button for "Authentication and access control". If you have enabled anonymous access, make sure that the user name is set to MY_DOMAIN\my-user and the password is inputted correctly.

Restart IIS and voila, everything should work. If not make sure you check the Event Viewer on the destination server and see if the error messages there give you any clue as to why it's not working. If it doesn't work and you see Microsoft Authentication Security Account Logon Failure, then it's an issue with the user name and password specified there.

In your PHP script make sure that you escape backslashes for the network share you're trying to get to. For instance if you're tring to do a "opendir" on "\\my_server\my_files", you would do:

$dh = opendir("\\\\my_server\\my_files");

If you get PHP undefined function errors such as:

Fatal error: Call to undefined function mysql_connect() in C:\Inetpub\wwwroot\your_script.php

make sure you enabled the module (in this case extension=php_mysql.dll) in php.ini. If you did, it could well be that PHP is not reading the php.ini for some reason. You can make sure by looking at the output of php_info() and look for "Loaded Configuration File". If it says "(none)", then there's a problem. Make sure that "MY_DOMAIN\my-user" (in our example) has read permissions to the php.ini file AND the directory in which it resides- whether it be C:\PHP or C:\Windows.

If you have trouble executing commands using exec(), passthru(), etc, and the script seems to hang, make sure you have given the proper permissions to cmd.exe. So make sure that c:\windows, c:\windows\system32, and cmd.exe all can be read and executed by, in our example, MY_DOMAIN\my-user.

Good luck and I hope this guide helps. These instructions could work for other PHP and Microsoft Internet Information Server (IIS) versions as well, though I used PHP 5.2 and IIS 6.

Discuss/Submit Comment | Email This

Workaround Solution for the 10 User Windows XP Pro Maximum Connections Limit

Posted by: David on Saturday, January 14, 2006 - 04:30 PM
Windows Php 
If you're using Windows XP Pro as a file server and often run into problems where users can't connect to because the 10 maximum simulaneous connections have been reached (or 5 maximum connections for XP Home) - here's a quick and simple solution via PHP. The script basically uses "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 open files. Combine this with some sort of cron app for Windows (I recommend PyCron), and it works terrifically:

<?php
//disconnects windows share sessions with any sort of idle time and doesn't //have any open files; combine with cron for windows:
//from David @ PHP4IT.com 2006-01-14

$cmd = "net session";
exec($cmd, $out);
$disconnect_these = array();
foreach (
$out as $line){
    
$line = trim($line);
    if (
substr($line, 0, 2) != "\\\\"){ continue; }
    
$t = explode(':', $line);
    
$idle_time = substr($t[0], -1);
    
$open_files = explode(' ', $t[0]);
    
$open_files = $open_files[sizeof($open_files)-2];
    if (
$idle_time > 0 || !$open_files){
        
$computer_name = trim(substr($line, 0, 17));
        
$disconnect_these[] = $computer_name;
        
#print "$computer_name\t$idle_time\t$open_files\n";
    
}
}
if (
sizeof($disconnect_these)){
    foreach (
$disconnect_these as $disconnect){
        
passthru("net session $disconnect /delete /Y");
    }
}
#print_r($disconnect_these);
?>

Here's the sample crontab entry to run the script, which we're calling disconnect_idle_sessions.php, to run every minute:

* * * * * c:\php\php c:\php\disconnect_idle_sessions.php



Discuss/Submit Comment | Email This

PHP-based cron script for Windows

Posted by: david on Thursday, January 05, 2006 - 06:44 PM
Windows Php 
You can easily create your own php cron-like script for Windows. 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 " . $dir . "\\" . 'cron_exec.php';
$interval = 10;
do {
    
$output = shell_exec($cmd);
    
$cur_time_str = date("F d, Y \t h:i A");
    print
"$cur_time_str: $output\n\n";
    
write_log();
    
sleep($interval);
} while (
TRUE)

function
write_log()
{
    global
dir, $output, $cur_time_str;
    
$log_file = $dir . "\\logs\\cron" . date("Ymd") . '.log';
    
$fp = fopen($log_file, 'a');
    
fwrite($fp, $cur_time_str . "\n" . $output . "\n");
    
fclose($fp);
}
?>

----

Here's cron_exec.php:

<?php
//cron script that executes commands
$dir = "c:\\php";
$lock_file = $dir . "\\" . 'cron_exec.lock';
if (
file_exists($lock_file)){ die("Error: cron_exec.lock exists.\n"); }
$fp = fopen($lock_file, 'w');
$result = fwrite($fp, 'cron_exec.php is running');
fclose($fp);
mysql_pconnect($host, $user_name, $password);
$q = "select min(cqid) from command_queue where executed != 'Y'";
if (!
$result = mysql_query($q)){
    
unlink($lock_file);
    die(
mysql_error() . " SQL: $q");
}
list(
$cqid) = mysql_fetch_row($result);
if (
mysql_num_rows($result) == 0 || !$cqid){
    
unlink($lock_file);
    die(
"Nothing to be executed.");
}

$q = "select * from command_queue where cqid = $cqid";
if (!
$result = mysql_query($q)){
    
unlink($lock_file);
    die(
mysql_error() . " SQL: $q");
}
$row = mysql_fetch_assoc($result);
$cmd = "c:\php\php -q " . $row['command'] . ' ' . @$row['add_switch'];
$s_time = get_microtime();
$output = shell_exec($cmd);
$e_time = get_microtime();
$duration = sprintf("%01.2f", $e_time - $s_time);
$q = "update command_queue
        set executed = 'Y', duration = '$duration', date = sysdate()
        where cqid = $cqid"
;
if (!
$result = mysql_query($q)){
    
unlink($lock_file);
    die(
mysql_error() . " SQL: $q");
}
print
"Executed $cmd, output: $output\n";
unlink($lock_file);
?>

The MySQL table would have the following structure:

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| cqid       | int(10) unsigned |      | PRI | NULL    | auto_increment |
| command    | char(32)         | YES  |     | NULL    |                |
| add_switch | char(8)          | YES  |     | NULL    |                |
| executed   | char(1)          | YES  |     | N       |                |
| date       | datetime         | YES  |     | NULL    |                |
| duration   | char(32)         | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+


Discuss/Submit Comment | Email This