developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See The Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Why Firefox Doesn't Take Google Chrome Features    June 26, 2009
First Major PHP Update in Years Coming Soon    June 25, 2009
Red Hat CEO Calls on Oracle to Keep Java Open    June 25, 2009
Google Widens AdSense for iPhone, Android Apps    June 24, 2009
Free Tech Newsletter -

Creating a Custom RSS Feed Aggregator
By W. Jason Gilmore

Go to page: Prev  1  2  

The PHP Script

The final task is to create the script that will display the user's chosen RSS feeds. This script, titled myrss.php, is surprisingly simple, consisting of just two main components:

  • Login: The user will need to log in to view his customized feeds. This is accomplished by using a simple form and some logic that will confirm the supplied e-mail address and password.
  • RSS Aggregation and Delivery: Assuming a valid login (or valid session; see next paragraph), we'll use the Magpie RSS Parser to display the user's chosen RSS feeds.

To save users the hassle of having to repeatedly log in every time they need an RSS "fix," I make use of PHP's session-handling feature. Once successfully logged in, the user's unique identifier is stored as a session variable for later retrieval. Keep in mind that the storage duration is entirely dependent upon how your PHP installation's session functionality is configured.

Listings 1-1 and 1-2 offer the commented code for both components. Both components are assembled and available for viewing via a link posted at the conclusion of Listing 1-2.

Listing 1-1: The Login Component

// Start or continue a session

session_start();


// Has the user

if (! isset($_SESSION['userid']))
{

   if (! isset($_POST['email']))
   {

      echo "<form action='myrss.php' method='post'>";
      echo "Email:<br />";
      echo "<input type='text' name='email' size='20'
                   maxlength='55' value='' /><br />";
      echo "Password:<br />";
      echo "<input type='password' name='pswd' size='20'
                   maxlength='20' value='' /><br />";
      echo "<input type='submit' value='login'>";
      echo "</form>";

   } else {

      mysql_connect("localhost","aggregator","secret");
      mysql_select_db("rssfeeds");

      $email = $_POST['email'];
      $pswd = md5($_POST['pswd']);

      $query = "SELECT rowID, email, pswd FROM user 
                WHERE email='$email' AND '$pswd'";
      $result = mysql_query($query);

      if (mysql_numrows($result) != 1)
      {
         echo "<p>Could not login!</p>";
      } else {
         list($rowID, $email, $pswd) = mysql_fetch_row($result);
         $_SESSION['userid'] = $rowID;
      }

      mysql_close();

   } // end isset[email]

}

Listing 1-2. The RSS Aggregation and Delivery Component

if (isset($_SESSION['userid'])) {

   require_once("rss_fetch.inc");

   mysql_connect("localhost","aggregator","secret");
   mysql_select_db("rssfeeds");	

   $userID = $_SESSION['userid'];

   $query = "SELECT rss.title, rss.url
             FROM rssfeed AS rss, user_to_rss_feed as userrss
             WHERE userrss.userid='$userID' 
             AND rss.rowID=userrss.rssid";

   $result = mysql_query($query);

   while(list($title,$url) = mysql_fetch_row($result)) 
   {

      $url = "$url";
      $rss = fetch_rss( $url );

      echo "<strong>" . $rss->channel['title'] . "</strong><p>";
      echo "<ul>";
      foreach ($rss->items as $item) 
      {
         $href = $item['link'];
         $title = $item['title'];
         echo "<li><a href=$href>$title</a></li>";
      }

      echo "</ul>";

   }

   mysql_close();

}

View the complete script here

Figure 1-1 depicts the login form.

Figure 1-2 depicts a typical display of requested RSS feeds.

Conclusion

I welcome questions and comments! E-mail me at jason@wjgilmore.com. I'd also like to hear more about your experiences integrating RSS feeds with PHP and MySQL!

About the Author

W. Jason Gilmore (http://www.wjgilmore.com/ ) is an Internet application developer for the Fisher College of Business. He's the author of the upcoming book, PHP 5 and MySQL: Novice to Pro, due out by Apress in 2004. His work has been featured within many of the computing industry's leading publications, including Linux Magazine, O'Reillynet, Devshed, Zend.com, and Webreview. Jason is also the author of A Programmer's Introduction to PHP 4.0 (453pp., Apress). Along with colleague Jon Shoberg, he's co-author of "Out in the Open," a monthly column published within Linux magazine.

# # #

Go to page: Prev  1  2  

Previous article: Searching Active Directory with Perl


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


XML Archives






internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs