View previous topic :: View next topic |
Author |
Message |
Lyfa The Lonely Man Reputation: 12
Joined: 02 Nov 2008 Posts: 744
|
Posted: Mon Aug 02, 2010 2:42 pm Post subject: URL Shortener |
|
|
Only needs two files.
index.php:
Code: | <?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "url";
$con = mysql_connect($hostname, $username, $password);
mysql_select_db($database, $con);
$error = "";
if (isset($_GET['id'])) {
if(is_numeric($_GET['id'])) {
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM urls WHERE id = '".$id."'") or die(mysql_error());
$data = mysql_fetch_assoc($sql);
if($sql) {
header("location: ".$data['url']."");
} else {
$error = '<div class="error">That URL is not in our database</div>';
}
} else {
$error = '<div class="error">That URL is not in our database</div>';
}
}
if(isset($_POST['url'])) {
$url = mysql_real_escape_string(stripslashes($_POST['url']));
if(!filter_var($url, FILTER_VALIDATE_URL)) {
$error = '<div class="error">That is not a valid URL</div>';
} else {
$sql = mysql_query("INSERT INTO urls (url, date) VALUES ('".$url."', '".time()."')") or die(mysql_error());
if($sql) {
$grab = mysql_query("SELECT * FROM urls ORDER BY id DESC LIMIT 1") or die(mysql_error());
$data = mysql_fetch_assoc($grab);
$error = '<div class="success">Your URL has been entered<br />It is: <a href="http://'.$_SERVER['SERVER_NAME'].'/'.$data['id'].'">http://'.$_SERVER['SERVER_NAME'].'/'.$data['id'].'</a></div>';
} else {
$error = '<div class="error">There was an error entering the URL into the database</div>';
}
}
}
if(empty($error)) {
$error = "Please enter a URL in the textbox below";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>F3ar's URL Shortener</title>
<style type="text/css">
body {
background: #999999;
height: 100%;
}
#errorbox {
position: absolute;
left: 30%;
top: 5%;
width: 528px;
background: lightgrey;
display: table-cell;
padding: 10px 15px 10px 15px;
text-align: center;
color: grey;
font-family: arial;
text-shadow: 0px 1px 0px rgba(255,255,255,0.8);
border: 2px solid #555;
-webkit-box-shadow: 0px 1px 5px grey;
-moz-box-shadow: 0px 1px 5px grey;
box-shadow: 0px 1px 5px grey;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
.error {
color: red;
font-weight: bold;
}
.success {
color: green;
font-weight: bold;
}
.success a {
color: blue;
font-weight: bold;
}
#greybox {
position: absolute;
left: 31%;
top: 8%;
width: 500px;
background: lightgrey;
display: table-cell;
padding: 55px 15px 25px 15px;
text-align: center;
color: grey;
font-family: arial;
text-shadow: 0px 1px 0px rgba(255,255,255,0.8);
border: 1px solid #333;
-webkit-box-shadow: 0px 1px 5px grey;
-moz-box-shadow: 0px 1px 5px grey;
box-shadow: 0px 1px 5px grey;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
a {
color: #666666;
}
</style>
</head>
<body onload="document.getElementById('url').focus()">
<div id="greybox">
<form action="" method="POST">
<label for="url">URL: </label><input type="text" name="url" id="url" value="http://" /><br />
<input type="submit" value="Shrink it!" />
</form>
</div>
<div id="errorbox"><?php echo $error; ?></div>
</body> |
.htaccess:
Code: | RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)$ index.php?id=$1 |
Create a database and insert this:
Code: | CREATE TABLE IF NOT EXISTS `urls` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(500) NOT NULL,
`date` varchar(25) NOT NULL,
`disabled` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ; |
_________________
|
|
Back to top |
|
|
Cryoma Member of the Year Reputation: 198
Joined: 14 Jan 2009 Posts: 1819
|
Posted: Mon Aug 02, 2010 3:09 pm Post subject: |
|
|
Right on.
|
|
Back to top |
|
|
Garavito Master Cheater Reputation: 0
Joined: 21 Jun 2009 Posts: 387
|
Posted: Mon Aug 02, 2010 5:14 pm Post subject: |
|
|
Looks cool now we just need to buy cool domains like "blah.smokes.cra.ck"
_________________
|
|
Back to top |
|
|
Maes Advanced Cheater Reputation: 10
Joined: 09 Apr 2009 Posts: 50
|
Posted: Tue Aug 03, 2010 5:33 pm Post subject: |
|
|
Would you be able to redo this in a way that it makes a subdomain link, instead of like http://example.com/65dfd4, so it would look like http://65dfd4.example.com/
_________________
Account reclaimed by former owner? |
|
Back to top |
|
|
Lyfa The Lonely Man Reputation: 12
Joined: 02 Nov 2008 Posts: 744
|
Posted: Tue Aug 03, 2010 9:45 pm Post subject: |
|
|
It should work for that already.
_________________
|
|
Back to top |
|
|
Maes Advanced Cheater Reputation: 10
Joined: 09 Apr 2009 Posts: 50
|
Posted: Wed Aug 04, 2010 1:16 pm Post subject: |
|
|
F3ar wrote: | It should work for that already. |
It only gives me "http://localhost/5gd43"
_________________
Account reclaimed by former owner? |
|
Back to top |
|
|
Lyfa The Lonely Man Reputation: 12
Joined: 02 Nov 2008 Posts: 744
|
Posted: Wed Aug 04, 2010 5:56 pm Post subject: |
|
|
Rokurai wrote: | F3ar wrote: | It should work for that already. |
It only gives me "http://localhost/5gd43" | It takes the server name, so if it's on localhost it'll give localhost, if it's on google.com it'll say google.com
_________________
|
|
Back to top |
|
|
|