Design Grid

Icon

Resources and Tutorials for Web Designers and Developers

A better way to grab your tweets

Most of my clients are on twitter, this results in my clients wanting to show their latest tweets on their home page.

While Twitter provide JavaScript ‘badges’ to show your latest tweets, it it sluggish and what happens if a user has JavaScript disabled?

My workaround for this was using php to parse the users RSS feed.

//RSS Feed URL
$rss_url = “http://twitter.com/statuses/user_timeline/14942878.rss”;

if (!$rss_data = @file_get_contents($rss_url)) {
echo "Oh snap, it seems twitter is down :(";

} else {

$rss_xml = SimpleXML_Load_String($rss_data);

$channel_title = $rss_xml->channel->title;
$channel_link = $rss_xml->channel->link;
$tweets = array();
foreach ($rss_xml->channel->item as $item) {
$item_title = $item->title;
$item_link = $item->link;
$item_description = $item->description;

$tweet = explode(": ", $item_title);
$tweetmessage = preg_replace("/(http:\/\/[^\s]+)/", "$1", $tweet[1]);
$tweetmessage = preg_replace("/(@[^\s]+)/", "$1", $tweetmessage);
$tweets[] = $tweetmessage ;
}
echo $tweets[0] ;
}

This chuck of code uses a regular expression to parse the XML of your RSS feed, puts each tweet into an array.

Example: Display your 5 most recent tweets:

echo $tweets[0] ;
echo $tweets[1] ;
echo $tweets[2] ;
echo $tweets[3] ;
echo $tweets[4] ;

Enjoy!

Display your unread gmail count using PHP

This little snippet of code does amazing things!

Using imap_open, you can display your unread email count from your googlemail (or any pop/imap server). I used it for my status board, and it works a treat.

$mbox = imap_open ("{imap.googlemail.com:993/imap/ssl/novalidate-cert}INBOX",
"gmailusername", "gmailpassword", OP_READONLY)
or die("can't connect: " . imap_last_error());
$check = imap_mailboxmsginfo($mbox);
if ($check) {
print $check->Unread; //. "/" . $check->Nmsgs;
} else {
print "Failed";
}

Just a couple of things to note:

  • novalidate-cert – This is important for Google Mail, otherwise you will get authentication failures
  • Make sure you have php5-imap installed. [sudo apt-get install php5-imap ; /etc/init.d/apache2 restart]

My status board, inspired by Panic.

So, maybe this is a bit off topic but it is so cool, I had to share it anyway.

Yesterday, I built my own status board (inspired by this: http://www.panic.com/blog/2010/03/the-panic-status-board/)

This is a brief over view of what it does.

It’s built using PHP/MYSQL and uses AJAX requests every 20 seconds to keep it updated constantly.

It shows the latest projects that are not started yet, ready to go, and active. It also has the days events/to-dos, does needing Rice & Sour Cream count as an event?

It is updated via a simple PHP/MYSQL backend from my local machine.

The bottom center module is a real time server status that pings each server to check that it is still alive and well.

The most exciting module is the bottom right that executes an IMAP request to pull the number of unread emails from my googlemail and my work email accounts.

That’s all there is to it, but it looks pretty cool mounted on the wall.

I’d love to know your thoughts on it!

Create a login form using PHP

A quick tutorial on creating a basic PHP login form.

Create a new php file, we will call this one login.php for the tutorial.
In login.php we need to add a form for the user to enter their credentials:

<form action="login_check.php" method="post">
&#60;label>User Name</label>
&#60;input type="text" name="user" />
&#60;label>Password</label>
&#60;input type="password" name="pass" />&#60;br />
&#60;button type="submit" name="submit">Sign in</button>
&#60;/form>

Now we create login_check.php. In there we put the following code:

&#60;?php
// Set the username and password here
$user = "username";
$pass = "password";
if ($_POST['user']==$user && $_POST['pass']==$pass){
$pass_secure = md5($pass); // Add md5 encryption to the password
$expire=time()+60*60*24*30; //Set the expiration of the cookie
setcookie("username", $user, $expire); // Add a cookie called "username"
setcookie ("password", $pass_secure, $expire); //Add a cookie called "password"
header('Location: whatever_page_you_want.php');
}
else {
echo "Incorrect username/password";
}
}
?>

Effectively, the code above checks the username and password, if they match then it sets a cookie for the username and password.

Now if you add this code to the top of every page you want protected by the login script then if these cookies are not set, it will redirect you back to the login page:
&#60;?php
if(!$_COOKIE['username'] || !$_COOKIE['pass']){
header('Location: login.php');
}
?>

Pretty simple eh? This form is by no means secure as it stores the login data using cookies. Even thought the password has been encrypted using md5, it can still be decoded! You have been warned!