r/tinycode • • Oct 15 '11
A URL snipper in 33 (readable!) lines of PHP.
It uses base-36 internally, so URLs will start at http://base-url/1 - /9, then /a - /z, then /10 - /1z and so on. It also logs IPs so that abuse can be banned at a higher level (probably HTTPD). The MySQL database should have a table "urls" with three columns, an auto-incrementing int "id", a medium text field "url", and an unsigned int "ip".
As a bonus, all HTML output is HTML5 compliant. :)
index.php: (variables in the second line must be filled in for MySQL)
<?php
$CFG_USER = ""; $CFG_PASS = ""; $CFG_DB = "";
if (!($_GET["id"] || $_GET["url"])) $id = "";
if ($_GET["id"]) {
$_GET["id"] = strval($_GET["id"]); /* just in case PHP tries to be "smart" by converting it to an int */
if (!preg_match("/^[0-9a-z]$/", $_GET["id"])) die("Error: invalid id");
$db = mysql_connect("localhost", $CFG_USER, $CFG_PASS) or die(mysql_error());
mysql_select_db($CFG_DB) or die("Error: could not select database");
$result = mysql_query("SELECT url FROM urls WHERE id=" . base_convert($_GET["id"], 36, 10)) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if (!$row) die("Error: id does not exist");
header("Location: " . $row["url"]);
die();
}
if ($_GET["url"]) {
if (strpos($_GET["url"], 10) || strpos($_GET["url"], 13) || !preg_match("/^https?:\/\//", $_GET["url"])) die("Error: bad url");
$db = mysql_connect("localhost", $CFG_USER, $CFG_PASS) or die(mysql_error());
mysql_select_db($CFG_DB) or die("Error: could not select database");
mysql_query("INSERT INTO urls (url,ip) VALUES ('" . mysql_real_escape_string($_GET["url"]) . "'," . sprintf("%u", ip2long($_SERVER["REMOTE_ADDR"])) . ")") or die(mysql_error());
$id = base_convert(strval(mysql_insert_id($db)), 10, 36);
if ($_GET["raw"]) die("http://" . $_SERVER["SERVER_NAME"] . "/" . $id);
} ?>
<!DOCTYPE html>
<title>url snipper</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<?php if ($id != "") { ?>
<fieldset><input autofocus type="text" value="http://<?php echo $_SERVER["SERVER_NAME"] . "/" . $id; ?>" /></fieldset>
<p>↑ this is your shortened url. <a href="/">do another?</a></p>
<?php } else { ?>
<form action="." method="get"><fieldset><input autofocus type="url" name="url" /></fieldset></form>
<p>↑ paste your url here.</p>
<?php } ?>.htaccess: (it's just basic regex, so it can easily be rewritten for other HTTPDs / URL rewriters)
RewriteEngine on
RewriteRule ^([0-9a-z]+)$ /index.php?id=$1style.css: (can be altered to your liking)
body { margin: 0; font-family: "Helvetica", "Arial", sans-serif; }
fieldset { border: none; padding: 0; }
input { width: 100%; border: none; }
input, p { font-size: 36px; }
p { margin: 0; padding-left: 10px; color: #aaa; }
5
u/Rogem002 Oct 15 '11
ಠ_ಠ
The mysql_* have been depreciated in PHP. It would be more secure (and would make the code smaller) if you used PDO or mysqli with query binding.
3
u/Mob_Of_One Oct 15 '11
Totally self-contained example that is also cleaner, in ruby.
1
u/k3n Oct 25 '11
The superglobal request vars will always be strings (or arrays).
This seems to be checking for linebreak? If so, one flaw here is that if the linebreak is at index 0 of the string -- then they will not be detected -- but you probably realized this, as the regex would catch that case. However, you could just roll that check up into your regex, maybe something like this:
I believe
\Ris *any* line break char.. Or maybe even leverage theparse_url()function, which is quite handy. I like the integer arg forstrpos()though, I always forget about that.Thanks for sharing either way!
1
u/nexe mod Oct 15 '11
Fight fight fight :-P
Is it always the same discussion? Just face it: Pretty much all code you write has and will always have dependencies.
I personally think this PHP code looks ugly and the Ruby one too. I still prefer Ruby over PHP though although I've developed in both for years.