Short URL Script
First off...the complete script requires a few things:
Linux Web Hosting.
1 MySQL Database.
"public_html" folder allowed to be chmoded to 777.
If any of those 3 things are not possible/not available...then the script will not work on your hosting space.
---
Right, Short URL Scripts are used to create shorter urls for people to use.
For example:
If I wanted to show off my site:
http://www.sub-domain.a-domain.com/d.../myphppage.php but I thought the url was too long for people to ever remember, I would simply place my url into a short url script and it will create a shorter url, for example
http://www.7ds.us/49. That 7ds.us link would then redirect to my original long url.
---
Step One:
I suppose we're going to want to create the MySQL Database...
Log in to your cPanel. (The following steps may differ depending on what version of cPanel you use, I am writing this based off cPanel 10.8.1).
Now, go to the MySQL Databases section, add a database...probably best to call it something similar to your domain name.
Once you have added that, you have to add a User, shouldn't be too hard...
Now add the user to the database, with Privileges set to ALL.
Once the user is added, click the phpMyAdmin link at the bottom of the page (may be different for different cPanels).
On the left of phpMyAdmin, there is a drop-down menu...on that select the database you have just created.
On the right (in the main box), a set of tabs should have appeared above the main content, click the SQL tab.
Now in the text box, copy this into it:
Code:
CREATE TABLE `url` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`url` text NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM;
Now press the Go button.
That will create a table in the database (url).
---
Step Two:
Now that we have all the MySQL stuff out the way...we're going to set up a file that will connect with the database.
Open Notepad (or any other html editor) and copy in this code:
Code:
<?
$server = "localhost";
$username = "user";
$password = "pass";
$database = "user_db";
?>
Explanation:
$server = "localhost"; is usually set at localhost but may be different for your server.
$username = "user"; change user to the username that you added to your database.
$password = "pass"; replace pass with the password you set with your user.
$database = "user_db"; replace user_db with the name of that database.
Save that file as "config.php".
---
Step 3:
Now we're going to create a page that will list all of the short urls that have been processed by the script, this is mainly for admin use...but you can always link to it if you want.
Save the following as "viewall.php"
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>yourdomain.com</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
</head>
<body>
<?
echo "Database Output
";
include("config.php");
mysql_connect($server,$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$query="SELECT * FROM url";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$url=mysql_result($result,$i,"url");
echo "www.yourdomain.com/$id - $url
";
$i++;
}
?>
</body>
</html> Obviously change yourdomain.com with your actual domain...
---
Step Four:
Before we create the index page, I think it would be a good idea to make it look good using CSS. If you need help with CSS, don't hesitate to view the CSS tutorials on this site.
Save the following as stylesheet.css:
Code:
/* CSS Document */
body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
background-color:#EBEBEB;
color: #000000;
}
div {
padding: 1px;
}
a:link { color: #004E82; text-decoration:none; }
a:visited { color: #004E82; text-decoration:none; }
a:hover { color: #E9592F; text-decoration:underline; }
a:active { color: #004E82; text-decoration:none; }
div.header {
border-left:1px solid #000000;
border-right:1px solid #000000;
border-top:1px solid #000000;
border-bottom:1px solid #000000;
width:40%;
font-size:18px;
}
div.inside {
background-color: #CCCCFF;
color: black;
}
div.main {
border-left:1px solid #000000;
border-right:1px solid #000000;
border-bottom:1px solid #000000;
width:40%;
font-size:11px;
border-top:none;
vertical-align:middle;
padding-bottom:0px;
padding-top: 8px;
}
div.footer {
border-left:1px solid #000000;
border-right:1px solid #000000;
border-bottom:1px solid #000000;
width:40%;
font-size:9px;
border-top:none;
} Feel free to edit all the hex codes etc.
---
Step Five:
Now for the index file...
Copy all the code from the text box below and save it as index.php:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>//yourdomain.com :: The URL Shortening Site!</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
</head>
<body>
<center>
</br>
</br>
<div class="header"><div class="inside">yourdomain.com</div></div>
<div class="main">
</br>
</br>
</br><?php
include("config.php");
$c = $_POST['url'];
$go = $_POST['go'];
if (!$go) {
echo "<form method='post' action=''>";
echo "HTTP://<input name='url' value='' />";
echo "<input name='go' value='Make Short URL' type='submit' />";
echo "</form>";
}
else if ($c) {
@mysql_connect ($server, $username, $password);
@mysql_select_db ($database);
$query = "SELECT id FROM url ORDER BY id DESC LIMIT 0,1";
$r = mysql_query($query);
while ($row = @mysql_fetch_array($r)) {
$lastid = $row[id];
}
$newid = $lastid + 1;
mysql_query ("INSERT INTO url VALUES ('','$c')");
mkdir("$newid");
$file = fopen("$newid/index.php", "w");
$c = "$c";
fwrite($file, "<? header("location:http://$c"); ?>");
fclose($file);
mysql_close();
echo "Thank you! Your new Short URL is http://www.yourdomain.com/$newid Go Back
";
} else {
echo "You did not specify your current URL
Go Back
";
}
?>
</br>
</br></div>
<div class="footer"><div class="inside">Short URL Scritp Copyright Oosband © 2006+ </div></div>
<div class="footer"><div class="inside">XHTML :: CSS </div></div>
</center>
</body>
</html> Notes On Index.php:
Replace all the yourdomain.com with your actual domain...
You may have noticed that I added some copyright? If you don't want to keep that there, then please don't use the script. I spent a lot of time creating this, it's only one line too.
You may have also noticed that I have included two links...both are W3 Validator links, you don't have to keep them in, but they show that the code is XHTML and CSS valid...if you do keep them in remember to change the end of the link url.
---
Step Six:
All that is left now is to upload all the files to the public_html folder on your host, and then chmod the folder to 777.
---
Final Notes:
Don't remove your copyright line - This took me days to create.
Remember to chmod correctly.
Enjoy it!