Click here to Skip to main content
13,090,299 members (92,008 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 14 Oct 2003

Compact Framework - PHP Writer

, 14 Oct 2003
Rate this:
Please Sign up or sign in to vote.
Use your PDA to write to PHP Server



I was recently working on a project where my device needed to wake up and run some routines, I also needed to be sure that app did not run at certain times. I really didn’t want to sit around to read local log files on the device so I created a quick routine that would log the info I needed to my Apache server. A little PHP, a little C# and yee-haw we're there.


You may want to understand PHP, or you could just write the backend in ASP.<stocktickeruk w:st="on"><stocktickerca w:st="on">NET which would also be easy. I chose not to do a web service simply to stay away from <stocktickeruk w:st="on"><stocktickerca w:st="on">SOAP overhead. That could be conceived as good or bad depending on your viewpoint, it was just my opinion and I mainly wanted to see if it was really simple to do.

Building the Code

To start with fire up VS.NET 2003 and create a new project:


I’ve called my app PHPApp for this tutorial. Select OK and you well be prompted with an option dialog for the exe for this app we’ll select PocketPC – Windows Application.


Select OK. And you will have a blank form in the designer.


Now Drop a button on the Form – listed below in the code as OK. Now select the Name property of the button and change it to OK.


The Form now Contains a Button:


Double Click the Button and add the code listed below for the button event as well as the static object and the Event for writing data to the PHP web page. I’ve put static text in the button event, but you could easily change it to anything you want.

Using the code

There are a number of ways to use this code. You could just call all of it on a event such as a button click, or you could setup a protected method such as I’ll describe here. I then setup a static variable to the project to hold the state which I pulled from and xml config file which I won’t get into in this article but will address in a following one.

Be sure to use:

  • using System.Net;
  • using System.IO;
  • using System.Text;
//I used a static variable so that in my other methods I could simply
//do and if statement so I’m going to list it here. 
//I’m going to setup the IsPHPEnable member variable to true to be sure it
//uses my method for logging.
using System.Net;
using System.IO;
using System.Text;
Namespace PHPApp
  public class Form1: System.Windows.Forms.Form
    private static bool IsPHPEnabled=false;
    public Form1()
/*Ok, now that we have the variable setup, lets move on to setting up the
Method itself that we’ll use when we want to log info to the Web Page I call
it WritePHPInfo and it takes a string for the message
      private static void WritePHPInfo(string message)
        ASCIIEncoding encoding=new ASCIIEncoding();
        string postData=DateTime.Now.ToShortTimeString();
        byte[] data = encoding.GetBytes(postData);
        HttpWebRequest myRequest;
        // Prepare web request...
          myRequest = (HttpWebRequest)WebRequest.Create
           ("http://ipaq/oos.php?touch=" + message + " at:" +
               DateTime.Now.ToShortTimeString() );

          //************note: I’m passing the call to ?touch, you’ll see 
          //below in the php where touch is used for input
          myRequest.Method = "POST";
          myRequest.ContentLength = data.Length;
          Stream newStream=myRequest.GetRequestStream();
          // Send the data.
        catch(WebException ex)
         //be sure you look in here I’m not using it for this example
/* Now lets look at how we can now call it, I’ll create a sample function,
lets say that were going to call it from a button on click event, so just
place a button on your app and call it OK for it’s name, go to the 
Action Page and double click tThe on click event
Private void OK_click(Object sender, System.EventArgs e)
  // Now lets fire off our method
  WritePHPInfo("Calling to Web method via PHP and HttpWebRequest");
/*Ok, so you can run PHP on Windows or Linux as well as Apache. I’m not going
to get into how to configure PHP to work with Windows but be assured it works
just fine. HINT: be sure you have php.exe setup for php extensions in IIS.
I chose to use Php on my Linux RedHat 9 machine so my path will be associated
with that.
On my system Apache is setup to look at /var/www/html as it root location for 
serving up pages. IIS would be under \InetPub\wwwroot\. I then proceeded to
create a quick PHP page.. Granted to be to hard on me with the PHP I just
threw it up and it has very limited Exception handlers. I call it 
logwriter.php I also created a form so I could test it from the actual 
HTML gui.
A Simple Form 
<form action="logwriter.php" method="post"> 
<input type="text" name="touch" size="10">
<input type="submit" name="submitbutton"> 
$filename ="/tmp/data.txt"; 
// NOTICE: I’m writing to /tmp/ because apache as rights to write to the 
/  file in this directory 
// First assign a name (and also a path if the file will not be in the 
// same directory) to the file that we're going to create. 
$myFile= fopen($filename,'a'); // Open the file for writing 
if(! $myFile)
  // Make sure the file was opened successfully 
  print ("File could not be opened."); 
//**note: here is the ‘touch’ we talked about in the .Net code. **
// Create data to be added to the text file. *********************

$string = "*****\n".$_REQUEST['touch']."\n*********\n"; 
fputs($myFile, $string); // Write the data ($string) to the text file 
fclose($myFile); // Closing the file after writing data to it 

Points of Interest

This wasn’t overly complex and only took a few minutes to bang out and saved me a little time standing in front of the device. I hope you might be able to use it in your next project.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalreading text file on mobile Pin
k36madman28-Aug-08 11:25
memberk36madman28-Aug-08 11:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 14 Oct 2003
Article Copyright 2003 by K-19
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid