<?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>PHP Starter &#187; CodeIgniter</title>
	<atom:link href="http://phpstarter.net/tag/codeigniter/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpstarter.net</link>
	<description>PHP Tips &#38; Tools From Starters to Experts</description>
	<lastBuildDate>Fri, 25 Jun 2010 14:14:24 +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>Akismet Class/Library for Codeigniter</title>
		<link>http://phpstarter.net/2009/02/akismet-classlibrary-for-codeigniter/</link>
		<comments>http://phpstarter.net/2009/02/akismet-classlibrary-for-codeigniter/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 12:00:15 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=301</guid>
		<description><![CDATA[Using Akismet to block spam on your WordPress blog is a no-brainer, but what about your custom blog or article site? It is possible to extend the Akismet anti-spam service to your own custom website. In this article, you will find a fully functional Akismet library for CodeIgniter as well as instructions and usage examples. [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Using Akismet to block spam on your WordPress blog is a no-brainer, but what about your custom blog or article site?  It is possible to extend the Akismet anti-spam service to your own custom website.  In this article, you will find a fully functional Akismet library for CodeIgniter as well as instructions and usage examples.</p>
<p><span id="more-301"></span></p>
<p><strong>Download Script:</strong> <a href="http://phpstarter.net/wp-content/uploads/2009/01/akismet_ci.tgz">tar</a> | <a href="http://phpstarter.net/wp-content/uploads/2009/01/akismet_ci.zip">zip</a></p>
<h3>Setup &#038; Configuration</h3>
<p>Extract the archive into your CI application.  There are two files &#8211; the library (class file) goes into the system/application/libraries directory, and the config file goes into the system/application/config directory.  Then, be sure to set the values in the config file.</p>
<h3>Usage Example</h3>
<pre class="brush: php">
$this-&gt;akismet-&gt;user_ip = $_SERVER[&#039;REMOTE_ADDR&#039;];
$this-&gt;akismet-&gt;user_agent = $_SERVER[&#039;HTTP_USER_AGENT&#039;];
$this-&gt;akismet-&gt;referrer = $_SERVER[&#039;HTTP_REFERRER&#039;];
$this-&gt;akismet-&gt;permalink = &#039;http://example.com/articles/some-article&#039;;
$this-&gt;akismet-&gt;comment_type = &#039;comment&#039;;
$this-&gt;akismet-&gt;comment_author = &#039;viagra-test-123&#039;;
$this-&gt;akismet-&gt;comment_author_url = &#039;&#039;;
$this-&gt;akismet-&gt;comment_author_email = &#039;user@example.com&#039;;
$this-&gt;akismet-&gt;comment_content = &#039;This is a test comment.&#039;;

$result = $this-&gt;akismet-&gt;is_spam($debug);
</pre>
<p>The example is pretty self-explanatory.  Just fill in those necessary values and get a result from the is_spam() function.  This function will return &#8216;true&#8217; if the comment is found to be spam, or &#8216;false&#8217; if it is ham.  Be advised, that it may also return error information if the request is not valid, so in a production environment, test for spam with something like this:</p>
<pre class="brush: php">
$spam = ($this-&gt;akismet-&gt;is_spam($debug) == &#039;true&#039;) ? 1 : 0;
</pre>
<p>The above statement forces the $spam variable to be &#8217;1&#8242; if spam, or &#8217;0&#8242; is ham, <strong>or if there was an error making the call to Akismet</strong>.  This sets it up to inserting into a MySQL boolean field.</p>
<p>That $debug variable is being passed by reference and is optional.  It will be set to the response headers received by the Akismet servers, and you can see the contents in case something goes wrong.</p>
<h3>Help Correct Akismet if they Mess Up</h3>
<p>Their spam detection isn&#8217;t always correct, but the cool thing about this service is that you can help them correct their mistakes.  (How cool is that?)  If they made a mistake, use the following methods to submit the correct information:</p>
<pre class="brush: php">
/* set the class variables - same as the usage example above */

/* submit spam to Akismet that was flagged as ham */
$result = $this-&gt;akismet-&gt;is_spam($debug);

/* submit ham to Akismet that was flagged as spam */
$result = $this-&gt;akismet-&gt;is_ham($debug);
</pre>
<h3>Getting an API Key</h3>
<p>Akismet only gives free API keys to WordPress blogs, so if you have one, just use that API key for your CI application.  See <a href="http://akismet.com/commercial/">this page</a> for more information on API keys and Akismet for commercial use.</p>
<p>So there you have it &#8211; easy spam protection on any CodeIgniter website.  This library can easily be extended to a non-CodeIgniter website.  The only CI resource this library uses is the config feature.</p>
<p>If you find a bug or have another feature idea to add, please post a comment. <img src='http://phpstarter.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Download Script:</strong> <a href="http://phpstarter.net/wp-content/uploads/2009/01/akismet_ci.tgz">tar</a> | <a href="http://phpstarter.net/wp-content/uploads/2009/01/akismet_ci.zip">zip</a></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/02/akismet-classlibrary-for-codeigniter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Run CodeIgniter from the Command Line / SSH</title>
		<link>http://phpstarter.net/2008/12/run-codeigniter-from-the-command-line-ssh/</link>
		<comments>http://phpstarter.net/2008/12/run-codeigniter-from-the-command-line-ssh/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 21:18:47 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=43</guid>
		<description><![CDATA[CodeIgniter has quickly become my favorite framework to use when coding applications in PHP. CodeIgniter makes it way too easy to follow the MVC approach, maintain modulated code, and to also have access to several additional helpers and libraries. But, there was one major flaw: The inability to easily access via the command line. Why? [...]


Related posts:<ul><li><a href='http://phpstarter.net/2010/05/send-print-jobs-directly-from-php/' rel='bookmark' title='Permanent Link: Send Print Jobs Directly from PHP'>Send Print Jobs Directly from PHP</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p>CodeIgniter has quickly become my favorite framework to use when coding applications in PHP.  CodeIgniter makes it way too easy to follow the MVC approach, maintain modulated code, and to also have access to several additional helpers and libraries.  <strong>But</strong>, there was one major flaw:  The inability to easily access via the command line.  Why?  Because CI uses the Request URI to route to the controllers.  That is a webserver-specific function, and is not available from the command line.  There are other solutions to this problem out there, but I think mine is better.</p>
<p><span id="more-43"></span></p>
<h3>Why?</h3>
<p>A lot of my web projects require import scripts and other actions that maintain the website outside the typical http request.  Most of the time, I use the CLI requests in &#8220;cron jobs&#8221;, or scripts that are automatically called at certain times by the server operating system.  CodeIgniter doesn&#8217;t have this CLI (command line) functionality, so we have to add it in ourselves.</p>
<h3>Existing/Old Method</h3>
<p>In order to call a controller/method from the command line, a separate file needs to be created that manually sets the REQUEST_URI / PATH_INFO, which are both Apache environment variables and are used by CI to determine which controller &#038; method to call up.  We need to set those items as well as a couple others to make this possible.  Time for an example:</p>
<pre class="brush: php">
#!/usr/bin/php
&lt;?php
/* make sure this isn&#039;t called from a web browser */
if (isset($_SERVER[&#039;REMOTE_ADDR&#039;])) die(&#039;Permission denied.&#039;);

/* set the controller/method path */
$_SERVER[&#039;PATH_INFO&#039;] = $_SERVER[&#039;REQUEST_URI&#039;] = &#039;/cron/purge_cache&#039;;

/* raise or eliminate limits we would otherwise put on http requests */
set_time_limit(0);
ini_set(&#039;memory_limit&#039;, &#039;256M&#039;);

/* call up the framework */
include(dirname(__FILE__).&#039;/index.php&#039;);
?&gt;
</pre>
<p>Since this is a shell script, we need the hashbang (line 1 of the example).  This tells Bash what binary to use to run the script.  If /usr/bin/php is not the path to PHP in your environment, you will need to change that accordingly.</p>
<p>Line 4 simply detects whether it is being ran through a shell session or browser.  If we have a remote IP address, then it was accessed by a web browser.</p>
<p>Line 7 is the import one.  This sets the controller/method to call.  In my example, we are calling the &#8216;cron&#8217; controller and the &#8216;purge_cache&#8217; method.</p>
<p><strong>There is one big problem with this method.</strong>  We have to create a file for every possible command line request.  As you can see, the requested controller/method is hard-coded in the file.  This method works ok when we only have one or two things callable by the command line, but usually, I have more than that.</p>
<h3>My New Method</h3>
<p>PHP has the option to accept command-line arguments, and we are going to use that to our advantage.  For my solution, I created a copy of index.php, and saved it as cli.php.  I then added the following code to the top of the file:</p>
<pre class="brush: php">
#!/usr/local/bin/php
&lt;?php

/**
 * only a few lines of code will make the best web framework
 * function on the command line
 */

	/* we don&#039;t need to be limited by...normal limitations */
	set_time_limit(0);
	ini_set(&#039;memory_limit&#039;, &#039;256M&#039;);

	/* make sure this isn&#039;t being called by a web browser */
	if (isset($_SERVER[&#039;REMOTE_ADDR&#039;])) die(&#039;Permission denied.&#039;);

	/* set some constants */
	define(&#039;CMD&#039;, 1);

	/* manually set the URI path based on command line arguments... */
	unset($argv[0]); /* ...but not the first one */
	$_SERVER[&#039;QUERY_STRING&#039;] =  $_SERVER[&#039;PATH_INFO&#039;] = $_SERVER[&#039;REQUEST_URI&#039;] = &#039;/&#039; . implode(&#039;/&#039;, $argv) . &#039;/&#039;;
</pre>
<p>Lines 10-14 should look familiar, as I took that form the previous example.  Line 17 is just a constant that we will use later.</p>
<p>I am using PHP&#8217;s $argv variable to get a list of the arguments, so if I called the script by saying:</p>
<pre class="brush: php">
./cli.php cron purge_cache
</pre>
<p>&#8230;then $argv would be an array with the elements of &#8216;cli.php&#8217;, &#8216;cron&#8217;, &#8216;purge_cache&#8217;.  I don&#8217;t need the first element, so I unset that in line 20.  Then I implode the rest of the elements to form the request path, and the end result would be /cron/purge_cache/, just like our hard-coded example at the beginning.</p>
<p>By doing it this way, we created another handler that can run any controller on the command line w/o modifications.  The only thing we needed was a handler for CLI requests.</p>
<h3>A Step Further &#8211; Custom Error Pages</h3>
<p>CodeIgniter&#8217;s error pages have HTML that we don&#8217;t need to see in our command line.  I defined that CMD constant so I can tell if we are accessing via the CLI or web browser.  The following bit of code is my 404 error page found at ./system/application/errors/error_404.php.  I use some conditionals and show a plain-text form of the error if viewed in the CLI.</p>
<pre class="brush: php">
&lt;?php header(&quot;HTTP/1.1 404 Not Found&quot;); ?&gt;
&lt;?php if(defined(&#039;CMD&#039;)) : ?&gt;

***&lt;?php echo $heading; ?&gt;***

&lt;?php echo $message; ?&gt;

&lt;?php else : ?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;404 Page Not Found&lt;/title&gt;
&lt;style type=&quot;text/css&quot;&gt;

body {
background-color:	#fff;
margin:				40px;
font-family:		Lucida Grande, Verdana, Sans-serif;
font-size:			12px;
color:				#000;
}

#content  {
border:				#999 1px solid;
background-color:	#fff;
padding:			20px 20px 12px 20px;
}

h1 {
font-weight:		normal;
font-size:			14px;
color:				#990000;
margin: 			0 0 4px 0;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div id=&quot;content&quot;&gt;
		&lt;h1&gt;&lt;?php echo $heading; ?&gt;&lt;/h1&gt;
		&lt;?php echo $message; ?&gt;
	&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?php endif; ?&gt;
</pre>
<p>So there you have it, following these steps will turn one of the best PHP frameworks into a CLI-capable wonder.  Please feel free to post a comment if you have a question or anything to add.</p>
<p>For more information on using PHP from the command line, see <a href="http://us.php.net/features.commandline">http://us.php.net/features.commandline</a>.</p>


<p>Related posts:<ul><li><a href='http://phpstarter.net/2010/05/send-print-jobs-directly-from-php/' rel='bookmark' title='Permanent Link: Send Print Jobs Directly from PHP'>Send Print Jobs Directly from PHP</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2008/12/run-codeigniter-from-the-command-line-ssh/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>
