2011
03.16

I’ve been running my own installation of the Yourls URL Shortener for some time over at girv.in, and have posted before about it a few times. One question I keep getting asked is “How can I make a page that lists my yourls links like the one on http://girv.in ?”, and I shall now attempt to answer it.

If you’re the impatient type, head on over to github and grab the index page source.

The important part is at the top and is very straightforward. Yourls does most of the work for you in fact!

// pull in YOURLS system and configuration
require_once(dirname(__FILE__).'/includes/load-yourls.php' );
 
// connect to db
$db = yourls_db_connect();
 
// get all URLs
$urls = null;
if ($db) {
    $urls = $db->get_results("SELECT `keyword`,`url`,`title`,`timestamp`,`clicks` FROM `" . YOURLS_DB_TABLE_URL . "` ORDER BY `timestamp` DESC");
}

This snippet initialises your Yours instance, connects to its database and fetches a sorted list of all the saved URLs as PHP objects. You can of course modify the query to return different lists depending on your needs. For example, sorted alphabetically, or limited to the ten most recently added or the twenty most popular.

You should note that the code above is intended for use with a Yourls 1.5 database. Other versions may require adjustment.

The remainder of the index page is simple markup and related code to display the URL list, making use of the jQuery javascript library and the tablesorter plugin to enhance the user experience.

Creating an index page for your personal Yourls installation is very easy and I hope this short tutorial has made it even easier.

The girv.in index page source is public domain and may be freely used for any purpose. Grab it now at github.

Related Posts

  1. yourls for twitter tools update 1.1
  2. yourls plugin for twitter tools 2.0
  3. add url shortening to twitter tools

2 comments so far

Add Your Comment
  1. This is so COOL! thx for the infromation!

  2. Thanks for your tips, But I am still not able to make it work.

    I have installed YOURLS script as a subdirectory. Right now I see INDEX of subdirectory folder to public view. I want to either redirect this to another site or make list of added links appear visible to public.

    Need help.

    Thanks,

    Dave