Stevp’s Blog
A Stevp into the unknown

PHP Mail()

June 29th, 2008 by Stevp

I’ve been doing a lot this week with PHP’s built in mail function. One of the projects i’m working on will require automated emails to be sent at regular intervals, so i’ve been weighing up the best way to achieve this.

First things is first, i needed to enable the PHP mail options within the PHP.ini file.
We are looking for the “[mail function]” section of the ini file. This is nice an easy to set up, you just need to enter a couple of lines to reflect the location of your mail server and the sender address that PHP will use.

[mail function]
; For Win32 only.
SMTP = email.domain.co.uk
smtp_port = 25
; For Win32 only.
sendmail_from = auto@domain.co.uk
; For Unix only.  You may supply arguments as well (default: “sendmail -t -i”).
;sendmail_path =
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

Once these Lines have been added then a server restart is required for the changes to take effect.

Once rebooted we can write a simple page to test the configuration.

<?php
$to = "test@domain.co.uk";
$subject = "a test email";
$body = "this is a test email";
if (mail($to, $subject, $body)) {
echo("Message successfully sent!");
} else {
echo("Message delivery failed...");
}
?>

Put this somewhere on an accessible page and navigate to it in a browser. If all has been successful you should see a page simply saying “Message successfully sent” and await the arrival of your email.

There are many different things you can do with PHP mail. A quick search will help you find out how to make html messages and add elements such as a from address.

I said i wanted to send automated emails, so lets have a quick look at that. I very quickly found a simple solution, though im pretty certain it isnt the best way to do it -  I shall investigate more in the coming week.

The way i found to do it (in windows) was to write a very simple one line batch file and schedule it as a system task to repeat daily.
My file is simply named “dailyemail.bat” and the content of the file is

php c:\websites\project\dailyemail.php

This file needs to be scheduled on the webserver as the “php” operator is dependent on having php available. When i find a better way i’ll let you know.

Posted in How To, Today's Achievements | Comments Off

MSQL, PHP & Tick Boxes

June 19th, 2008 by Stevp

Have had a busy few weeks if you’ll forgive the lack of blogging of late. I’ve got a number of projects on the go at work. The main achievement of the week has been adding tick boxes (thats “checkboxes” for our US listeners) to a dynamic page written in php, pulled from a MySql database. I’ve done loads of reading and searching and found pieces of the puzzle all over the net. This video however was most useful, although it isnt quite what i’m after it helped get my head round a few things i was trying to achieve.

The scenario is such:
A page is generated giving a list of member in a particular group, with over a thousand members each being part of several groups. I want to add to be able to select a subset of member from each group (or even the whole group) and add a number of points to each member. This is effectively a point scoring system keeping only running totals..

How i achieved it:
First the page was written in php with a MySql query which displays the relevant list of members. Simply a query to read the list of members of the appropriate group then a couple of lines of PHP to write out the contents of the page. I used a For loop to cycle through each of the members found.

In the write out i wanted to add a tick box alongside each name, so as to select multiple members or use a floodfill. This was more difficult than i first thought. I easily added a tick box to each line, just a loop to write one after each entry, however i failed to make these do anything useful.
I did some searching on the net and found a script that was written a few years ago that makes the use of tick boxes much easier. The script seems that old that the URL of the author is no longer valid.

The script goes a lot of the way to achieving what i need. It allows multiple tick boxes to be linked together to make an array of values. This array is then sent as a POST variable to the next page. After implementing the script i was then in a position to use a “flood fill” option, which is most useful to me as group sizes are sometimes 35+.

Once the array is sent to the next page it needs to be interpreted and the selected records acted upon. On this page i used the PHP POST function to retrieve the array sent as a variable.

<?php print_r($_POST) ?>
This is a useful function i found which can be used to display the variables being passed on. A very handy troubleshooter.

I then used the PHP IMPLODE to change the array to a string a variable, this is a most handy function as it allows to be searched. The final step was to search my database looking for members that matched the selection, each member has unique identifier which i assigned as the value of the tick box, and add points to their running total. This was achieved with a simple SQL query which reads the current value and incremements it as necessary. Here is the relevant bit of code.

<?php
include '../config.php';        // includes the information to connect to the mysql database
$credit = $_POST[credit];    // grabs a number of credit to add to the total
$selection = implode(’,', $_POST['subset']);  //grabs the id of the subset of members
and converts to a string
//the query updates the records of the selected members by adding the new credit.
$updatequery = mysql_query(”UPDATE credits SET
total = total +”. $credit. ” WHERE `id` IN ($selection)”)
or die(mysql_error());
mysql_query($updatequery);
header(’Location:submitted.php’);   //page redirect
?>

Posted in Today's Achievements | Comments Off

ASP ASP ASP

May 23rd, 2008 by Stevp

Have been incredibly busy this last week. I’ve been working on several ASP applications and not had much time for anything else. The first is a logging system. Enties are made throughout the day to a MS access database through an ASP front end.  Once the entry is submitted a log is displayed to the end user showing the events for the day. As an admin section to the application i’ve added the facility to query past dates and bring up records. A simple application all in all but still took a good days work to complete.

Then, i’ve created an online form to replace one of the paper ones currently in use. Similar to above, the end user enters data into a form which is then passed to an ASP page which writes the details to a database and also to an email for the relevant staff.

Thirdly a more ambitious project, which is top secret at the moment, but will entail potentially hundreds of database entries daily. I think i’m going to approach this with MySQL not MS access, but havent quite got the details of the design ironed out yet.

And, lastly, I’ve been working on a design for a preference selection system for a department head to put pay to the current pen and paper system and hopefully make a few people’s lives that little bit less tedious.

Posted in Today's Achievements | 3 Comments »

Installing and Configuring IIS

May 11th, 2008 by Stevp

Earlier I’ve done a couple of How Tos which require IIS to already be installed. so I suppose I should cover this in a How To aswell.

To install IIS in Server 2003 you must add the “Application Server” role from the “Manage your server” page.
Select the “add role” option and then “application server”. Here you are asked if you wish to add Frontpage extensions and ASP.Net support. I always add the ASP support but tend not to use FP extensions. If in doubt, add both, or neither, or just one of them. A Lot of the projects I have done are built on databases, and ASP is essential for this.
You now have an IIS server.

Now, open up “Internet Information Services (IIS) Manager” from your Admin Tools Menu.

Lets create our first simple site.

Under the server name select the “Web Sites” folder and you should see a “Default Web Site”. Right clcik on this an open up its properties. The first thing I suggest doing is change the Description to something meaningful. On the “Home Directory” tab change the path to the folder you wish to use as your website. If you don’t already havea site to use, create a directory and point to that. Windows defaults to putting the websites within the C: drive of the server, however I suggest that this is moved onto a data drive to make backups easier and to allow plenty of space for big sites.

Now we can close IIS as we have our default site set up. Open up the directory you set for the site (assuming this is empty) create a new file called index.htm and add the code;


<html>
<head>
<title>Test Page </title>
</head>
<body>
<h2> This is a test page </h2>
</body>
</html>

We can now go to another pc on the LAN and in a browser open http://servername, in my case http://garydc and a test page should be displayed.

Posted in How To | 5 Comments »

New URL and Moving WordPress Blogs

May 11th, 2008 by Stevp

Just a quick one. Stevp’s Blog has now moved to a new URL, you should now be viewing at www.stevp.co.uk .

 width=

Changing the URL required a few steps, so here they are.
First I registered the new domain name and changed the site’s DNS entries to point at my web hosting server.
Here I created an “Add-on” domain. This gives my new URL a subdomain within my existing site and points the new address at it.

Now onto moving WordPress Blogs. Well this was easy, I followed the instructions from the WordPress site.

  • Change the URL in the WP Settings section to the new URL
  • Copy all of the data from the old subdomain to the new
  • Put an html redirect in the old location, so people can still find this page
  • change any specific link that reference the old URL, such as for pictures

Easy as that.

Posted in How To, Today's Achievements | 2 Comments »

New Moodle Nightmare and Disaster Recovery

May 7th, 2008 by Stevp

The last couple of days have been hectic. The planned Moodle migration was brought forward quite abruptly as the mainboard of the current webserver gave up and moved on to a better place. Migrations and disaster recovery are much easier when its not a real scenario. However, after a twelve hour day yesterday the vast majority of the websites have been moved to a new server and the Moodle install restored and upgraded.

I’ve learnt loads of IIS stuff in the last twenty four hours, certainly a lot more than I intended to. So here is a quick list of How Tos to look forward to

  • Application Pools
  • Enabling ASP
  • Anonymous user account access
  • IIS Host headers

and not to mention

  • Moodle installation
  • Moodle Upgrade

Posted in New Moodle, Today's Achievements | 3 Comments »

Installing MySQL Server on Windows Server 2003

May 4th, 2008 by Stevp

Earlier I wrote a How To detailing the installation of PHP. One of the biggest benefits of PHP is it’s ability to talk to databases. I use MySQL databases for several things, most notably for Moodle.

 width=

Here is another How To explaining the procedure I use to install MySQL Server.

This installation will be done on Windows Server 2003. First things is first, download the latest version of the MySQL server. I use the Community Server, there is an Enterprise version available to buy if you need more support or is more critical.
Perform the download and extract the installer to somewhere on your server. I tend to keep an “installations” folder on the data disk of all my servers with the programs it uses.

Start the installer running, I’m going to do a “Complete” install.
Form this point on I will explain the configuration I am familiar with and used for Moodle. I have chosen “Detailed Configuration” and then “Server Machine”. My intention is to put this installation onto the same server as a Moodle installation, however if high database use is required i’d suggest the “Dedicated Server” option. Next we want to select the option for “Multifunctional Database” and then an “OLTP configuration”. Keep the default port and enable strict mode (default) Now for moodle we need to change the default character set to “utf-8″. Next we want to install as a service and also include in the “Windows Path” variable. Now set a password to complete the installation.

Next we need to go back to the MySQL website and download the GUI Tools, then simply perform a complete install of this. Now before we can use our installation the server needs a Reboot.

We are now able to log in to MySQL Administrator with;
Server: Localhost
Username: root
Password: as set in installation.

Your server is now a MySQL Server. In a future post I will detail how to configure PHP and MySQL for Moodle.

Posted in How To | 2 Comments »

Installing PHP 5.2.6 for IIS

May 3rd, 2008 by Stevp

My primary use of Server 2003 is for webserving. I have no aversion to Linux / Apache but the network at work is all Windows. Installing PHP on Server 2003 can be a little awkward so I thought I’d put together another How To detailing the procedure I use.

 width=

For this I will assume you already have IIS installed and are able to access it’s websites from other pcs.

The first thing we need to do is to download the latest PHP version from the website at www.php.net. The top right of their site gives you a quick link to the latest current stable version. At the time of me writing this is 5.2.6. It is always best to use the latest version for new installations. Download the windows binaries zip package (not the installer) for this How To. Once downloaded unzip this to the c:\ drive of your webserver into a folder called “C:\php5″.

I’m going to keep the configuration as simple as possible using as many defaults as possible. Open up the folder and rename php.ini-recommended to php.ini.

 width=

That was the easy bit, now onto IIS, open it up from your Administrative tools menu.

  • Right click the “Web Service Extension” folder and “add new web service extension”
  • Name it php5, in the required files box add c:\php5\php5isapi.dll and allow the extension

There are still a few things we need to do in order to make php work.
On your default site in IIS, right click and go to properties \ home directory \ Configuration
Now choose the Add button. Set the executable path to “c:\php5\php5isapi.dll” and call the extension “.php”.
Click ok and check your default site is allowed to run scripts in the “execute permissions” menu. Next, in the “Documents” tab of the default site add “index.php” to the list of default start pages. (I generally have this at the top of the list)

 width=

Now onto the scarier bits
Open up the run command and type “regedit” to open the registry editor. Regedit can do serious damage to your windows installation so Be Careful. Open up HKLM \ Software and in here create a “PHP” folder, and in it a new string value of “IniFilePath” and give it a value of “c:\php5″. Close Regedit.
Now, open up your system properties page and on the advanced tab open up “Environment Variables”. On the end of the “Path” variable add “;c:\php5″. This will let the system use the php configuration from boot. Now you need to Reboot your server.

Deep breath - Job Done.

Now I suppose we best test this. Open up the directory of your default website and create a file “index.php” open it up with notepad and add the line<?php phpinfo(); ?>and save. Now go to a browser on a different PC and navigate to your default website. In my case http://stevpdc on my local network. If all has been successful you will be faced with a page showing you your php configuration. If you aren’t then reread carefully and check you’ve hit every step.

Posted in How To | 5 Comments »

DNS Forwarding Server 2003

April 29th, 2008 by Stevp

The trust relationships between two server 2003 domains was complicated and i got stuck, so instead turned my attentions to playing with DNS. The main thing i gained from the exercise was that i learnt how to set up and use DNS forwarders for name requests.

A situation where you would want to use a DNS forwarder might be having two domains on the same physical network, using seperate DNS servers. However you might have reason to want a Network A workstation talk to a Network B Workstation.

I will attempt to share the wealth with a quick How To.

Server 2003 DNS Forwarding Setup

First I will assume that you are performing this on a Windows Server 2003 Domain Controller. If you’re not doing this on a DNS server then there probably isnt much point anyway. The server is running the DNS server role within server 2003’s “manage server” screen.

Right, this is easy enough.
First open up your “Administrative tools” menu within the Control Panel and choose DNS.
Here you should see the name of your domain controller at the top of the list, in my example it’s StevpDC (as seen in the picture). Right click your DC and select properties. In the properties window move to the second tab “forwarders”. Here simply add the name of the second domain and the ip address of its DNS server. In my lab example I have MrGaryNet.local setup as a separate domain. So I add “MrGaryNet.local” into the “DNS Domain” section, insert the ip address and select Add. Finally apply.

Now, if StevpNet.local’s DNS server (StevpDC) receives a request looking for a workstation on the other network eg computer.mrgarynet.local, it will pass the request onto the named DNS server and GaryDC will handle the request.

Easy, huh?

Posted in How To | 4 Comments »

WP Youtube Plugin

April 29th, 2008 by Stevp

I’ve just added the Youtube Plugin from here. Again, another thanks to gumbald for pointing this out.

[youtube]dMH0bHeiRNg[/youtube]

Does anybody else think this guy looks quite a bit like MrGary? (Check out MrGary’s Blog.)

edit: And don’t forget the big round of applause and debt of gratitude owed to the plugin creator Mr Jens Törnell. Check him out at http://www.jenst.se

Posted in Today's Achievements | 5 Comments »

« Previous Entries