<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Who... What... Where...</title>
	<atom:link href="http://www.yeltuor.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.yeltuor.com</link>
	<description>General things I find interesting</description>
	<lastBuildDate>Sun, 02 Aug 2009 09:58:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>VB WMI Query Computer Attributes</title>
		<link>http://www.yeltuor.com/articles/script/vb-wmi-query-computer-attri/</link>
		<comments>http://www.yeltuor.com/articles/script/vb-wmi-query-computer-attri/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 09:58:13 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[Scripting Snippets]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[VB]]></category>
		<category><![CDATA[WMI]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=117</guid>
		<description><![CDATA[By using a VB scripts we can perform a WMI (Windows Management Instrumentation) connection to a host to execute a query against.
Below is an example of a WMI query connecting to the host named &#8220;computer&#8221;, once connected will loop around each drive letter which is a hard drive displaying the drive letter and total disk [...]]]></description>
			<content:encoded><![CDATA[<p>By using a VB scripts we can perform a WMI (Windows Management Instrumentation) connection to a host to execute a query against.</p>
<p>Below is an example of a WMI query connecting to the host named &#8220;computer&#8221;, once connected will loop around each drive letter which is a hard drive displaying the drive letter and total disk space in GB</p>
<blockquote><p>On Error Resume Next<br />
Const HARD_DISK = 3</p>
<p>Set objWMIService = GetObject(&#8220;winmgmts:&#8221; &amp; &#8220;{impersonationLevel=impersonate}!\\&#8221; &amp; &#8220;computer&#8221; &amp; &#8220;\root\cimv2&#8243;)</p>
<p>if Err.Number = 0 then<br />
&#8216; Add WMI Query Script in here!</p>
<p>Set colDisks = objWMIService.ExecQuery (&#8220;Select * from Win32_LogicalDisk Where DriveType = &#8221; &amp; HARD_DISK &amp; &#8220;&#8221;)<br />
For Each objDisk in colDisks</p>
<p>Wscript.Echo objDisk.DeviceID + &#8221; Disk Size (GB): &#8221; + CStr(Round((((objDisk.Size / 1024)/1024)/1024),1))</p>
<p>Next</p>
<p>else<br />
Wscript.echo &#8220;Could not connect to &#8221; &amp; &#8220;computer&#8221; &amp; &#8221; error &#8221; &amp; Err.Number<br />
End If</p>
<p>Err.Clear</p></blockquote>
<p>The above WMI query can be combined with an Active Directory query to make the script more versatile (further information is <a href="/articles/script/vb-query-active-directory-objects">here</a>), an example of this script is below:</p>
<blockquote><p>On Error Resume Next<br />
Const ADS_SCOPE_SUBTREE = 2<br />
Const HARD_DISK = 3</p>
<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)<br />
Set objCommand =   CreateObject(&#8220;ADODB.Command&#8221;)<br />
objConnection.Provider = &#8220;ADsDSOObject&#8221;<br />
objConnection.Open &#8220;Active Directory Provider&#8221;</p>
<p>Set objCOmmand.ActiveConnection = objConnection<br />
objCommand.CommandText = “Select Name from ‘LDAP://OU=MyServers,DC=Domain,DC=co,DC=uk’ ” &amp; “Where objectClass=’computer’”<br />
objCommand.Properties(”Page Size”) = 1000<br />
objCommand.Properties(”Searchscope”) = ADS_SCOPE_SUBTREE<br />
Set objRecordSet = objCommand.Execute<br />
objRecordSet.MoveFirst</p>
<p>Do Until objRecordSet.EOF<br />
Wscript.Echo &#8220;Computer Name: &#8221; &amp; objRecordSet.Fields(&#8220;Name&#8221;).Value</p>
<p>Set objWMIService = GetObject(&#8220;winmgmts:&#8221; &amp; &#8220;{impersonationLevel=impersonate}!\\&#8221; &amp; objRecordSet.Fields(&#8220;Name&#8221;).Value &amp; &#8220;\root\cimv2&#8243;)</p>
<p>if Err.Number = 0 then</p>
<p>&#8216; Add WMI Query Script in here!</p>
<p>else<br />
Wscript.echo &#8220;Could not connect to &#8221; &amp; objRecordSet.Fields(&#8220;Name&#8221;).Value &amp; &#8221; error &#8221; &amp; Err.Number<br />
End If</p>
<p>Err.Clear<br />
objRecordSet.MoveNext<br />
Loop</p></blockquote>
<p>A number of WMI queries used can be found in the reference page <a href="/">WMI_Queries</a></p>

	Tags: <a href="http://www.yeltuor.com/articles/tag/active-directory/" title="Active Directory" rel="tag">Active Directory</a>, <a href="http://www.yeltuor.com/articles/tag/vb/" title="VB" rel="tag">VB</a>, <a href="http://www.yeltuor.com/articles/tag/wmi/" title="WMI" rel="tag">WMI</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/script/vb-wmi-query-computer-attri/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VB Query Active Directory Objects</title>
		<link>http://www.yeltuor.com/articles/script/vb-query-active-directory-objects/</link>
		<comments>http://www.yeltuor.com/articles/script/vb-query-active-directory-objects/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 19:56:58 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[Scripting Snippets]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[LDAP]]></category>
		<category><![CDATA[VB]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=89</guid>
		<description><![CDATA[In an Active Directory environment there is often a requirement to query objects within the AD  database, either returning selected attribute values or using this information to for further queries.
The same principles apply across any object type, the script below connects via LDAP, performs a query and returns selected values from Active Directory.
The below script [...]]]></description>
			<content:encoded><![CDATA[<p>In an Active Directory environment there is often a requirement to query objects within the AD  database, either returning selected attribute values or using this information to for further queries.</p>
<p>The same principles apply across any object type, the script below connects via LDAP, performs a query and returns selected values from Active Directory.</p>
<p>The below script will query all objects in the OU <em>MyServers</em> and subtree, in the domain <em>Domain.co.uk</em>, of type <em>computer</em>, returning the attributes <em>Name </em>and <em>Location</em>.</p>
<blockquote><p>Const ADS_SCOPE_SUBTREE = 2</p>
<p>Set objConnection = CreateObject(”ADODB.Connection”)<br />
Set objCommand = CreateObject(”ADODB.Command”)<br />
objConnection.Provider = “ADsDSOObject”<br />
objConnection.Open “Active Directory Provider”</p>
<p>Set objCOmmand.ActiveConnection = objConnection<br />
objCommand.CommandText = “Select Name, Location from ‘LDAP://OU=MyServers,DC=Domain,DC=co,DC=uk’ ” &amp; “Where objectClass=’computer’”<br />
objCommand.Properties(”Page Size”) = 1000<br />
objCommand.Properties(”Searchscope”) = ADS_SCOPE_SUBTREE<br />
Set objRecordSet = objCommand.Execute<br />
objRecordSet.MoveFirst<br />
Do Until objRecordSet.EOF</p>
<p>‘ Insert Operators here<br />
objRecordSet.MoveNext</p>
<p>Loop</p></blockquote>
<p>Once the query has been run the results can then be used for any requirement, a simple example below is to display  the name of the computer followed by the Location attribute stored in AD.</p>
<blockquote><p>……<br />
Do Until objRecordSet.EOF</p>
<p>‘ Insert Operators here<br />
Wscript.Echo “Computer Name: ” &amp; objRecordSet.Fields(”Name”).Value<br />
Wscript.Echo “Location: ” &amp; objRecordSet.Fields(”Location”).Value</p>
<p>objRecordSet.MoveNext<br />
Loop</p></blockquote>
<p>Any attribute of the object can be displayed as long as it was requested in the initial query; <em>Select Name, Location from….</em> , attribute names are LDAP attributes rather than field names seen in the Active Directory Users and Computers mmc. A list of attributes can be found here; <a href="/reference/active-directory-ldap-attributes/">Active Directory LDAP Attributes</a></p>
<p>One way to enhance this script further is for each computer selected perform a WMI query against it; <a href="/">VB WMI Query</a></p>

	Tags: <a href="http://www.yeltuor.com/articles/tag/active-directory/" title="Active Directory" rel="tag">Active Directory</a>, <a href="http://www.yeltuor.com/articles/tag/ldap/" title="LDAP" rel="tag">LDAP</a>, <a href="http://www.yeltuor.com/articles/tag/vb/" title="VB" rel="tag">VB</a>, <a href="http://www.yeltuor.com/articles/tag/windows/" title="Windows" rel="tag">Windows</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/script/vb-query-active-directory-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AD Command Line Queries</title>
		<link>http://www.yeltuor.com/articles/script/ad-command-line-queries/</link>
		<comments>http://www.yeltuor.com/articles/script/ad-command-line-queries/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 20:48:31 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[Scripting Snippets]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Query]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=84</guid>
		<description><![CDATA[There are a number of ways to query the Active Directory database to export objects and there attributes. One of the most common groups of tools are provided by Microsoft; dsget and dsquery.
Query Group Members
To display the member of a specific group use the dsget group command
dsget group &#8220;CN=My Group,OU=Domain Groups,DC=Domain,DC=co,DC=uk&#8221; -members
The above command will [...]]]></description>
			<content:encoded><![CDATA[<p>There are a number of ways to query the Active Directory database to export objects and there attributes. One of the most common groups of tools are provided by Microsoft; <em>dsget</em> and <em>dsquery.</em></p>
<h3>Query Group Members</h3>
<p>To display the member of a specific group use the <em>dsget group</em> command</p>
<blockquote><p>dsget group &#8220;CN=My Group,OU=Domain Groups,DC=Domain,DC=co,DC=uk&#8221; -members</p></blockquote>
<p>The above command will list the UPN of the users in <em>My Group</em>, if more information is required from each of the users within <em>My Group</em> the output can be piped into another dsget command</p>
<p>To display the member of a specific group use the <em>dsget group</em> command</p>
<blockquote><p>dsget group &#8220;CN=My Group,OU=Domain Groups,DC=Domain,DC=co,DC=uk&#8221; -members | dsget user -upn -display -disabled -acctexpires</p></blockquote>
<p>This commnd above will list the users in the group <em>My Group</em> with there UPN, display name, if the account is disabled and when the account expires.</p>

	Tags: <a href="http://www.yeltuor.com/articles/tag/active-directory/" title="Active Directory" rel="tag">Active Directory</a>, <a href="http://www.yeltuor.com/articles/tag/cli/" title="CLI" rel="tag">CLI</a>, <a href="http://www.yeltuor.com/articles/tag/query/" title="Query" rel="tag">Query</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/script/ad-command-line-queries/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SVCHOST High CPU Usage Issue</title>
		<link>http://www.yeltuor.com/articles/how-to/svchost-high-cpu-usage-issue/</link>
		<comments>http://www.yeltuor.com/articles/how-to/svchost-high-cpu-usage-issue/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 22:18:01 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[Automatic Updates]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=75</guid>
		<description><![CDATA[There are a number of threads in the Microsoft Windows Update Newsgroup and around other well known sites regarding the infamous SVCHOST Issue!
The main issue that people are reporting is the high CPU usage from the process SVCHOST.exe, generally fluctuating between 20 and 85 percent CPU usage, in some cases using all of the CPU [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p align="left">There are a number of threads in the Microsoft Windows Update Newsgroup and around other well known sites regarding the infamous SVCHOST Issue!</p>
<p align="left">The main issue that people are reporting is the high CPU usage from the process <em>SVCHOST.exe</em>, generally fluctuating between 20 and 85 percent CPU usage, in some cases using all of the CPU cycles and leaving the computer un-usable.<!--break --></p>
<p align="left">The cause of this is the Automatic Update service during its detection process; this is where the client PC either checks in to the local WSUS server or accesses the Windows Update site and determines which MS updates are required from the update catalogue.</p>
<p align="left"><em>All of this information is very good&#8230;</em> I hear you say, however where is the fix!</p>
<p align="left">Ok, after much web crawling and some testing, below is the method I have used for my clients which has proved sucessfull:</p>
<ol>
<li>
Firstly stop the Automatic Update service either through services.msc or with the command line <em>net stop &#8220;Automatic Updates&#8221;. </em>This will ensure the update process is as quick as possible.</li>
<li>
Next apply the first of two patches; an update to the Windows Update Agent, the current version is 2.0, this patch updates it to version 3.0 which is pushed out via WSUS v3.0:</p>
<ul>
<li>
<a title="WUA 3.0 - x86" href="http://download.windowsupdate.com/v7/windowsupdate/redist/standalone/WindowsUpdateAgent30-x86.exe">Windows Update Agent 3.0 &#8211; x86</a>
</li>
<li>
<a title="WUA 3.0 - x64" href="http://download.windowsupdate.com/v7/windowsupdate/redist/standalone/WindowsUpdateAgent30-x64.exe">Windows Update Agent 3.0 &#8211; x64</a>
</li>
<li>
<a title="WUA 3.0 - ia64" href="http://download.windowsupdate.com/v7/windowsupdate/redist/standalone/WindowsUpdateAgent30-ia64.exe">Windows Update Agent 3.0 &#8211; ia64</a>
</li>
</ul>
</li>
<li>Apply the hotfix for this issue, originally stated in <a title="KB916089" href="http://support.microsoft.com/kb/916089/">KB916089</a> and then superseded by <a title="KB927891" href="http://support.microsoft.com/kb/927891">KB927891</a> which provides an update for the Msi31.dll (exact name depends on operating version) with basically a more efficient detection algorithm in place:
<ul>
<li>
<a title="KB927891 - x86" href="http://www.microsoft.com/downloads/details.aspx?FamilyId=7A81B0CD-A0B9-497E-8A89-404327772E5A">Hotfix KB927891 &#8211; x86</a>
</li>
<li>
<a title="KB927891 - x64" href="http://www.microsoft.com/downloads/details.aspx?FamilyId=ECF96769-BEB6-4F7F-AC92-5BAEFB5632A3">Hotfix KB927891 &#8211; x64</a>
</li>
</ul>
</li>
<li>
Finally reboot the client PC and start the Automatic Update service if it is not set to start automatically.
</li>
</ol>
<div>I hope this solution is effective for everyone who reads this article as it was for me, and there are only one statement to make; <em>Thanks again MS&#8230; another fine mess you have gotten us into.. however at least you are keeping us in our jobs!</em></div>
</div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/automatic-updates/" title="Automatic Updates" rel="tag">Automatic Updates</a>, <a href="http://www.yeltuor.com/articles/tag/windows/" title="Windows" rel="tag">Windows</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/how-to/svchost-high-cpu-usage-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create a Custom Windows Service</title>
		<link>http://www.yeltuor.com/articles/how-to/create-a-custom-windows-service/</link>
		<comments>http://www.yeltuor.com/articles/how-to/create-a-custom-windows-service/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 22:12:59 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[Services]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=72</guid>
		<description><![CDATA[This article describes how to create a service on either Windows NT, 2000 or XP (untested on Vista). The service can run any application required and can be configured as any other service on the system can.
This information has been extracted from the Microsoft article KB137890, and requires two programs that can be found in [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p>This article describes how to create a service on either Windows NT, 2000 or XP (untested on Vista). The service can run any application required and can be configured as any other service on the system can.</p>
<p>This information has been extracted from the Microsoft article KB137890, and requires two programs that can be found in the Windows NT resource kit (or <a title="srvany.zip" href="http://www.yeltuor.com/s/files/srvany.zip" target="_blank">here</a>)</p>
<ul>
<li>SRVANY.EXE &#8211; Allows any application to run as a service</li>
<li>INSTSRV.EXE &#8211; Creates the system service</li>
</ul>
<h3>Create the service</h3>
<p>Use the INSTSRV.EXE application to create the system service, it requires two parameters; the first is the name of the service you want to create, the second is always the path to the SRVANY.EXE application.</p>
<blockquote><p>INSTSRV.EXE Notepad c:\SRVANY.EXE</p></blockquote>
<h3>Set the application as a service</h3>
<p>Next edit the registry and enter the path to the application that is required to run as a service. The location is the registry depends on the name of the service that was defined above; in this case <em>Notepad</em></p>
<blockquote><p>Run <em>regedit.exe</em></p>
<p>Navigate to HKLM\SYSTEM\CurrentControlSet\Services\Notepad</p>
<p>Create a new key called <em>Parameters</em></p>
<p>Under the new key create a new string value called <em>Application</em> and set its value to the path of the application <em>c:\WINDOWS\NOTEPAD.EXE</em></p></blockquote>
<p>Once created this service can be managed through the services mmc to set its startup type, recovery response etc.</p>
<p>To make this process easier, the following code can be placed into a batch file and run (with the correct parameters) to create the service and congfiure the registry. It assumes that INSTSRV.EXE is already in a search path and that SRVANY.EXE is located in the root of the C drive.</p>
<blockquote><p>@echo off</p>
<p>rem #################################<br />
rem ## This batch file created a new system service ##<br />
rem ## ensure the correct parameters are usedÂ Â Â Â Â Â         ##<br />
rem #################################</p>
<p>rem Parameter 1 should be the name of the service<br />
rem Parameter 2 should be the path to the executable</p>
<p>echo Creating Service<br />
INSTSRV.EXE %1 c:\SRVANY.EXE</p>
<p>echo Creating Registry Keys<br />
reg add &#8220;HKLM\SYSTEM\CurrentControlSet\Services\%1\Parameters&#8221;<br />
reg add &#8220;HKLM\SYSTEM\CurrentControlSet\Services\%1\Parameters&#8221; /v Application /d %2</p></blockquote>
</div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/services/" title="Services" rel="tag">Services</a>, <a href="http://www.yeltuor.com/articles/tag/windows/" title="Windows" rel="tag">Windows</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/how-to/create-a-custom-windows-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows CLI &#8211; Tasklist</title>
		<link>http://www.yeltuor.com/articles/how-to/windows-cli-tasklist/</link>
		<comments>http://www.yeltuor.com/articles/how-to/windows-cli-tasklist/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 22:09:31 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Services]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=70</guid>
		<description><![CDATA[The command line tool tasklist without any switches will simply display the local currently running processes and if the /S parameter is used process&#8217;s running on a remote machine can be queried.
Now all of this isn&#8217;t really worth writing about you might say, however one switch that I found useful was the command tasklist /svc [...]]]></description>
			<content:encoded><![CDATA[<p>The command line tool <em>tasklist</em> without any switches will simply display the local currently running processes and if the <em>/S</em> parameter is used process&#8217;s running on a remote machine can be queried.</p>
<p>Now all of this isn&#8217;t really worth writing about you might say, however one switch that I found useful was the command <em>tasklist /svc</em> ; this will display a list of all running processes along with any related services attached to that process.</p>

	Tags: <a href="http://www.yeltuor.com/articles/tag/cli/" title="CLI" rel="tag">CLI</a>, <a href="http://www.yeltuor.com/articles/tag/services/" title="Services" rel="tag">Services</a>, <a href="http://www.yeltuor.com/articles/tag/windows/" title="Windows" rel="tag">Windows</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/how-to/windows-cli-tasklist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Batch File &#8211; For Loop</title>
		<link>http://www.yeltuor.com/articles/script/batch-file-for-loop/</link>
		<comments>http://www.yeltuor.com/articles/script/batch-file-for-loop/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 22:02:52 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[Scripting Snippets]]></category>
		<category><![CDATA[Batch File]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=67</guid>
		<description><![CDATA[I will commonly write small batch files to automate tasks that are repetitive, due to this I find myself using the for loop a lot. Below are a few examples of when and where the for loop can be used effectively.
Looping through entries in a file
This example will take a standard text file (MyTextFile.txt) and [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p class="MsoNormal">I will commonly write small batch files to automate tasks that are repetitive, due to this I find myself using the <em>for loop</em> a lot. Below are a few examples of when and where the for loop can be used effectively.</p>
<h3>Looping through entries in a file</h3>
<p>This example will take a standard text file (MyTextFile.txt) and then loop through all lines until the end of the file is reached. For each of the lines the first word (represented by <em>%%A</em>) will be printed out to the screen and ping&#8217;ed.</p>
<blockquote><p>FOR /F &#8220;tokens=1&#8243; %%A IN (C:\MyTextFile.txt) DO (</p>
<p>echo %%A</p>
<p>ping %%A</p>
<p>)</p></blockquote>
<p>An example of the text file is simply:</p>
<blockquote><p>COMPUTER01<br />
COMPUTER02<br />
COMPUTER03<br />
COMPUTER04</p></blockquote>
<p>By changing the <em>tokens</em> value, which word on a line that is used will change. By setting the value of tokens from <em>1</em> to <em>1,2</em> both the first and second words per line will be available via the parameters <em>%%A</em> and <em>%%B</em>. When using the following input file, within the loop %%A will be the computer name and %%B will be the IP address.</p>
<blockquote><p>COMPUTER01 192.168.1.100<br />
COMPUTER02 192.168.1.101<br />
COMPUTER03 192.168.1.102</p></blockquote>
<p>To ignore selected lines, i.e. lines that are actually comments use the <em>eol </em>parameter, the following for loop will ignore all lines that start with a colon:</p>
<blockquote><p>FOR /F &#8220;tokens=1 eol=:&#8221; %%A IN (C:\MyTextFile.txt) DO (</p>
<p>echo %%A</p>
<p>)</p></blockquote>
<p>If the input file has a number of header lines that should not be included the <em>skip</em> parameter should be used, for following will exclude the first 3 files of the input file:</p>
<blockquote><p>FOR /F &#8220;tokens=1 eol=: skip=3&#8243; %%A IN (C:\MyTextFile.txt) DO (</p>
<p>echo %%A</p>
<p>)</p></blockquote>
<p>To override the default delimiters (space and tab) the <em>delims</em> parameter should be used. The following will ignore the space delimiter and instead delimiter by comma:</p>
<blockquote><p>FOR /F &#8220;tokens=1 delims=,&#8221; %%A IN (C:\MyTextFile.txt) DO (</p>
<p>echo %%A</p>
<p>)</p></blockquote>
</div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/batch-file/" title="Batch File" rel="tag">Batch File</a>, <a href="http://www.yeltuor.com/articles/tag/loop/" title="Loop" rel="tag">Loop</a>, <a href="http://www.yeltuor.com/articles/tag/windows/" title="Windows" rel="tag">Windows</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/script/batch-file-for-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Email Advanced</title>
		<link>http://www.yeltuor.com/articles/how-to/php-email-advanced/</link>
		<comments>http://www.yeltuor.com/articles/how-to/php-email-advanced/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 22:48:10 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=63</guid>
		<description><![CDATA[For a simple introduction to the mail() function see the  PHP_Advanced article. This article describes some of the more advanced features  that can be achieved through the mail function.

Assign Names to email addresses
When receiving an email you will notice that the To field often  contains a name rather than the email address [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p>For a simple introduction to the <strong>mail()</strong> function see the  PHP_Advanced article. This article describes some of the more advanced features  that can be achieved through the <em>mail</em> function.</p>
<p><a title="Assign_Names_to_email_addresses" name="Assign_Names_to_email_addresses"></a></p>
<h3>Assign Names to email addresses</h3>
<p>When receiving an email you will notice that the <em>To</em> field often  contains a name rather than the email address it was send to.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?PHP<br />
mail(&#8216;bob@email.com&#8217;, &#8216;Test email&#8217;,<br />
&#8216;This is a test email&#8217;,<br />
&#8220;To: Bob Jones &lt;bob@email.com&gt;\n&#8221; .<br />
&#8220;From: Jane Jones &lt;jane@email.com&gt;\n&#8221; .<br />
&#8220;cc: Another Person &lt;another@email.com&gt;\n&#8221; .<br />
&#8220;Bcc: Yet Another &lt;more@email.com\n&gt;&#8221;);<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="HTML_Emails" name="HTML_Emails"></a></p>
<h3>HTML Emails</h3>
<p>The next stage is sending HTML email messages, this allows for standard HTML  tags to be used when composing the message content. When sending a message in  HTML it must be declared that is it HTML in the header of the email, this is  done through both the <em>Content-type:</em> and <em>MIME-Version headers&#8217;:</em></p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?PHP<br />
mail(&#8216;bob@email.com&#8217;, &#8216;Test email&#8217;,<br />
&#8216;&lt;html&gt;&lt;body&gt;&lt;b&gt;Hello! World&lt;/b&gt; \n &lt;i&gt;this is a test email&lt;/i&gt;&lt;/body&gt;&lt;/html&gt;&#8217;,<br />
&#8220;MIME-Version: 1.0\n&#8221; .<br />
&#8220;Content-type: text/html; charset=iso-8859-1&#8243;);<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p>The <em>MIME-Version</em> (<strong>M</strong>ulitpurpose  <strong>I</strong>nternet <strong>M</strong>ail <strong>E</strong>xtensions)  header indicates that the email follows the internet standards, following that  the <em>Content-type</em> header can declare the format being used;  <em>text/html;</em> followed by the <a class="external text" title="Character-sets" rel="nofollow" href="http://www.iana.org/assignments/character-sets" target="_blank">character set</a> being used <em>charset=iso-8859-1</em></p>
<p><a title="Mixed_Format_Emails" name="Mixed_Format_Emails"></a></p>
<h3>Mixed Format Emails</h3>
<p>Although the majority of email clients support HTML email messages, there are  some that don&#8217;t. The mixed format ensures that the email clients that do support  it see the HTML formatted message, where as the ones that don&#8217;t see a plain text  version.</p>
<p>The technique involved is to actually send two versions of the message and  rely on the email client to read and understand <em>Content-Type:  multipart/alternative;</em> header which will make the client only display the  supported version.</p>
<blockquote><p>*** PHP code not fully completed yet ***</p></blockquote>
<p><a title="Emailing_Attachments" name="Emailing_Attachments"></a></p>
<h3>Emailing Attachments</h3>
<p>Emailing file Attachments work in the same way that mixed format email  messages do. The header <em>Content-Type: multipart/mixed;</em> is used and the  message split into two parts; one the message and the other the file  attachment(s).</p>
<p>This is more complicated than previous email examples, all the steps required  are explained below. The examples assume that the email details including the  file to be emailed have been submitted to the PHP page from another page.</p>
<blockquote><p>$to      = $_POST['to'];<br />
$from    = $_POST['from'];<br />
$subject = $_POST['subject'];<br />
$message = $_POST['message'];</p></blockquote>
<p><a title="Attributes_of_the_file_attachment" name="Attributes_of_the_file_attachment"></a></p>
<h4>Attributes of the file attachment</h4>
<p>The first stage is to extract the required attributed from the file that has  been passed. The file details in PHP are stored in an array named  <em>$_FILES</em> which are extracted to variables.</p>
<blockquote><p>// example: /tmp/phpfile12345 &#8211; tmp file name and loc where uploaded<br />
$file_loc  = $_FILES['fileatt']['tmp_name'];<br />
// example: text/text &#8211; will vary depending on file type<br />
$file_type = $_FILES['fileatt']['type'];<br />
// example: mywork.txt &#8211; always the name of the file<br />
$file_name = $_FILES['fileatt']['name'];</p></blockquote>
<p><a title="Extract_data_from_file_attachment" name="Extract_data_from_file_attachment"></a></p>
<h4>Extract data from file attachment</h4>
<p>The data within the file is required to be placed into a variable then used  Base64 encoding to convert (possible) binary data into text. The <a class="external text" title="PHP function is_uploaded_file" rel="nofollow" href="http://uk.php.net/is_uploaded_file" target="_blank">is_uploaded_file</a> function is  used to ensure that the file <em>was</em> in fact uploaded by an http get  command, this helps to ensure no malicious activity.</p>
<blockquote><p>if (is_uploaded_file($file_loc)) {<br />
// Read the file in &#8216;rb&#8217; read binary<br />
$file = fopen($file_loc,&#8217;rb&#8217;);<br />
$filedata = fread( $file <em>(comma)</em> filesize ($file_loc));<br />
// Base64 encode the file data<br />
$filedata = chunk_split(base64_encode($filedata));</p></blockquote>
<p>The data is now in a format that is ready to be emailed, the next stage is  producing the standard mail parameters.</p>
<p><a title="Producing_mail_function" name="Producing_mail_function"></a></p>
<h4>Producing mail function</h4>
<p>The basic mail parameters are set in the same manor, the diffrences come in  the <em>header</em> and <em>message</em> parameters</p>
<p>The header parameter contains the <em>MIME</em> version, the  <em>Content-Type: multipart/mixed;</em> declares that there will be an  attachment, finally the boundary string (containing random text) is used as a  marker to split the message into the two sections.</p>
<blockquote><p>&#8220;\nMIME-Version: 1.0\n&#8221; .<br />
&#8220;Content-Type: multipart/mixed;\n&#8221; .<br />
&#8221; boundary=\&#8221;==Multipart_Boundary_x45985365x\&#8221;";</p></blockquote>
<p>The message section starts with a declaration which MIME compatible email  clients will not show, next is the Multipart Boundary string denoting the  beginning of the first section. Following this the usual header information is  declared, following by the desired message text.</p>
<blockquote><p>&#8220;This is a multi-part message in MIME format. you should not see this\n\n&#8221; .<br />
&#8220;&#8211;==Multipart_Boundary_x45985365x\n&#8221; .<br />
&#8220;Content-Type: text/plain; charset=\&#8221;iso-8859-1\&#8221;\n&#8221; .<br />
&#8220;Content-Transfer-Encoding: 7bit\n\n&#8221; .<br />
&#8220;This is the message contents, there should be a file attached to this message&#8221;</p></blockquote>
<p>After the text of the message, the next part is the message attachment which  follows the same format as above.</p>
<blockquote><p>&#8220;&#8211;==Multipart_Boundary_x45985365x\n&#8221; .<br />
&#8220;Content-Type: {$file_type};\n&#8221; .<br />
&#8221; name=\&#8221;{$file_} \n&#8221; .<br />
&#8220;Content-Disposition: attachment;\n&#8221; .<br />
&#8221; filename=\&#8221;{$file_name}\&#8221;\n&#8221; .<br />
&#8220;Content-Transfer-Encoding: base64\n\n&#8221; .<br />
$filedata . &#8220;\n\n&#8221; .<br />
&#8220;&#8211;==Multipart_Boundary_x45985365x&#8211;\n&#8221;;</p></blockquote>
<p>Then message should always have the message boundary string followed by  <strong>&#8211;</strong> to signify the end.</p>
<p>To see the fully working source code, please see <a class="external text" title="PHP Email Code" rel="nofollow" href="http://www.yeltuor.com/s/files/PHP_Mail.rar">here</a></p>
<p>For more information on different MIME types see <a class="external text" title="MIMe types" rel="nofollow" href="http://www.ietf.org/rfc/rfc2045.txt" target="_blank">here</a><br />
Many thanks to the tutorials where this information came from, <a class="external text" title="w3schools" rel="nofollow" href="http://www.w3schools.com/php/default.asp" target="_blank">W3Schools</a> &amp; <a class="external text" title="PHP Net" rel="nofollow" href="http://uk.php.net/manual/en/introduction.php" target="_blank">PHP</a> &amp; <a class="external text" title="Sitepoint" rel="nofollow" href="http://www.sitepoint.com/article/advanced-email-php" target="_blank">sitepoint</a></div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/email/" title="Email" rel="tag">Email</a>, <a href="http://www.yeltuor.com/articles/tag/php/" title="PHP" rel="tag">PHP</a>, <a href="http://www.yeltuor.com/articles/tag/web/" title="Web" rel="tag">Web</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/how-to/php-email-advanced/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Advanced</title>
		<link>http://www.yeltuor.com/articles/script/php-advanced/</link>
		<comments>http://www.yeltuor.com/articles/script/php-advanced/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 22:37:40 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[Scripting Snippets]]></category>
		<category><![CDATA[Cookies]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Files]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Sessions]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=58</guid>
		<description><![CDATA[This article will guide you through some of the more complex features of PHP. a more basic introduction article is also available.

User Data
When browsing the internet you will come across some websites that store information about you or your visits to the site. There are two main methods of storing this data, these are through [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p>This article will guide you through some of the more complex features of PHP. a more basic introduction article is also available.</p>
<p><a title="User_Data" name="User_Data"></a></p>
<h3>User Data</h3>
<p>When browsing the internet you will come across some websites that store information about you or your visits to the site. There are two main methods of storing this data, these are through <strong>Cookies</strong> and <strong>Sessions</strong>.</p>
<p><a title="Cookies" name="Cookies"></a></p>
<h4>Cookies</h4>
<p>Cookies are the older of the two methods, it consists of a client side file that the browser writes to. With PHP you can create new cookies and also retrieve existing values, examples of both are below</p>
<p>To create new cookies the <strong>setcookie</strong> function is used, the main parameters are:</p>
<ul>
<li>Name &#8211; The Name of the cookie</li>
<li>Value &#8211; Data that the cookie is to store</li>
<li>Expire &#8211; Time &amp; Date when the cookie will expire</li>
</ul>
<p>When creating cookies in PHP you must call the setcookie function <strong>before</strong> any HTML tags are used in the page, an example is shown below:</p>
<blockquote><p>
	 &lt;?PHP<br />
	setcookie(&#8220;yeltuor&#8221;, &#8220;MyValue&#8221;, time()+86400);<br />
	?&gt;<br />
	&lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	The cookie named yeltuor has been &#8217;set&#8217;<br />
	It will expire 24 Hours (86400 seconds) from now.<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>With the cookie now created, the next stage is to read back the values stored in it. The values in the cookie can be called in the same way variables are:</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	//Check whether the cookie named yeltuor is set<br />
	if (isset($_COOKIE["yeltuor"]))<br />
	echo &#8220;Welcome back &#8221; . $_COOKIE["yeltuor"] . &#8220;&lt;br&gt;&#8221;;<br />
	else<br />
	echo &#8220;Welcome newbe &lt;br&gt;&#8221;;<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p><a title="Sessions" name="Sessions"></a></p>
<h4>Sessions</h4>
<p>A Sessions is a server side file that is written to, again PHP support the creation and retrieval of session data. When a session is created a unique <em>session ID</em> is used to reference it, this is all taken care within the underlying PHP code.</p>
<p>The advantage of using sessions is that the user can&#8217;t view or edit the data within them; unlike the client side cookie&#8217;s. Before using sessions on a PHP page, the <em>session_start();</em> function must be called, after this a session can be created, however like the cookie functions all session calls must be done before any HTML code on the page:</p>
<p>Below is an example of setting up a server side session:</p>
<blockquote><p>
	 &lt;?PHP<br />
	//start session<br />
	session_start();<br />
	//if session variable total doesn&#8217;t exist create one<br />
	if(!isset($_SESSION['total']))<br />
	$_SESSION['total'] = &#8216;0&#8242;;<br />
	?&gt;<br />
	&lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	The session variable total set to 0, unless it already existed.<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>To reference existing session variables and dispose of variables that are no longer required, the following code can be used:</p>
<blockquote><p>
	 &lt;?PHP<br />
	//start session<br />
	session_start();<br />
	?&gt;<br />
	&lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	echo &#8220;The total is &#8221; . $_SESSION['total'];<br />
	//Finished with session variable, disposing of it<br />
	unset($_SESSION['total']);<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>For the security minded individuals, a list of more advanced session functions that are available can be seen <a class="external text" title="PHP Net" rel="nofollow" href="http://uk.php.net/session" target="_blank">here</a>.</p>
<p><a title="Files" name="Files"></a></p>
<h3>Files</h3>
<p>PHP supports various file related operations, some examples are below:</p>
<p>Opening a file is done with the <strong>fopen( );</strong> function, the first parameter is the filename, the second is the mode to open the file in:</p>
<table style="width: 70%;" border="1">
<tbody>
<tr>
<th>Mode</th>
<th>Description</th>
</tr>
<tr>
<td>r</td>
<td>Read Only, pointer at the beginning of the file</td>
</tr>
<tr>
<td>r+</td>
<td>Read and Write, pointer at the beginning of the file</td>
</tr>
<tr>
<td>w</td>
<td>Write Only, existing file will be truncated or new file will be created.</td>
</tr>
<tr>
<td>w+</td>
<td>Write and Read, existing file will be truncated or new file will be created.</td>
</tr>
<tr>
<td>a</td>
<td>Write Only, Places pointer at end of the file or new file will be created.</td>
</tr>
<tr>
<td>a+</td>
<td>Write and Read, Places pointer at end of the file or new file will be created.</td>
</tr>
<tr>
<td>x</td>
<td>Write Only, If the file already exists the <em>fopen( )</em> function will return <em>FALSE</em>, or a new file will be created.</td>
</tr>
<tr>
<td>x</td>
<td>Write and Read, If the file already exists the <em>fopen( )</em> function will return <em>FALSE</em>, or a new file will be created.</td>
</tr>
</tbody>
</table>
<p>Some examples of using the fopen( ) function are below:</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	//Open file hello.txt, in the case of an error exit<br />
	$f=fopen(&#8220;hello.txt&#8221;,&#8221;r&#8221;) or exit(&#8220;Cant open file&#8221;);<br />
	//if reached the end of the file<br />
	if (feof($f))<br />
	echo &#8220;End of file&#8221;;<br />
	//Loop through file 1 char at a time, echo value<br />
	while (!feof($f))<br />
	{<br />
	$x=fgetc($f);<br />
	echo $x;<br />
	}<br />
	//Close file handle when finished<br />
	fclose($f) ;<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>This function below accepts a parameter of a filename and location and returns the file data as a variable called <strong>contents</strong>:</p>
<blockquote><p>
	 function fileContents($filename){<br />
	// get contents of a file into a string<br />
	$f = fopen($filename, &#8220;r&#8221;);<br />
	$contents = fread($f, filesize($filename)) ;<br />
	fclose ($f);<br />
	return $contents;<br />
	}
</p></blockquote>
<p><a title="Functions" name="Functions"></a></p>
<h3>Functions</h3>
<p>Functions are used in the same way that procedures are used in other programming languages, they are used to aid with the re-use of code. examples of using functions can be seen below:</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	function echoHelloWorld($name)<br />
	{<br />
	echo &#8220;Hello World my name is &#8221; . $name;<br />
	}<br />
	//Using the created function<br />
	echoHelloworld(&#8220;Borris&#8221;)<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>Functions can also return values as an output by the <strong>return</strong> command from within the function.</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	function addnum($num1,$num2)<br />
	{<br />
	$tot = $num1 + $num2;<br />
	return $tot;<br />
	}<br />
	//Using the function addnum<br />
	echo &#8220;1 + 2 = &#8221; . addNum(1,2);<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p><a title="Require" name="Require"></a></p>
<h3>Require</h3>
<p>The require function works in the same way as a function, except that it is used to call another file which is inserted into the current page. This is useful when a <em>header</em> is being used;</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	require(&#8220;header.php&#8221;)<br />
	?&gt;<br />
	&lt;h3&gt;More text here.&lt;/h3&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p><a title="Email" name="Email"></a></p>
<h3>Email</h3>
<p>PHP has built in email support by using the <strong>mail()</strong> function, it requires the usual email parameters;</p>
<table style="width: 70%;" border="1">
<tbody>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
<tr>
<td>to</td>
<td>The recipients address in the form of abc@xyz.com</td>
</tr>
<tr>
<td>subject</td>
<td>Subject of the email</td>
</tr>
<tr>
<td>message</td>
<td>Content of the email; use the <em>\n</em> command for a new line</td>
</tr>
<tr>
<td>headers</td>
<td>An optional parameter which may contain and Cc&#8217;s of Bcc&#8217;s</td>
</tr>
<tr>
<td>parameters</td>
<td>Additional parameters that the sendmail program accepts i.e. From</td>
</tr>
</tbody>
</table>
<p>An example of the code for a mail function with error checking is shown below, to send emails to multiple recipients, a comma: <em>,</em> should be used as a delimiter.</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	$to = &#8220;bob@email.com&#8221;;<br />
	$subject = &#8220;Test email&#8221;;<br />
	$message = &#8220;Hello! World \n this is a test email&#8221;;<br />
	$from = &#8220;tom@email.com&#8221;;<br />
	$headers = &#8220;From: $from&#8221;;<br />
	if (mail($to,$subject,$message,$headers))<br />
	echo &#8220;Mail Sent sucessfully&#8221;;<br />
	else<br />
	echo &#8220;Failed to Send Mail&#8221;;<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>This is very basic example of using emailing, however for a more complete guide to PHP emailing please see PHP Mail article.</p></div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/cookies/" title="Cookies" rel="tag">Cookies</a>, <a href="http://www.yeltuor.com/articles/tag/email/" title="Email" rel="tag">Email</a>, <a href="http://www.yeltuor.com/articles/tag/files/" title="Files" rel="tag">Files</a>, <a href="http://www.yeltuor.com/articles/tag/php/" title="PHP" rel="tag">PHP</a>, <a href="http://www.yeltuor.com/articles/tag/sessions/" title="Sessions" rel="tag">Sessions</a>, <a href="http://www.yeltuor.com/articles/tag/web/" title="Web" rel="tag">Web</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/script/php-advanced/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Basic</title>
		<link>http://www.yeltuor.com/articles/script/php-basic/</link>
		<comments>http://www.yeltuor.com/articles/script/php-basic/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 22:30:37 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[Scripting Snippets]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Conditional]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[Operators]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Variables]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=41</guid>
		<description><![CDATA[PHP is a server-side scripting language which allow for the creation dynamic and interactive webpages, standing for PHP: Hypertext Preprocessor
It is the widely-used, free, and efficient alternative to competitors such as Microsoft&#8217;s ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code of a page.
PHP is often used [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p>PHP is a server-side scripting language which allow for the creation dynamic and interactive webpages, standing for <strong>P</strong>HP: <strong>H</strong>ypertext <strong>P</strong>reprocessor</p>
<p>It is the widely-used, free, and efficient alternative to competitors such as Microsoft&#8217;s ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code of a page.</p>
<p>PHP is often used together with the <a class="external text" title="Apache Web Server" rel="nofollow" href="http://www.apache.org/" target="_blank">Apache</a> web server on various operating systems. It also supports ISAPI and can be used with Microsoft&#8217;s IIS on Windows.</p>
<p>This article will go through some useful PHP syntax and uses.</p>
<p><a title="Basic_Syntax" name="Basic_Syntax"></a></p>
<h3>Basic Syntax</h3>
<p>Basic Syntax included in a PHP page and how in integrates with HTML</p>
<p>A piece of PHP code will always start with the tag <strong>&lt;?php</strong> and end with <strong>?&gt;</strong></p>
<p>Using the <strong>echo &#8221; &#8221; ;</strong> tag displays standard HTML that the browser will display. To echo multiple strings of variables (explained later) use a dot <strong>.</strong> &lt;HTML&gt; &lt;BODY&gt; &lt;?php echo &#8220;&lt;h1&gt; Hello World &lt;/h1&gt;&#8221;; echo &#8220;Hello&#8221; . &#8220;World&#8221;; ?&gt; &lt;/BODY&gt; &lt;/HTML&gt;</p>
<p><a title="Comments" name="Comments"></a></p>
<h4>Comments</h4>
<p>Adding comments in PHP is just as important as any other programming language. within PHP there are two different methods for adding comments, through the <strong>//</strong> syntax for a single line, or to add a block of comment use <strong>/*</strong> to start, and <strong>*/</strong> to finish the block</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
//Single Comment<br />
/*<br />
Start of comment block<br />
Still commenting<br />
*/<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Variables" name="Variables"></a></p>
<h4>Variables</h4>
<p>Variables are not typed in PHP, when referencing variables in PHP they just start with the <strong>$</strong> symbol.</p>
<p>The snippet of code assigns a variable, then uses the <strong>echo</strong> command to output its value</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
$var1=&#8221;Hello World&#8221;;<br />
echo $var1;<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Operators" name="Operators"></a></p>
<h4>Operators</h4>
<p>PHP has the several standard operators which can be used, see table below</p>
<table style="width: 50%;" border="1">
<caption>PHP Operators </caption>
<tbody>
<tr>
<th>Operator</th>
<th>Action</th>
</tr>
<tr>
<td>+</td>
<td>Add</td>
</tr>
<tr>
<td>-</td>
<td>Subtract</td>
</tr>
<tr>
<td>*</td>
<td>Multiply</td>
</tr>
<tr>
<td>/</td>
<td>Divide</td>
</tr>
<tr>
<td>%</td>
<td>Modulus</td>
</tr>
<tr>
<td>++</td>
<td>Increment</td>
</tr>
<tr>
<td>&#8211;</td>
<td>Decrement</td>
</tr>
<tr>
<td>=</td>
<td>Equals</td>
</tr>
<tr>
<td>==</td>
<td>Is equal to</td>
</tr>
<tr>
<td>!=</td>
<td>Is NOT equal to</td>
</tr>
<tr>
<td>&lt;</td>
<td>Less than</td>
</tr>
<tr>
<td>&gt;</td>
<td>Greater than</td>
</tr>
<tr>
<td>&lt;=</td>
<td>Less than or equal</td>
</tr>
<tr>
<td>&gt;=</td>
<td>Greater than or equal</td>
</tr>
<tr>
<td>&amp;&amp;</td>
<td>and</td>
</tr>
<tr>
<td>||</td>
<td>or</td>
</tr>
<tr>
<td>!</td>
<td>not</td>
</tr>
</tbody>
</table>
<h3>Conditional Statements</h3>
<p>Conditional statements help make PHP a dynamic language, being able to put if statements into webpages allows for the generation of dynamic HTML. The common statements are described below:</p>
<p><a title="If_statement" name="If_statement"></a></p>
<h4>If statement</h4>
<p>The most common statement, the syntax for this is shown below</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
if ($x==$y)<br />
echo $x . &#8221; does equal &#8221; . $y;<br />
elseif ($x &lt; $y)<br />
echo $x . &#8221; is less than &#8221; . $y;<br />
else<br />
echo $x . &#8221; does not equal &#8221; . $y;<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Looping_Statements" name="Looping_Statements"></a></p>
<h3>Looping Statements</h3>
<p>Loops are used to save repetition of code, instead a piece of code is used once and told to be repeated a number of times, or until a condition is met.</p>
<p><a title="While_statement" name="While_statement"></a></p>
<h4>While statement</h4>
<p>The While statement should be used when the number of iterations is unknown, the syntax is shown below</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
while ($x != $y)<br />
{<br />
$x++;<br />
echo &#8220;X is &#8221; . $x;<br />
}<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p>The other method of writing a While loop is the <strong>do</strong> &#8230; <strong>while</strong> loop. In this example the code is always run once before the <strong>do</strong> statement is reached.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
do<br />
{<br />
$x&#8211;;<br />
echo &#8220;X is &#8221; . $x;<br />
}<br />
while ($x &gt; 15);<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="For_Loop" name="For_Loop"></a></p>
<h4>For Loop</h4>
<p>This statement is used when the number of repetitions required is known.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
for ($i=1; $i&lt;=5; $i++)<br />
{<br />
echo &#8220;i is &#8221; . $i;<br />
}<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Foreach_Loop" name="Foreach_Loop"></a></p>
<h4>Foreach Loop</h4>
<p>This is very similar to the For Loop above, however in this case the loop is done through an array which is passed into the loop.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
$arr1=array(&#8220;dayone&#8221;, &#8220;daytwo&#8221;, &#8220;daythree&#8221;);<br />
foreach ($arr1 as $value)<br />
{<br />
echo &#8220;Value is &#8221; . $value;<br />
}<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Arrays" name="Arrays"></a></p>
<h3>Arrays</h3>
<p>Arrays are very useful data types which are commonly under used. an array can consist of any type of data that is required, is provides a better way of storing the data and opens up easier ways of using the data when in this format.</p>
<p>There are three different types of array:</p>
<ul>
<li>Numeric Arrays &#8211; array where there is a number that references the position in the array when being accessed</li>
<li>Associative arrays &#8211; array where values are used to reference the location within the array</li>
<li>Multidimensional arrays &#8211; Simply an array with has more than one array within it</li>
</ul>
<p><a title="Numeric_Array" name="Numeric_Array"></a></p>
<h4>Numeric Array</h4>
<p>Two different methods of creating these types of arrays, both yield the same result</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
$arr1=array(&#8220;dayone&#8221;, &#8220;daytwo&#8221;, &#8220;daythree&#8221;);</p>
<p>$arr2[0]=vaule1;<br />
$arr2[1]=vaule2;<br />
$arr2[2]=vaule3;</p>
<p>//Referencing the array is done the same way<br />
echo $arr1[0];<br />
echo $arr2[1];<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Associative_Array" name="Associative_Array"></a></p>
<h4>Associative Array</h4>
<p>This array type does not use standard numbering that numeric array does (0, 1, 2, 3&#8230;), here the array entries are referenced by user defined values. Again there are two different ways of creating this array type.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
$arr1=array(&#8220;dayone&#8221;=&gt;28, &#8220;daytwo&#8221;=&gt;25, &#8220;daythree&#8221;=&gt;20);</p>
<p>$arr2['monday']= &#8220;11&#8243;;<br />
$arr2['tuesday']=&#8221;12&#8243;;<br />
$arr2['wednesday']=&#8221;13&#8243;;</p>
<p>//Referencing the array is done the same way<br />
echo $arr1['dayone'];<br />
echo $arr2['tuesday'];<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Multidimensional_Array" name="Multidimensional_Array"></a></p>
<h4>Multidimensional Array</h4>
<p>The Multidimensional array is the most complicated of them all, when implemented a grid like array is formed. When implementing a multidimensional array the sub array of each array item does not have to be the same size, as shown below.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
$streets = array(<br />
&#8220;Highstreet&#8221;=&gt;array(<br />
&#8220;No1&#8243;,<br />
&#8220;No5&#8243;,<br />
&#8220;No10&#8243;,<br />
),<br />
&#8220;MiddleStreet&#8221;=&gt;array(<br />
&#8220;No100&#8243;<br />
),<br />
&#8220;LowStreet&#8221;=&gt;array(<br />
&#8220;No33&#8243;,<br />
&#8220;No34&#8243;,<br />
&#8220;No35&#8243;<br />
)<br />
);<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Forms" name="Forms"></a></p>
<h3>Forms</h3>
<p>With the use of HTML forms and PHP, information can be passed between pages easily through using the different methods available.</p>
<p>A basic HTML form looks like the following:</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;FORM action=&#8221;page2.php&#8221; method=&#8221;POST&#8221;&gt;<br />
Enter value1: &lt;input type=&#8221;text&#8221; name=&#8221;value1&#8243; /&gt;<br />
Enter value2: &lt;input type=&#8221;text&#8221; name=&#8221;value2&#8243; /&gt;<br />
&lt;input type=&#8221;submit&#8221; /&gt;<br />
&lt;/FORM&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p>Once submitted to <em>page2.php</em> the values can be shown with the following code:</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
Hello World&lt;br&gt;<br />
&lt;?php echo $_POST["value1"]; ?&gt;&lt;br&gt;<br />
&lt;?php echo $_POST["value2"]; ?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p>The syntax <strong>$_POST</strong> changes to <strong>$_GET</strong> depending on the form method. The final piece of syntax is <strong>$_REQUEST</strong>, this contains values for $_GET, $_POST and $_COOKIE (discussed later)</p>
<p>Form <em>Get</em> method displays the data transferred between pages in the address bar, the advantage of this is the new page can be book-marked and each time opened the same data will appear. The disadvantage the limit of 100 characters per variable.</p>
<p>Form <em>Post</em> method does not display the data transferred between pages in the address bar, instead the data is piped directly between pages. in this case the new page can&#8217;t be book-marked successfully, however the variable size limit is not enforced.<br />
Many thanks to the tutorials where this information came from, <a class="external text" title="W3Schools" rel="nofollow" href="http://www.w3schools.com/php/default.asp" target="_blank">W3Schools</a> &amp; <a class="external text" title="PHP .net" rel="nofollow" href="http://uk.php.net/manual/en/introduction.php" target="_blank">PHP</a></div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/array/" title="Array" rel="tag">Array</a>, <a href="http://www.yeltuor.com/articles/tag/conditional/" title="Conditional" rel="tag">Conditional</a>, <a href="http://www.yeltuor.com/articles/tag/loop/" title="Loop" rel="tag">Loop</a>, <a href="http://www.yeltuor.com/articles/tag/operators/" title="Operators" rel="tag">Operators</a>, <a href="http://www.yeltuor.com/articles/tag/php/" title="PHP" rel="tag">PHP</a>, <a href="http://www.yeltuor.com/articles/tag/variables/" title="Variables" rel="tag">Variables</a>, <a href="http://www.yeltuor.com/articles/tag/web/" title="Web" rel="tag">Web</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/script/php-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
