Example #1
0
    // Let's make sure the length of the input URL is bigger than zero
    if (strlen($url) == 0) {
        return false;
    }
    // Let's hope the user is not shortening URLs with wrong protocols
    if (!($helper->starts_with($url, "http://") || $helper->starts_with($url, "https://") || $helper->starts_with($url, "ftp://"))) {
        return false;
    }
    // Also, let's make sure that the end result will be shorter than the
    // original URL - otherwise, what's the point in shortening it!
    if (strlen($url) < strlen($host) + 1 + $max_length) {
        return false;
    }
    // Finally, make sure that the URL is not already shortened by another
    // URL shortener
    $exclusions = $config->getExcludedUrlShorteners();
    if (in_array($url, $exclusions)) {
        return false;
    }
    return true;
};
$redirect = function ($shortened) use($app, $helper) {
    $row = $helper->find_by_shortened($shortened);
    if (isset($row)) {
        $id = $row["id"];
        $count = $row["count"];
        $helper->update_count($id, $count + 1);
        $original = $row["original"];
        $app->redirect($original);
    } else {
        $app->redirect('/');