Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick and dirty hacks to make it work in PHP 7 #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] . '/');
Expand Down
15 changes: 11 additions & 4 deletions redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
Expand All @@ -53,4 +60,4 @@ function getIDFromShortenedURL ($string, $base = ALLOWED_CHARS)
$out += strpos($base, $char) * pow($length, $size - $i);
}
return $out;
}
}
17 changes: 12 additions & 5 deletions shorten.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
37 changes: 37 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -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;
}