Jump to content

URL shortening service


alex3

Recommended Posts

I have URL shortening service built that I've combined with a file upload service to give users short URLS (domain.com/xyz12) for their uploaded files. This is fine for shortening URLS, but for the locally hosted files (i.e. the uploaded files) the upload script redirects to the file, showing the real file location (domain.com/files/username/file.ext) which is very undesirable, as you can imagine.

 

I've got the rewrite engine redirecting the short URLs to a redirect.php file, which uses the alias (the short part, xyz12) to retrieve the real URL from a MySQL database and redirect to this real URL.

 

redirect.php:

<?php
// Get the DB connection details and some functions
require_once("includes/config.php");
require_once("includes/functions.php");

// Custom connect to db function
db_connect();

$alias = trim(mysql_real_escape_string($_GET['alias']));

if (!preg_match("/^[a-zA-Z0-9_-]+$/", $alias)) {
  header("Location: ".SITE_URL, true, 301);
  exit();
}

// The function get_url is what retrieves the actual URL based on the alias
if (($url = get_url($alias))) {
    header("Location: $url", true, 301);
    exit();
}

header("Location: ".SITE_URL, true, 301);

 

My problem is that I don't know how to hide the location of local files. I can think of a way to start this (I mean, I can easily detect whether the proper URL is a local one) but I can't think of a way to keep this hidden, only showing the alias.

 

This link shows the desired effect, no iframes or anything.

Link to comment
https://forums.phpfreaks.com/topic/169670-url-shortening-service/
Share on other sites

Use something such as this instead to ensure the user is prompted with a download on a file..

 

<?php
header('Content-type: application/zip');

// It will be called downloaded.zip.. if needed.
header('Content-Disposition: attachment; filename="downloaded.zip"');

// Source of original file..
readfile('./files/user/original324_3423.zip');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.