Syndicate content
> Jump Start to Easy URLs : Using PHP and mySQL

Jump Start to Easy URLs : Using PHP and mySQL

Introduction
Using PHP and mySQL


Using PHP and mySQL

Luckily for me, I use PHP and mySQL. If you do, too, here's an easy solution that you can cobble together quickly.

Start by creating a simple mySQL table called "redirects". If your hosting service doesn't provide a front-end for mySQL table creation, execute an SQL statement like this:

CREATE TABLE redirects ( id mediumint(8) unsigned NOT NULL auto_increment, address varchar(200) NOT NULL, label varchar(60) NOT NULL, PRIMARY KEY (id), KEY id (id, address), UNIQUE id_2 (id));

Once the table has been created, add records to this table. The fully-qualified URL gets entered into the address column, a description (for documentation purposes) in the label column. No need to fill in the id column -- mySQL automatically assigns this number.

For the above link, my record would have the following column values:

address: http://www.webthejoint.com/s... 0/december/index.html

label: Archive list for Dec. 2000 newsletters

Assuming this was the fifth record I put into the table, the URL I would use would be the much simpler:

http://www.webthejoint.com/jump.php?id=5

As you may have guessed, the final piece of this puzzle is a PHP program called "jump".

Copy this code and save as "jump.php" into your root directory. Remove the line counters before use.

~~~~~~~~~~~ ( begin code ) ~~~~~~~~~~~


1 <?php
2 $db_name = "yourdatabasename";
3 $table_name = "redirects";
4
5 $cnx = mysql_connect("localhost", "dbname", "pwd")
6 or die("Couldn't connect.");
7
8 $db = mysql_select_db($db_name, $cnx)
9 or die("Couldn't select database.");
10
11 $sql = "SELECT id, address FROM $table_name
12 WHERE id = '$id'";
13
14 $result = mysql_query($sql,$cnx)
15 or die("Couldn't execute query.");
16
17 while ($row = @mysql_fetch_array($result)) {
18 $id = $row["id"];
19 $address = $row["address"];
20 }
21
22 header("location: $address");
23 ?>

~~~~~~~~~~~ ( end code ) ~~~~~~~~~~~

Now our example link http://www.webthejoint.com/j...) takes the visitor to our "buried" page. The jump program does a lookup to the redirects table in mySQL and returns the record whose id value is "5". The program then sends the browser an HTTP "content has moved" notice, telling it that the page it is after is actually at the "buried" page's address. The browser, upon receiving this notice, transparently moves on to the longer address. That's all there is to it!

Generally I use "straight" links on our site at WEBtheJOINT, but when I find the need to shorten a long URL or perhaps "disguise" the actual link, all I need to do is add a record to my redirects table and note the id number that mySQL has assigned to the record.
Keith Reichley
www.webthejoint.comKeith Reichley is webmaster for WEBtheJOINT, the web resource center for small business. Contact Keith at keith@webthejoint.com.

User login

Christian-Web-Masters.com newsletter

Stay informed on our latest news!