Welcome Guest, Not a member yet? Register   Sign In
URL shortening service
#11

[eluser]xwero[/eluser]
[quote author="TheFuzzy0ne" date="1243964884"]My take on a similar service:

Code:
function shorten_url($url)
{
    return substr($url, 0, 1);
}

(Sorry, couldn't resist). Tongue[/quote]
It doesn't work as you return h every time or if you are lucky someone will get f, g, m, t or s. If you want an url shortener service quick and dirty try
Code:
function shorten_url($url)
{
   file_put_contents('urls.php', '$urls[] = '.$url.';'.PHP_EOL, FILE_APPEND | LOCK_EX);
   return count(file('urls.php'))-1; // remove <?php line from the count
}

function elongate_url($url)
{
   include('urls.php');

   if(isset($urls[$url]))
   {
      header('Location: '$urls[$url]);
      exit;
   }

   header('Location: '$_SERVER['HTTP_REFERER']);
   exit;
}
#12

[eluser]Dregond Rahl[/eluser]
Thanks everyone ^^ even you Fuzzy :p

I don't quite understand the __remap() method O.o

and xwero your idea works i might add a ~ , what other symbols are allowed?

Also this doesn't seem to be working =/

Code:
$route['~([a-zA-Z0-9]+)'] = "snip/re_link/$1";

EDIT: Also if possible, how would i check if the input was a valid URL, And thanks xwero for that alternative method, I might have a use for it for another project.
#13

[eluser]xwero[/eluser]
you need to add the tilde (~) to the allowed_characters as the segments are validated before the routing takes place.
#14

[eluser]Dam1an[/eluser]
The tilde is part of allowed characters by default
Code:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';

Edit: And I'm not sure what could be causing it to not work :-S
#15

[eluser]Dregond Rahl[/eluser]
Code:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_-';

its already there by default =/

Code:
$route['default_controller'] = "main";
$route['scaffolding_trigger'] = "";


// BEGIN SNIP LINK
$route['~([a-zA-Z0-9]+)'] = "snip/re_link/$1";
// END SNIP LINK


// BEGIN PAGES ROUTES
$route['contact_us'] = "main/contact_us";
$route['about_us'] = "main/about_us";
$route['stats'] = "main/stats";
$route['tos'] = "main/tos";
// END PAGES ROUTES

That's what i have so far,


Example url that works:
Code:
http://uqyg.com/~JaJh

Should route to

http://uqyg.com/snip/re_link/~JaJh

the routing doesn't work tho =/
#16

[eluser]jdfwarrior[/eluser]
Well theres different ways of doing it.

Certainly the way xwero is telling you is a valid way as well, I just mentioned the way with the _remap function because of the way you formed the other url. As an explanation of what I was talking about..

You would create your route:
Code:
$route['(:any)'] = snip/$1;

Then in your controller you would create a _remap function. It's purpose is to override index as the default function. Instead remap would be called and accept the next uri segment (supposed to be the function) as a parameter.

Code:
function _remap($url)
{

  if (is_valid_short_url($url)) {

    header('Location: '.get_long_url($url));

  }

  else {

    show_404();

  }

}

Obviously the url functions are made up, checking the database and such would be your own method, I was just showing an example. That would be in your snip controller and coupled with the route I mentioned, would allow you to access short urls via: http://domain.com/snip/<id> or with the route configured, http://domain.com/<id>
#17

[eluser]xwero[/eluser]
which routing doesn't work? the routing to the other controllers?
#18

[eluser]Dregond Rahl[/eluser]
http://uqyg.com/~JaJh

the routing of the snip =]

EDIT: OKie i figured out whats wrong the ~ before $1

Code:
$route['~([a-zA-Z0-9]+)'] = "snip/re_link/~$1";


Sorry guys XD, thanks for all your help <33333

Also is this method good for validating URLs? or would you recommend a different method?

http://www.w3schools.com/PHP/filter_validate_url.asp




Theme © iAndrew 2016 - Forum software by © MyBB