<?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>alecs &#187; php</title>
	<atom:link href="http://www.ylipsis.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ylipsis.com/blog</link>
	<description>web development, SEO, technology</description>
	<lastBuildDate>Sat, 15 May 2010 11:51:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to display the page rank</title>
		<link>http://www.ylipsis.com/blog/2009/03/how-to-display-the-page-rank/</link>
		<comments>http://www.ylipsis.com/blog/2009/03/how-to-display-the-page-rank/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 20:25:37 +0000</pubDate>
		<dc:creator>alecs</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[pagerank]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.ylipsis.com/blog/?p=220</guid>
		<description><![CDATA[If you are looking for a way to determine the Google page rank for a group of websites, here&#8217;s a simple solution: Download the page rank script and test it on your web server. Note that the server is forced to query one of Google&#8217;s servers each time you call the page rank function. Because [...]]]></description>
			<content:encoded><![CDATA[<p>If you are looking for a way to determine the Google page rank for a group of websites, here&#8217;s a simple solution:</p>
<p>Download the <a href="http://www.pagerankcode.com/download-script.html" target="_blank">page rank script</a> and test it on your web server.</p>
<p>Note that the server is forced to query one of Google&#8217;s servers each time you call the page rank function. Because page rank doesn&#8217;t change very often (at least the one displayed by G&#8217;s toolbar) you can greatly improve performance by caching it. For example if you run a directory of websites you can set up a cron job to run and keep the page rank values updated.</p>
<p>Imagine the time it would take for a page with more than 400 URLs to display the page rank in real time (e.g. <a href="http://www.dirpedia.ro/directoare-generale.html" target="_blank">DirPedia</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ylipsis.com/blog/2009/03/how-to-display-the-page-rank/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web development security tips (PHP)</title>
		<link>http://www.ylipsis.com/blog/2009/02/web-development-security-tips-php/</link>
		<comments>http://www.ylipsis.com/blog/2009/02/web-development-security-tips-php/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 17:17:37 +0000</pubDate>
		<dc:creator>alecs</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.ylipsis.com/blog/?p=167</guid>
		<description><![CDATA[I&#8217;ve seen a lot of hacked websites lately (most because of SQL Injection). Almost all the security problems occur because us web developers assume that the users will act naturally and because we&#8217;re lazy to keep testing all the input data or wonder what if?. This is not a how to hack article, but it [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://flickr.com/photos/amagill/235453953/"><img class="alignright" title="lock" src="http://farm1.static.flickr.com/84/235453953_b565f23939_m.jpg" alt="" width="191" height="240" /></a>I&#8217;ve seen a lot of hacked websites lately (most because of SQL Injection). Almost all the security problems occur because us web developers assume that the users will act naturally and because we&#8217;re lazy to keep testing all the input data or wonder <strong>what if?</strong>. This is not a how to hack article, but it should give you, the web developer, some basic ideas to develop a more secure website.</p>
<h2>First line of defense</h2>
<p><strong>Cross-site scripting (XSS)</strong></p>
<p>Do you have an input somewhere on your website? Have you tested what happens when you type in html tags? What about HTML tags with JavaScript actions?</p>
<p>My best solution is a classic <a href="http://www.php.net/manual/en/function.htmlspecialchars.php" target="_blank">htmlspecialchars()</a> on the whole user input when displaying it on a web page. Of course, If you want some extra styles for the text you can always use <a href="http://en.wikipedia.org/wiki/BBCode" target="_blank">BBCode</a>.</p>
<p>Be careful when trying to manually strip HTML tags form strings. You may find out that many browsers tend to correct bad formatted tags and interpret them. Here&#8217;s an example that will be interpreted even though it&#8217;s not XML compliant:</p>
<pre>&lt;script src='http://www.badguy.com/badscript.js' &lt;/script</pre>
<p><strong>SQL Injection</strong></p>
<p>On a database driven website, you will almost certainly save data provided by your users in the database. This means that you will perform a query using unknown strings. Let&#8217;s have a look over a simple example. The user typed in the following string in a comment:</p>
<pre>I don't like this article.</pre>
<p>On the next page, you have the following code:</p>
<pre>mysql_query("INSERT INTO comments(`content`) VALUES('".$_POST['comment']."')");</pre>
<p>The SQL server will be left to execute the next query.</p>
<pre>INSERT INTO comments(`content`) VALUES('I don't like this article.')</pre>
<p>This will obviously result in a SQL error because of the apostrophe from<strong> don&#8217;t</strong>. This isn&#8217;t such a big deal but then again, the user didn&#8217;t mean to do any harm. You may argue that PHP has the <a href="http://www.php.net/manual/en/security.magicquotes.php" target="_blank">magic quote</a> but <a href="http://en.wikipedia.org/wiki/Magic_quotes#Criticism" target="_blank">they&#8217;re not so effective</a>.</p>
<p>A more frequent SQL injection method is through the GET parameters. Yes, you <strong>must make sure</strong> that$_ GET['id'] which will be used in &#8220;SELECT * FROM `articles` WHERE id=&#8217;&#8221;.$_GET['id'].&#8221;&#8216;&#8221; is numeric.</p>
<p><strong>Don&#8217;t assume anything</strong></p>
<p>Just because there is no outside link to some specific administration page doesn&#8217;t mean that you shouldn&#8217;t test the user&#8217;s credentials.</p>
<p>Watch for scripts called with AJAX. Each one is called using a different HTTP request so you need to test the user in each of those.</p>
<h2>Second line of defense</h2>
<p>It seems that some people were stubborn and ended up with a little more access to your website than they should have. What can you do now to minimize their mess?</p>
<p><strong>Encript user passwords</strong></p>
<p>I don&#8217;t know why some  still keep their all the users&#8217; passwords in plain text. This is also a matter of privacy. You wouldn&#8217;t want the webmaster to know everybody&#8217;s password, would you?</p>
<p><strong>Assign read-only privileges to the database user</strong></p>
<p>Most web applications will only use basic <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" target="_blank">CRUD</a> functions. So why give the possibility to ALTER TABLE, DROP DATABASE, etc?</p>
<p><strong>Setup an automated database backup script</strong></p>
<p>Make sure the database is backed up regularly. In most cases it is more important than the script files which normally have an off site backup on your HDD.</p>
<p>Here&#8217;s a shell command which can easily be inserted into a cron job (should be set to run daily):</p>
<pre>date=`date -I` ; mysqldump -h localhost -u db_user -ppassword database_name | gzip &gt; /home/your_user/mysql_backup/backup_$date.gz</pre>
<p>And make sure that the folder where you store your backup<strong> isn&#8217;t public</strong>!</p>
<p>These are the only the first things that came in mind right now. I don&#8217;t want to turn this article into a book so I will end now.</p>
<p>I you have something to add just post a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ylipsis.com/blog/2009/02/web-development-security-tips-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: sending emails whenever an error occurs</title>
		<link>http://www.ylipsis.com/blog/2009/01/php-sending-emails-whenever-an-error-occurs/</link>
		<comments>http://www.ylipsis.com/blog/2009/01/php-sending-emails-whenever-an-error-occurs/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 21:43:00 +0000</pubDate>
		<dc:creator>alecs</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ylipsis.com/blog/?p=126</guid>
		<description><![CDATA[Problem PHP, by default, displays an error directly in the user&#8217;s browser. Something like this is common around the web: Notice: Undefined variable: name in /abc/public_html/index.php on line 29 This is not so useful because if someone else caused this error you have no way of knowing about it and because potentially sensitive information about [...]]]></description>
			<content:encoded><![CDATA[<h3>Problem</h3>
<p>PHP, by default, displays an error directly in the user&#8217;s browser. Something like this is common around the web:</p>
<pre>Notice: Undefined variable: name in /abc/public_html/index.php on line 29</pre>
<p>This is not so useful because if someone else caused this error you have no way of knowing about it and because potentially sensitive information about your PHP code is leaking.</p>
<h3>Solution</h3>
<p><a href="http://us2.php.net/manual/en/function.set-error-handler.php" target="_blank">set_error_handler()</a> can be used to change the way PHP deals with errors by specifying a custom function to be called. Now you can easily output any message you want to the client, keeping the error details to yourself.</p>
<pre>set_error_handler("myErrorHandler",E_ALL);</pre>
<p>I&#8217;ve used E_ALL because I want to know all the error types that occur. Now the only thing left to do is create the myErrorHandler() function in which you print a message to your user and also email the sensitive error details to your own address:</p>
<pre>function myErrorHandler($errno, $errstr, $errfile, $errline){
    $message = '&lt;p&gt;Error number &lt;strong&gt;'.$errno.'&lt;/strong&gt;
                was encountered in &lt;strong&gt;'.$errfile.'&lt;/strong&gt;
                on line &lt;strong&gt;'.$errline.'&lt;/strong&gt;
                &lt;br /&gt;
                '.$errstr.'
                &lt;/p&gt;';
    mail("youremail@example.com","Error at yourWebsite",$message,"Content-type: text/html; charset=utf-8");
    echo "&lt;div class=\"error\"&gt;Script error detected.&lt;/div&gt;";
    return true;
}</pre>
<p>You may also include any debug info in your message, such as the $_SERVER['REQUEST_URI'], time, the user who generated the error (if you have a website with users) etc.</p>
<p>If you want to work with uncaught exceptions, use the <a href="http://us2.php.net/manual/en/function.set-exception-handler.php" target="_blank">set_exception_handler()</a> function instead. It works the same way as <a href="http://us2.php.net/manual/en/function.set-error-handler.php" target="_blank">set_error_handler()</a>.</p>
<p>This is a PHP equivalent to the <a href="http://agilewebdevelopment.com/plugins/exception_notifier" target="_blank">Exception Notifier</a> in Ruby on Rails.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ylipsis.com/blog/2009/01/php-sending-emails-whenever-an-error-occurs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
