<?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... &#187; Sessions</title>
	<atom:link href="http://www.yeltuor.com/articles/tag/sessions/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.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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>
	</channel>
</rss>
