<?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>Albertech.net &#187; PHP</title>
	<atom:link href="http://albertech.net/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://albertech.net</link>
	<description>Tips, Tricks, and Reviews in Linux, Apache, MySQL, PHP</description>
	<lastBuildDate>Wed, 28 Jul 2010 16:09: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: Display page render time</title>
		<link>http://albertech.net/2009/06/php-display-page-render-time/</link>
		<comments>http://albertech.net/2009/06/php-display-page-render-time/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 19:06:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[render time]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://albertech.net/?p=191</guid>
		<description><![CDATA[Here's what I currently use to determine how long it takes to generate a page in PHP. Its only 8 lines of code. ]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Falbertech.net%252F2009%252F06%252Fphp-display-page-render-time%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22PHP%3A%20Display%20page%20render%20time%22%20%7D);"></div>
<p>Here&#8217;s what I currently use to determine how long it takes to generate a page in PHP. Its only 4 lines of code.</p>
<p><strong>PHP 5</strong></p>
<p><code>$time_start = microtime(true);<br />
$time_end = microtime(true);<br />
$time = $time_end - $time_start;<br />
echo "Displaying the render time: $time seconds\n";</code></p>
<p><strong>PHP 4 Version: (8 lines)</strong></p>
<p>Put this at the top of the PHP script:<br />
<code><br />
$render_time_start = microtime_float();<br />
</code></p>
<p>Put this at the end of the script:<br />
<code><br />
$render_time_end = microtime_float();<br />
$render_time = $render_time_end - $render_time_start;<br />
echo $render_time;</code></p>
<p><code>function microtime_float(){<br />
list($usec, $sec) = explode(" ", microtime());<br />
return ((float)$usec + (float)$sec);<br />
}</code></p>

]]></content:encoded>
			<wfw:commentRss>http://albertech.net/2009/06/php-display-page-render-time/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Preventing MySQL injection with PHP</title>
		<link>http://albertech.net/2009/03/preventing-mysql-injection-with-php/</link>
		<comments>http://albertech.net/2009/03/preventing-mysql-injection-with-php/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 22:13:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[prevent injection]]></category>

		<guid isPermaLink="false">http://albertech.net/?p=110</guid>
		<description><![CDATA[<strong>A good practice is to check input strings to make sure users don't put in mySQL commands in your server.</strong> For instance, if a username or password POST variable isn't filtered, there is a potential for an injection like 'OR myusername='. In the past, I've been using my own PHP toolkit to "clean" the input variables. But recently, I began searching to see if there are a built-in solution in PHP for this, especially since I'm converting a script written in Python that had the filter <B>MySQLdb.escape_string</B>. Enter <a href="http://us2.php.net/manual/en/function.mysql-real-escape-string.php">mysql_real_escape_string()</a>]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Falbertech.net%252F2009%252F03%252Fpreventing-mysql-injection-with-php%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Preventing%20MySQL%20injection%20with%20PHP%22%20%7D);"></div>
<p><strong>A good practice is to check input strings to make sure users don&#8217;t put in mySQL commands in your server.</strong> For instance, if a username or password POST variable isn&#8217;t filtered, there is a potential for an injection like &#8216;OR myusername=&#8217;. In the past, I&#8217;ve been using my own PHP toolkit to &#8220;clean&#8221; the input variables. But recently, I began searching to see if there are a built-in solution in PHP for this, especially since I&#8217;m converting a script written in Python that had the filter <B>MySQLdb.escape_string</B>. Enter <a href="http://us2.php.net/manual/en/function.mysql-real-escape-string.php">mysql_real_escape_string()</a></p>
<p>1) Step 1: Filter out the backtick character, ` which is often used in mySQL to denote field names<br />
<code><br />
$variable = str_replace("`","",$variable);<br />
</code></p>
<p>2) Step 2: If its a username or password field which does not have a space, you should use ctype_alnum  This will determine whether there is a character thats not alphanumeric or numeric. So, spaces will not work.<br />
<code><br />
if (ctype_alnum($variable))<br />
{<br />
   //allowed variable<br />
}<br />
</code></p>
<p>3) Step 3: Use the mysql_real_escape_string as long as it exists on your server. If it doesn&#8217;t exist, an error message will display. I suggest upgrading your PHP if possible since this function will take care of different character sets/etc. You can use this function after the mySQL connection has been initialized.<br />
<code><br />
if(function_exists("mysql_real_escape_string"))<br />
{<br />
   $variable = mysql_real_escape_string(htmlspecialchars($variable, ENT_QUOTES));<br />
}<br />
else<br />
{<br />
    echo "mysql_real_escpe_string does not exist on the server. Upgrade PHP to the latest version.";<br />
}<br />
</code></p>

]]></content:encoded>
			<wfw:commentRss>http://albertech.net/2009/03/preventing-mysql-injection-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; MySQL datetime to seconds</title>
		<link>http://albertech.net/2009/01/php-mysql-datetime-to-seconds/</link>
		<comments>http://albertech.net/2009/01/php-mysql-datetime-to-seconds/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 02:53:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[datetime to seconds]]></category>

		<guid isPermaLink="false">http://albertech.net/?p=59</guid>
		<description><![CDATA[<strong>$unixseconds = strtotime($mysqldate);</strong>

For instance, you can use this to write a timeout script for login failures. Usually, a system should lock after 3-5 consecutive failed login attempts. I save the timestamp after the 5th consecutive login failure, then run a check on this timestamp if the current time is within the ~5-10 minute lockout window. 5 minutes is 300 seconds, 10 minutes is 600 seconds.]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Falbertech.net%252F2009%252F01%252Fphp-mysql-datetime-to-seconds%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22PHP%20-%20MySQL%20datetime%20to%20seconds%22%20%7D);"></div>
<p><strong>$unixseconds = strtotime($mysqldate);</strong></p>
<p>For instance, you can use this to write a timeout script for login failures. Usually, a system should lock after 3-5 consecutive failed login attempts. I save the timestamp after the 5th consecutive login failure, then run a check on this timestamp if the current time is within the ~5-10 minute lockout window. 5 minutes is 300 seconds, 10 minutes is 600 seconds.</p>

]]></content:encoded>
			<wfw:commentRss>http://albertech.net/2009/01/php-mysql-datetime-to-seconds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validate MySQL date format in PHP</title>
		<link>http://albertech.net/2009/01/validate-mysql-date-format-in-php/</link>
		<comments>http://albertech.net/2009/01/validate-mysql-date-format-in-php/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 20:32:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ereg]]></category>
		<category><![CDATA[mysql date validation]]></category>
		<category><![CDATA[php 6]]></category>
		<category><![CDATA[preg_match]]></category>

		<guid isPermaLink="false">http://albertech.net/?p=54</guid>
		<description><![CDATA[Validating the date in MySQL should be done using preg_match since ereg_* functions will be removed in PHP 6.

<strong>More info on PHP 6 changes</strong>: <a href="http://wiki.php.net/todo/php60">http://wiki.php.net/todo/php60</a> It appears there will be a module that you can use to utilize existing ereg expressions, so that'll buy some time to port code from ereg* to preg*.
function isValidDate($date){

if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date))
{

return true;
}
else{
return false;
}

}]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Falbertech.net%252F2009%252F01%252Fvalidate-mysql-date-format-in-php%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Validate%20MySQL%20date%20format%20in%20PHP%22%20%7D);"></div>
<p>Validating the date in MySQL should be done using preg_match since ereg_* functions will be removed in PHP 6.</p>
<p><strong>More info on PHP 6 changes</strong>: <a href="http://wiki.php.net/todo/php60">http://wiki.php.net/todo/php60</a> It appears there will be a module that you can use to utilize existing ereg expressions, so that&#8217;ll buy some time to port code from ereg* to preg*.<br />
function isValidDate($date){</p>
<p>if (preg_match (&#8220;/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/&#8221;, $date))<br />
{</p>
<p>return true;<br />
}<br />
else{<br />
return false;<br />
}</p>
<p>}</p>

]]></content:encoded>
			<wfw:commentRss>http://albertech.net/2009/01/validate-mysql-date-format-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Define variables in PHP 5</title>
		<link>http://albertech.net/2008/12/define-variables-in-php-5/</link>
		<comments>http://albertech.net/2008/12/define-variables-in-php-5/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 23:46:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[define variables]]></category>

		<guid isPermaLink="false">http://albertech.net/?p=41</guid>
		<description><![CDATA[If you have older scripts, you may encounter warning messages such as "<strong>Notice:</strong> Undefined variable: "

As a standard practice, you should define variables in PHP by putting in the variable name = FALSE;
<code>$myvar = FALSE;</code>

This is primarily for local variable names that aren't passed in through a $_REQUEST or $_POST, etc.]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Falbertech.net%252F2008%252F12%252Fdefine-variables-in-php-5%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Define%20variables%20in%20PHP%205%22%20%7D);"></div>
<p>If you have older scripts, you may encounter warning messages such as &#8220;<strong>Notice:</strong> Undefined variable: &#8221;</p>
<p>As a standard practice, you should define variables in PHP by putting in the variable name = FALSE;<br />
<code>$myvar = FALSE;</code></p>
<p>This is primarily for local variable names that aren&#8217;t passed in through a $_REQUEST or $_POST, etc.</p>

]]></content:encoded>
			<wfw:commentRss>http://albertech.net/2008/12/define-variables-in-php-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Uptime check</title>
		<link>http://albertech.net/2008/11/php-uptime-check/</link>
		<comments>http://albertech.net/2008/11/php-uptime-check/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 21:49:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[uptime]]></category>

		<guid isPermaLink="false">http://albertech.net/?p=38</guid>
		<description><![CDATA[I found a useful script in PHP that can be used for checking uptime of a server. It can be useful for checking when the servers have a such a significant load that pages can't be displayed. The benefit or running it locally is that I can configure the script to perform failover functionality if necessary. Online uptime services are good too, but most of them aren't free. Maybe I should force the server to show a <a href="http://www.urbandictionary.com/define.php?term=failwhale">failwhale</a> when the site gets too busy...  j/k :D

Here's the link to the script.
<a href="http://www.programmingtalk.com/showthread.php?t=34999">http://www.programmingtalk.com/showthread.php?t=34999</a>

<code>
<?php 

// the URL you want to ... ]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Falbertech.net%252F2008%252F11%252Fphp-uptime-check%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22PHP%20Uptime%20check%22%20%7D);"></div>
<p>I found a useful script in PHP that can be used for checking uptime of a server. It can be useful for checking when the servers have a such a significant load that pages can&#8217;t be displayed. The benefit or running it locally is that I can configure the script to perform failover functionality if necessary. Online uptime services are good too, but most of them aren&#8217;t free. Maybe I should force the server to show a <a href="http://www.urbandictionary.com/define.php?term=failwhale">failwhale</a> when the site gets too busy&#8230;  j/k <img src='http://albertech.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Here&#8217;s the link to the script.<br />
<a href="http://www.programmingtalk.com/showthread.php?t=34999">http://www.programmingtalk.com/showthread.php?t=34999</a></p>
<p><code><br />
<?php </p>
<p>// the URL you want to keep tabs on<br />
$url = 'http://www.somewebsite.com'; </p>
<p>// if not available, send email<br />
if ( url_exists( $url ) === FALSE )<br />
{<br />
    $to   = 'my@email.com';<br />
    $subj = $url . ' is down!';<br />
    $msg  = $url . ' was tested at ' . date( 'Y-m-d H:i:s' ) . ' and was inaccessible.';<br />
    @mail( $to, $subj, $msg );<br />
} </p>
<p>/**<br />
 * url_exists()<br />
 * Checks for the validity of a given URL<br />
 *<br />
 * @param string The http[s] URL you're checking for<br />
 * @return bool TRUE if online; FALSE if not; NULL if not an http(s):// URL<br />
 */<br />
function url_exists( $strURL = NULL )<br />
{<br />
    if ( !preg_match( '/^http(s?):\/\//i', $strURL, $m ) )<br />
        return NULL;<br />
    @$ch = curl_init();<br />
    curl_setopt( $ch, CURLOPT_URL, $strURL );<br />
    curl_setopt( $ch, CURLOPT_BINARYTRANSFER, 1 );<br />
    curl_setopt( $ch, CURLOPT_HEADERFUNCTION, 'curlHeaderCallback' );<br />
    curl_setopt( $ch, CURLOPT_FAILONERROR, 1 );<br />
    curl_setopt( $ch, CURLOPT_TIMEOUT, 5 );<br />
    // https?<br />
    if ( $m[1] == 's' )<br />
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );<br />
    @curl_exec( $ch );<br />
    $intReturnCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );<br />
    curl_close( $ch );<br />
    return ( $intReturnCode == 200 OR $intReturnCode == 302 OR $intReturnCode == 304 ) ? TRUE : FALSE;<br />
} </p>
<p>?><br />
</code></p>

]]></content:encoded>
			<wfw:commentRss>http://albertech.net/2008/11/php-uptime-check/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
