diff --git a/config.php b/config.php index bd7ba99..d05b5b9 100644 --- a/config.php +++ b/config.php @@ -13,8 +13,8 @@ define('DB_TABLE', 'shortenedurls'); // connect to database -mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); -mysql_select_db(DB_NAME); +$DB = new mysqli(DB_HOST, DB_USER, DB_PASSWORD); +$DB->select_db(DB_NAME); // base location of script (include trailing slash) define('BASE_HREF', 'http://' . $_SERVER['HTTP_HOST'] . '/'); diff --git a/redirect.php b/redirect.php index d929209..f223b96 100644 --- a/redirect.php +++ b/redirect.php @@ -21,7 +21,11 @@ $long_url = file_get_contents(CACHE_DIR . $shortened_id); if(empty($long_url) || !preg_match('|^https?://|', $long_url)) { - $long_url = mysql_result(mysql_query('SELECT long_url FROM ' . DB_TABLE . ' WHERE id="' . mysql_real_escape_string($shortened_id) . '"'), 0, 0); + $result = $DB->query('SELECT long_url FROM ' . DB_TABLE . ' WHERE id="' . $DB->real_escape_string($shortened_id) . '"'); + $result->data_seek(0); + $row = $result->fetch_row(); + $long_url = $row[0]; + @mkdir(CACHE_DIR, 0777); $handle = fopen(CACHE_DIR . $shortened_id, 'w+'); fwrite($handle, $long_url); @@ -30,12 +34,15 @@ } else { - $long_url = mysql_result(mysql_query('SELECT long_url FROM ' . DB_TABLE . ' WHERE id="' . mysql_real_escape_string($shortened_id) . '"'), 0, 0); + $result = $DB->query('SELECT long_url FROM ' . DB_TABLE . ' WHERE id="' . $DB->real_escape_string($shortened_id) . '"'); + $result->data_seek(0); + $row = $result->fetch_row(); + $long_url = $row[0]; } if(TRACK) { - mysql_query('UPDATE ' . DB_TABLE . ' SET referrals=referrals+1 WHERE id="' . mysql_real_escape_string($shortened_id) . '"'); + $DB->query('UPDATE ' . DB_TABLE . ' SET referrals=referrals+1 WHERE id="' . $DB->real_escape_string($shortened_id) . '"'); } header('HTTP/1.1 301 Moved Permanently'); @@ -53,4 +60,4 @@ function getIDFromShortenedURL ($string, $base = ALLOWED_CHARS) $out += strpos($base, $char) * pow($length, $size - $i); } return $out; -} \ No newline at end of file +} diff --git a/shorten.php b/shorten.php index 74ef577..753342c 100644 --- a/shorten.php +++ b/shorten.php @@ -36,7 +36,13 @@ } // check if the URL has already been shortened - $already_shortened = mysql_result(mysql_query('SELECT id FROM ' . DB_TABLE. ' WHERE long_url="' . mysql_real_escape_string($url_to_shorten) . '"'), 0, 0); + $query = 'SELECT id FROM ' . DB_TABLE. ' WHERE long_url="' . $DB->real_escape_string($url_to_shorten) . '"'; + $result = $DB->query($query); + + $result->data_seek(0); + $row = $result->fetch_row(); + $already_shortened = $row[0]; + if(!empty($already_shortened)) { // URL has already been shortened @@ -45,10 +51,11 @@ else { // URL not in database, insert - mysql_query('LOCK TABLES ' . DB_TABLE . ' WRITE;'); - mysql_query('INSERT INTO ' . DB_TABLE . ' (long_url, created, creator) VALUES ("' . mysql_real_escape_string($url_to_shorten) . '", "' . time() . '", "' . mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . '")'); - $shortened_url = getShortenedURLFromID(mysql_insert_id()); - mysql_query('UNLOCK TABLES'); + $DB->query('LOCK TABLES ' . DB_TABLE . ' WRITE;'); + $query = 'INSERT INTO ' . DB_TABLE . ' (long_url, created, creator) VALUES ("' . $DB->real_escape_string($url_to_shorten) . '", "' . time() . '", "' . $DB->real_escape_string($_SERVER['REMOTE_ADDR']) . '")'; + $DB->query($query); + $shortened_url = getShortenedURLFromID($DB->insert_id); + $DB->query('UNLOCK TABLES'); } echo BASE_HREF . $shortened_url; } diff --git a/style.css b/style.css new file mode 100644 index 0000000..d84efd1 --- /dev/null +++ b/style.css @@ -0,0 +1,37 @@ +#shortener{ + display: block; + width: 555px; + margin: auto; +} +#shortener label{ + display: none; + text-align: center; + padding-right: 4em; + font-size: 2.5em; +} +#shortener #longurl{ + width: 400px; +} +#shortener #submitb{ + width: auto; + padding-bottom: 1.5em; +} +#shortener input{ + line-height: 1.5em; + height: 1.5em; + font-size: 1.5em; +} +#shortener::before{ + content: url(logo.png); + display: block; + margin: auto; + width: 555px; + height: 476px; +} +#topheading{ + text-align: center; + font-size: 6em; + padding-right: 1em; + margin-bottom: 10px; + font-family: cursive; +}