John's little part of the web

John's home page

2012-01-05 06:28:11

I needed to read a feed from a wordpress blog so I used the following.  I start by declaring the URI and then reading the data using the PHP file content reader.  This is then passed to the simple xml element for processing.  Once I have the XML from the feed it is just a matter for walking the items with a foreach and print the data.  In this case I use a line item.  I also put the title of the blog and description on the top.  The below code was for testing and had to be modifed for the client server enviroment.

<div>
<?php
$uri = "http://www.site.com/blog/?feed=rss2";
$rawFeed = file_get_contents($uri);
$xml = new SimpleXmlElement($rawFeed);
 
echo 'Title: ' . $xml->channel->title;
echo '</br>';
echo 'Description: ' . $xml->channel->description;
echo '</br>';
 
foreach ($xml->channel->item as $item)
{
?>
<li> <a href='<?php echo $item->link; ?>'
     title='<?php echo 'Posted ' . date('j F Y | g:i a', $item->pubDate)?>'> <?php echo $item->title; ?></a> </li>
 
<?php
}
?>
</div>

 

2011-12-17 04:31:11

Codeigniter has a nice CAPTCHA function (would be nice if we did not need them) which I used on the contact page.  To use this you include the captcha helper, include a table in the database, and then just check the return value against what is expected.  The system handles the cleaning.  When inserting a new record you have to save the IP address, time, and the word to be displayed (I used the default).  In the form validation function I use a call back to handle this.

$this->load->helper('captcha');
$vals = array(
    'img_path' => './images/',
    'img_url' => base_url() . 'images/'
    );

$cap = create_captcha($vals);
$data['cap'] = $cap;

$sqlData = array(
    'captcha_time' => $cap['time'],
    'ip_address' => $this->input->ip_address(),
    'word' => $cap['word']
    );

$query = $this->db->insert_string('captcha', $sqlData);
$this->db->query($query);            
// the call back
$this->form_validation->set_rules('captcha', 'CAPTCHA', 'required|callback_checkCaptcha');
    function checkCaptcha()
    {
        // First, delete old captchas
        $expiration = time()-7200// Two hour limit
        $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration); 
 
        // Then see if a captcha exists:
        $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
        $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
        $query = $this->db->query($sql, $binds);
        $row = $query->row();
        if($row->count == 0)
        {
            $this->form_validation->set_message('checkCaptcha', 'CAPTCHA must match input value');
            return FALSE;
        }
 
        return TRUE;
    }

Search using bing

this is a test