<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.1" -->
<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/"
	>

<channel>
	<title>TechToolBlog &#187; php</title>
	<link>http://www.techtoolblog.com</link>
	<description></description>
	<pubDate>Tue, 22 Jul 2008 14:24:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
	<language>en</language>
			<item>
		<title>xajax - PHP Notices/Warnings</title>
		<link>http://www.techtoolblog.com/archives/xajax-php-noticeswarnings</link>
		<comments>http://www.techtoolblog.com/archives/xajax-php-noticeswarnings#comments</comments>
		<pubDate>Mon, 25 Jun 2007 19:20:38 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
		<category><![CDATA[php]]></category>

		<category><![CDATA[web 2.0 ish]]></category>

		<guid isPermaLink="false">http://www.techtoolblog.com/archives/xajax-php-noticeswarnings</guid>
		<description><![CDATA[Today we were playing with xajax trying to eliminate some redundant page reloads and also keeps some divs hidden based on user input without having to manage their state.&#160; All was working as expected on our Linux box (Fedora) but when we tried to get it running on IIS it was dying.&#160; It looked like [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "xajax - PHP Notices/Warnings", url: "http://www.techtoolblog.com/archives/xajax-php-noticeswarnings" });</script>]]></description>
			<content:encoded><![CDATA[<p>Today we were playing with <a href="http://www.xajaxproject.org/">xajax</a> trying to eliminate some redundant page reloads and also keeps some divs hidden based on user input without having to manage their state.&nbsp; All was working as expected on our Linux box (Fedora) but when we tried to get it running on IIS it was dying.&nbsp; It looked like no response or anything was coming back from some simple AJAX calls.&nbsp; So we did as every good web developer should do - bust out <a href="http://www.getfirebug.com/">FireBug</a>.&nbsp; Looking at the NET results we noticed we were in fact getting a response from our request but with an additional &#8220;PHP Notice undeclared variable message&#8221; (not show in the pic). It didn&#8217;t really dawn on us that this was the culprit until we opened IE 7 and it returned with a dialog box saying it could not parse the XML because of a space, pointing out the &#8220;Notice&#8221; message was returned before the XML response.&nbsp; A closer look at our PHP.ini file resulted in use turning off Notices and turning on Errors&nbsp;only.&nbsp; This was the issue.&nbsp; Goes to show you having two browsers for debugging is a good thing <img src='http://www.techtoolblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.techtoolblog.com//wp-content/uploads/2007/06/image3.png" atomicselection="true"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="115" alt="image" src="http://www.techtoolblog.com//wp-content/uploads/2007/06/image_thumb3.png" width="240" border="0"></a></p>
<p><a href="http://sharethis.com/item?&wp=2.3.1&amp;publisher=24accfc2-4cf2-46ea-abf6-c137f8fa267d&amp;title=xajax+-+PHP+Notices%2FWarnings&amp;url=http%3A%2F%2Fwww.techtoolblog.com%2Farchives%2Fxajax-php-noticeswarnings">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.techtoolblog.com/archives/xajax-php-noticeswarnings/feed</wfw:commentRss>
		</item>
		<item>
		<title>Subdomain Cookies and Localhost</title>
		<link>http://www.techtoolblog.com/archives/subdomain-cookies-and-localhost</link>
		<comments>http://www.techtoolblog.com/archives/subdomain-cookies-and-localhost#comments</comments>
		<pubDate>Tue, 22 May 2007 18:50:24 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
		<category><![CDATA[asp.net]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[web 2.0 ish]]></category>

		<guid isPermaLink="false">http://www.techtoolblog.com/archives/subdomain-cookies-and-localhost</guid>
		<description><![CDATA[So I have a site - we&#8217;ll call www.site1.com that sets a client side cookie named CookieName1 and I need a subdomain test.site1.com to be able to read that value. Using php&#8217;s standard setcookie() method I set the cookie on www by using:
setcookie( &#8220;CookieName1&#8243;, $CookieName1Value, $CookieExpDate, &#8220;/&#8221;, &#8220;.site1.com&#8221; );
This works, no problem.&#160; Now test.site1.com uses [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Subdomain Cookies and Localhost", url: "http://www.techtoolblog.com/archives/subdomain-cookies-and-localhost" });</script>]]></description>
			<content:encoded><![CDATA[<p>So I have a site - we&#8217;ll call <a href="http://www.site1.com">www.site1.com</a> that sets a client side cookie named CookieName1 and I need a subdomain test.site1.com to be able to read that value. Using php&#8217;s standard setcookie() method I set the cookie on www by using:</p>
<p><font face="Courier New" size="1">setcookie( &#8220;CookieName1&#8243;, $CookieName1Value, $CookieExpDate, &#8220;/&#8221;, &#8220;.site1.com&#8221; );</font></p>
<p><font face="v" size="2">This works, no problem.&nbsp; Now test.site1.com uses .NET to read in the value and do something.&nbsp; Pretty standard stuff here:</font></p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red163\green21\blue21;\red0\green128\blue0;}??\fs20  \cf3 If\cf0  Request.Cookies(\cf4 "SourceKey"\cf0 ) \cf3 IsNot\cf0  \cf3 Nothing\cf0  \cf3 Then\par ??\cf0             \cf5 ''DO SOMETHING\par ??\cf0         \cf3 End\cf0  \cf3 If}<br />
-->
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px">&nbsp;&nbsp;<span style="color: blue">If</span> Request.Cookies(<span style="color: #a31515">&#8220;CookieName1&#8243;</span>) <span style="color: blue">IsNot</span> <span style="color: blue">Nothing</span> <span style="color: blue">Then</span></p>
<p style="margin: 0px">&nbsp;&nbsp; <span style="color: green">&#8221;DO SOMETHING</span></p>
<p style="margin: 0px">&nbsp;&nbsp;<span style="color: blue">End</span> <span style="color: blue">If</span></p>
</div>
<p><font size="2"></font>&nbsp;</p>
<p>Build &gt; Debug, set a breakpoint on the if block, skips right over it.&nbsp; Huh?&nbsp; I make sure IE has the cookie, try again.&nbsp; Nope, Request.Cookies(&#8221;CookieName1&#8243;) is Nothing.&nbsp;&nbsp;Can&#8217;t be an IE thing can it?&nbsp; Run same scenerio in FireFox - same result.&nbsp; Then it dawns on me, I&#8217;m not running this under test.site1.com, I&#8217;m running this under localhost.&nbsp; Everything was working as expected, cookies can only be read by *.site1.com.&nbsp; So how am I going to test this thing on my local machine? I do have a sandbox, test1sandbox.site1.com, but what if I didn&#8217;t?&nbsp; A simple solution is to change the host file found at <tt>%SystemRoot%\system32\drivers\etc\</tt> with:</p>
<p>127.0.0.1 localhost<br />127.0.0.1 test1.site1.com</p>
<p>Run another Debug, change the <a href="http://localhost">http://localhost</a> to <a href="http://test1.site1.com">http://test1.site1.com</a> and now I&#8217;m able to read cookies from *.site1.com. Nifty.</p>
<p><a href="http://sharethis.com/item?&wp=2.3.1&amp;publisher=24accfc2-4cf2-46ea-abf6-c137f8fa267d&amp;title=Subdomain+Cookies+and+Localhost&amp;url=http%3A%2F%2Fwww.techtoolblog.com%2Farchives%2Fsubdomain-cookies-and-localhost">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.techtoolblog.com/archives/subdomain-cookies-and-localhost/feed</wfw:commentRss>
		</item>
		<item>
		<title>How to Expire a Cookie at Midnight using PHP</title>
		<link>http://www.techtoolblog.com/archives/how-to-expire-a-cookie-at-midnight-using-php</link>
		<comments>http://www.techtoolblog.com/archives/how-to-expire-a-cookie-at-midnight-using-php#comments</comments>
		<pubDate>Wed, 25 Oct 2006 01:30:15 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.techtoolblog.com/archives/how-to-expire-a-cookie-at-midnight-using-php</guid>
		<description><![CDATA[
One line of code:
setcookie("CookieName", "CookieValue", mktime(23, 59, 59, date("m"), date("d"), date("y"))); 
<script type="text/javascript">SHARETHIS.addEntry({ title: "How to Expire a Cookie at Midnight using PHP", url: "http://www.techtoolblog.com/archives/how-to-expire-a-cookie-at-midnight-using-php" });</script>]]></description>
			<content:encoded><![CDATA[<p>
One line of code:</p>
<p><code>setcookie("CookieName", "CookieValue", mktime(23, 59, 59, date("m"), date("d"), date("y"))); </code></p>
<p><a href="http://sharethis.com/item?&wp=2.3.1&amp;publisher=24accfc2-4cf2-46ea-abf6-c137f8fa267d&amp;title=How+to+Expire+a+Cookie+at+Midnight+using+PHP&amp;url=http%3A%2F%2Fwww.techtoolblog.com%2Farchives%2Fhow-to-expire-a-cookie-at-midnight-using-php">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.techtoolblog.com/archives/how-to-expire-a-cookie-at-midnight-using-php/feed</wfw:commentRss>
		</item>
		<item>
		<title>Upload Files to MySQL using PHP Tutorial</title>
		<link>http://www.techtoolblog.com/archives/upload-files-to-mysql-using-php-tutorial</link>
		<comments>http://www.techtoolblog.com/archives/upload-files-to-mysql-using-php-tutorial#comments</comments>
		<pubDate>Thu, 20 Jul 2006 14:58:56 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.techtoolblog.com/archives/upload-files-to-mysql-using-php-tutorial</guid>
		<description><![CDATA[I much prefer to upload files to mysql instead of saving them directly to the file system.  I can run database backups/mirrors that are much easier to manage then if files were placed on the file system.
Here are the simple scripts I use to upload files and a script to stream the file back [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Upload Files to MySQL using PHP Tutorial", url: "http://www.techtoolblog.com/archives/upload-files-to-mysql-using-php-tutorial" });</script>]]></description>
			<content:encoded><![CDATA[<p>I much prefer to upload files to mysql instead of saving them directly to the file system.  I can run database backups/mirrors that are much easier to manage then if files were placed on the file system.</p>
<p>Here are the simple scripts I use to upload files and a script to stream the file back to the browser.</p>
<p><strong>MySQL Database Script:</strong><br />
<code><br />
CREATE TABLE `UploadedFiles` (<br />
  `UploadedFileID` int(11) NOT NULL auto_increment,<br />
  `name` varchar(30) default NULL,<br />
  `type` varchar(30) default NULL,<br />
  `size` int(11) default NULL,<br />
  `content` longblob,<br />
  PRIMARY KEY  (`UploadedFileID`)<br />
) TYPE=MyISAM;<br />
</code></p>
<p><strong><br />
Upload Script:</strong><br />
<code><br />
<?php<br />
'holds db constructor<br />
include("{$_SERVER['DOCUMENT_ROOT']}/phplibrary/dbconnect.php");<br />
###################<br />
//Consume Post Vars<br />
###################<br />
if (!empty($_POST['upload']))<br />
{<br />
	//Loop thru Post Array<br />
    foreach($_POST as $key => $value) {<br />
    $$key = $value;<br />
    }</p>
<p>    $connect->connect_db(mydatabase)</p>
<p>    //Insert File<br />
    if(isset($_POST[&#8217;upload&#8217;]) &#038;&#038; $_FILES[&#8217;SpecialFile&#8217;][&#8217;size&#8217;] > 0) {<br />
    $fileName = $_FILES[&#8217;SpecialFile&#8217;][&#8217;name&#8217;];<br />
    $tmpName  = $_FILES[&#8217;SpecialFile&#8217;][&#8217;tmp_name&#8217;];<br />
    $fileSize = $_FILES[&#8217;SpecialFile&#8217;][&#8217;size&#8217;];<br />
    $fileType = $_FILES[&#8217;SpecialFile&#8217;][&#8217;type&#8217;];<br />
    $fp= fopen($tmpName, &#8216;r&#8217;);<br />
    $content  = addslashes($content);<br />
    fclose($fp);<br />
    }<br />
    if(!get_magic_quotes_gpc()) {<br />
    $fileName = addslashes($fileName);<br />
    }</p>
<p>    $query = &#8220;INSERT INTO UploadedFiles (name, size, type, content) &#8220;.<br />
    &#8220;VALUES (&#8217;$fileName&#8217;, &#8216;$fileSize&#8217;, &#8216;$fileType&#8217;, &#8216;$content&#8217;)&#8221;;</p>
<p>   $result = mysql_query($query);<br />
   if (!$result) {<br />
    dberror (mysql_error(), $_SERVER[&#8217;PHP_SELF&#8217;] );<br />
    echo mysql_error();<br />
  }</p>
<p>  //Display Confirmation<br />
  $message = urlencode(&#8221;Your file has been uploaded.&#8221;);<br />
  header( &#8220;Location: confirmed.php?m=$message&#8221; );<br />
  exit;<br />
}<br />
?></p>
<p><html><head></head><br />
<body></p>
<form method="post" enctype="multipart/form-data" action="<?php echo $PHP_SELF;?>&#8220;></p>
<input type="hidden" name="MAX_FILE_SIZE" value="5000000"> <!--5mb file limit--></p>
<p>Upload File:<br />
<input name="SpecialFile" type="file"></p>
<input name="upload" type="submit" class="box" id="upload" value="Submit"></form>
<p></body></html></p>
<p></code></p>
<p>To download the file I prefer to push the file down as octet stream/binary data (NOTE: To display images you would need to push out the particular mime type).  That way users get the &#8220;Save&#8221; dialoge instead of the browser (especially IE) automatically trying to open with what it *thinks* should.  The following codes displays the file in a drop down list for users to select and have the file streamed to their web browser.</p>
<p><b>Download File Script</b></p>
<p><code><br />
<?php<br />
//holds db constructor<br />
include("{$_SERVER['DOCUMENT_ROOT']}/phplibrary/dbconnect.php"); </p>
<p>if (!empty($_POST['GetFile']))<br />
{<br />
    $UploadedFileID = $_POST["UploadedFileID"];<br />
    $connect->connect_db(mydatabase)<br />
    $query = &#8220;SELECT * FROM UploadedFile WHERE UploadedFileID = $UploadedFileID&#8221;;<br />
    $result = mysql_query( $query );<br />
	if( !$result ) {<br />
	echo mysql_error();<br />
	exit;<br />
	}</p>
<p>    $row = mysql_fetch_array( $result );<br />
    if (!empty($row[&#8221;content&#8221;]))<br />
  {<br />
    // Output the MIME header - Force as Octet Stream<br />
   // You could get this from the FileType Column<br />
    header(&#8221;Content-type: application/octet-stream&#8221;);<br />
    header(&#8221;Content-Length: &#8221; . strlen($row[&#8217;content&#8217;])  );<br />
    header(&#8221;Content-Type: application/octet-stream&#8221;);<br />
    header(&#8217;Content-Disposition: attachment; filename=&#8221;&#8216;.$row[&#8217;name&#8217;].&#8217;&#8221;&#8216;);<br />
    header(&#8221;Content-Transfer-Encoding: binary\n&#8221;);<br />
    echo $row[&#8217;content&#8217;];<br />
   }</p>
<p>}<br />
?></p>
<p><html><head></head><br />
<body></p>
<form action="<?php echo $PHP_SELF;?>&#8221; method=&#8221;post&#8221; ><br />
View Files:<br />
<select name="UploadedFileID" >
<option value="">- Select File -</option><br />
<?php<br />
  $connect->connect_db(mydatabase)<br />
  $result = mysql_query(&#8221;SELECT UploadedFileID, name FROM UploadedFiles&#8221;);<br />
  if ($myrow = mysql_fetch_array($result)) {<br />
  do { ?><br />
    <option value="<? echo $myrow['UploadedFileID']; ?>&#8220;><? echo $myrow['name']; ?></option><br />
    <?<br />
  } while ($myrow = mysql_fetch_array($result));<br />
  }<br />
?><br />
  </select>
<input type="Submit" value="Get File" name="GetFile">
</form>
<p></code></p>
<p><a href="http://sharethis.com/item?&wp=2.3.1&amp;publisher=24accfc2-4cf2-46ea-abf6-c137f8fa267d&amp;title=Upload+Files+to+MySQL+using+PHP+Tutorial&amp;url=http%3A%2F%2Fwww.techtoolblog.com%2Farchives%2Fupload-files-to-mysql-using-php-tutorial">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.techtoolblog.com/archives/upload-files-to-mysql-using-php-tutorial/feed</wfw:commentRss>
		</item>
		<item>
		<title>RSS Feed any MySQL Database using PHP</title>
		<link>http://www.techtoolblog.com/archives/rss-feed-any-mysql-database-using-php</link>
		<comments>http://www.techtoolblog.com/archives/rss-feed-any-mysql-database-using-php#comments</comments>
		<pubDate>Thu, 16 Feb 2006 22:30:40 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://techtoolblog.com/archives/rss-feed-any-mysql-database-using-php</guid>
		<description><![CDATA[To some folks rss is this hot new technology that is changing our lives in technology.  The truth is rss is a speciality formated text file that people finally have standarized on.  It's the standarization that changes people lives, not neccessary the technology.  Anyway, a while back I wanted to rss feed our knowledgebase.  The knowledgebase articles are stored in a mysql database.  I played around with using <a href="http://pear.php.net/package/XML_RPC/">PEAR::PACKAGE::XML </a> to do the job but found it to be a bit overkill.  So I wrote my own xml builder - source code is below or  you can download the <a href="http://techtoolblog.com/wp-content/uploads/2006/02/rssfeed.txt" title="">php rss feed mysql database code</a>.  What is nice, is that you can RSS feed any database.<script type="text/javascript">SHARETHIS.addEntry({ title: "RSS Feed any MySQL Database using PHP", url: "http://www.techtoolblog.com/archives/rss-feed-any-mysql-database-using-php" });</script>]]></description>
			<content:encoded><![CDATA[<p>To some folks rss is this hot new technology that is changing our lives in technology.  The truth is rss is a specific formated text file that people finally have standarized on.  It&#8217;s the standarization that changes people lives, not neccessary the technology.  Anyway, a while back I wanted to rss feed our knowledgebase.  The knowledgebase articles are stored in a mysql database.  I played around with using <a href="http://pear.php.net/package/XML_RPC/">PEAR::PACKAGE::XML </a> to do the job but found it to be a bit overkill.  So I wrote my own xml builder - source code is below or  you can download the <a href="http://techtoolblog.com/wp-content/uploads/2006/02/rssfeed.txt" title="">php rss feed mysql database code</a>.  What is nice, is that you can RSS feed any database.</p>
<p><code><?</p>
<p>// prepare HTML text for use as UTF-8 character data in XML<br />
function cleanText($intext) {<br />
    return utf8_encode(<br />
        htmlspecialchars(<br />
            stripslashes($intext)));<br />
}</p>
<p>// set the file's content type and character set<br />
// this must be called before any output<br />
header("Content-Type: text/xml;charset=utf-8");</p>
<p>// retrieve database records</p>
<p>$db = mysql_connect("YourMySQLSERVER", "YOURUSERNAME", "YOURPASSWORD");<br />
if (!$db)<br />
{<br />
   error_log("Error: Could not connect to database in rss.php.");<br />
   exit;<br />
}</p>
<p>// store items from the database in the $result1 array<br />
mysql_select_db("knowledgebase");</p>
<p>$query1 = "SELECT faqarticles.question, faqarticles.content, SUBSTRING(content,1,600) as mycontent,<br />
          faqarticles.cat, faqarticles.id, faqarticles.date FROM faqarticles WHERE<br />
          faqarticles.approved != '1'<br />
          ORDER BY faqarticles.date DESC";<br />
$result1 = mysql_query($query1);<br />
$phpversion = phpversion();</p>
<p>// display RSS 2.0 channel information</p>
<p>ECHO <<<END<br />
<?xml version="1.0" encoding="utf-8"?><br />
<rss version="2.0"><br />
   <channel></p>
<link>http://www.yourlinkgoeshere.com</link>
      <description>Describe your RSS Feed Heredescription><br />
      <language>en-us</language><br />
      <docs>http://backend.userland.com/rss</docs><br />
      <generator>PHP/$phpversion</generator></p>
<p>    <image><br />
      <url>http://www.yoursite.com/images/YourLogo.gif</url></p>
<link>http://www.yoursite.com/index.php</link>
      <width>140</width><br />
      <height>60</height><br />
      <description>Describe Your Image</description><br />
    </image><br />
END;</p>
<p>// loop through the array pulling database fields for each item<br />
for ($i = 0; $i < mysql_num_rows($result1); $i++) {<br />
   @$row = mysql_fetch_array($result1);<br />
   $title = cleanText($row["question"]);<br />
   $link = "http://www.YourSite.com/kb/index.php?article=".$row["id"];<br />
   $description =  $row["mycontent"];</p>
<p>   //Replace Ugly HTML that got into the Knowledgebase with nothing<br />
   $desc_replace = array("<H3>&nbsp;</H3>&#8220;, &#8220;<P>&nbsp;</P>&#8220;, );<br />
   $desc_replace_with  = array(&#8221;", &#8220;&#8221;, &#8220;&#8221;);<br />
   $desc_temp = str_replace($desc_replace, $desc_replace_with, $description);</p>
<p>   //Now clean the HTML<br />
   $mydescription = cleanText($desc_temp);<br />
   $pubDate = $row[&#8221;date&#8221;];</p>
<p>// display an item<br />
ECHO <<<END</p>
<p>	<item></p>
<link>$link</link>
		<description>$mydescription&#8230; <b>For the entire article, please visit our site.</b></description></p>
<pubDate>$pubDate</pubDate>
<p>	</item><br />
END;</p>
<p>}</p>
<p>ECHO <<<END</p>
<p>   </channel><br />
</rss><br />
END;</p>
<p>?></code></p>
<p><a href="http://sharethis.com/item?&wp=2.3.1&amp;publisher=24accfc2-4cf2-46ea-abf6-c137f8fa267d&amp;title=RSS+Feed+any+MySQL+Database+using+PHP&amp;url=http%3A%2F%2Fwww.techtoolblog.com%2Farchives%2Frss-feed-any-mysql-database-using-php">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.techtoolblog.com/archives/rss-feed-any-mysql-database-using-php/feed</wfw:commentRss>
		</item>
		<item>
		<title>Performance Testing with PHP</title>
		<link>http://www.techtoolblog.com/archives/performance-testing-with-php</link>
		<comments>http://www.techtoolblog.com/archives/performance-testing-with-php#comments</comments>
		<pubDate>Fri, 10 Feb 2006 17:02:41 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://techtoolblog.com/2006/02/10/performance-testing-with-php/</guid>
		<description><![CDATA[After writing some functions or classes its good idea to make sure your code is executing in a timely fashion, especially for web applications. For PHP I use a function derived from the chronmeter class to look for bottle necks (the function is below or <a href="http://techtoolblog.com/wp-content/uploads/2006/02/php_chronometer.txt" title="">download it here</a>).<script type="text/javascript">SHARETHIS.addEntry({ title: "Performance Testing with PHP", url: "http://www.techtoolblog.com/archives/performance-testing-with-php" });</script>]]></description>
			<content:encoded><![CDATA[<p>After writing some functions or classes its good idea to make sure your code is executing in a timely fashion, especially for web applications. For PHP I use a function derived from the chronmeter class to look for bottle necks (the function is below or <a href="http://techtoolblog.com/wp-content/uploads/2006/02/php_chronometer.txt" title="">download it here</a>).</p>
<p>We recently ran into a problem of a web form running particular slow. It had the following functionality.</p>
<li>Insert some data into 2 mysql tables</li>
<li> Grab some data from a Microsoft SQL Database</li>
<li>Read in a text file, replace some verbiage, then send out a couple of emails.</li>
<p>I architected the form and was letting one of our interns implement it. Everything seemed pretty straight forward - this code should fly. After the site went live, I checked out the form and found it running slow. Using the chronometer function I paired it down to our phpmailer. Turns out we were using phpâ€™s built in mail function and not phpmailer.</p>
<p>After switching to phpmailer and setting the option to smtp, I cut the runtime in half. I then realized we were sending out 2 emails and each time opening/closing an smtp connection. I refactored so only 1 connection was opened. Here are the results of the average form runtime:</p>
<li>PHPâ€™s built in mail function: Avg: 3 seconds.</li>
<li>PHPMAILER function with 2 connections: 1.5 seconds.</li>
<li>PHPMAILER function with 1 connection: .105 seconds</li>
<p>To use the chronometer function, simply call it before a class or function call and then call it right after it .</p>
<p><code><br />
$chronometer(); //start<br />
$this->SendSalesEmail($_POST);<br />
$runtime = chronometer();<br />
print â€œSendSalesEmail took: $runtimeâ€;</p>
<p><code>$CHRONO_STARTTIME = 0;</code></p>
<p><code>define(â€RET_TIMEâ€, â€œsâ€); //Can be set to â€œmsâ€ for milliseconds or â€œsâ€ for seconds<br />
function chronometer()<br />
{<br />
global $CHRONO_STARTTIME;</p>
<p>$now = microtime(TRUE); // float, in _seconds_</p>
<p>if (RET_TIME === â€™sâ€™) {<br />
$now = $now + time();<br />
$malt = 1;<br />
$round = 7;<br />
} elseif (RET_TIME === â€˜msâ€™) {<br />
$malt = 1000;<br />
$round = 3;<br />
} else {<br />
die(â€Unsupported RET_TIME valueâ€);<br />
}</p>
<p>if ($CHRONO_STARTTIME > 0) {<br />
/* Stop the chronometer : return the amount of time since it was started,<br />
in ms with a precision of 3 decimal places, and reset the start time.<br />
We could factor the multiplication by 1000 (which converts seconds<br />
into milliseconds) to save memory, but considering that floats can<br />
reach e+308 but only carry 14 decimals, this is certainly more precise */</p>
<p>$retElapsed = round($now * $malt - $CHRONO_STARTTIME * $malt, $round);</p>
<p>$CHRONO_STARTTIME = $now;</p>
<p>return $retElapsed;<br />
} else {<br />
// Start the chronometer : save the starting time</p>
<p>$CHRONO_STARTTIME = $now;</p>
<p>return 0;<br />
}<br />
}</code><br />
?><a href="http://techtoolblog.com/wp-content/uploads/2006/02/php_chronometer.txt" title="">php_chronometer.txt</a></p>
<p><a href="http://sharethis.com/item?&wp=2.3.1&amp;publisher=24accfc2-4cf2-46ea-abf6-c137f8fa267d&amp;title=Performance+Testing+with+PHP&amp;url=http%3A%2F%2Fwww.techtoolblog.com%2Farchives%2Fperformance-testing-with-php">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.techtoolblog.com/archives/performance-testing-with-php/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
