0

I'm trying to create my own "shrink link" website, e.g: www.goo.gl and adf.ly

but this website only for my help not for public,

so you see when you shrink your link on goo.gl so it give you link e.g goo.gl/exapmle_value

example_value is value that pass in database, and then gog.gl redirect to real website.

so i want same this think,

i'm using PHP and MySql i try to shrink link, and its out put with table_id,

e.g www.mydomin.com/1 , www.mydomin.com/2 , www.mydomin.com/3

How can i do this by using simple PHP not any PHP frame work?

genpfault
  • 51,148
  • 11
  • 85
  • 139
user2511667
  • 153
  • 1
  • 4
  • 13

1 Answers1

1

This problem needs to be split into two parts.

Firstly, the PHP script to get the "shrink code" of the URL, i.e. the bit that goes after the final forward-slash character:

www.mydomin.com/asdfasdfasfd
                ^^^^^^^^^^^^ This bit is the "shrink code"

You can obtain the URL of the page using $_SERVER['REQUEST_URI'], then all you need to do is get the text after the last slash, using strrpos and substr:

$url  = $_SERVER['REQUEST_URI'];
// Last position of the slash character
$lastSlashPos = strrpos($url, "/");
// Get the text after the last character
$shrinkCode = substr($url, $lastSlashPos);
print "Great, now I need to look up $shrinkCode in MySQL."

This will get you the last part of the URL.


Seperately, you need to get your webserver to present the same PHP page for all the possible URLs on your website.

If you are using apache, one way to cheat would be to set this page as your custom 404 page. Thus, whenever a user visits your site with a shrunken-URL this will point to a path that does not exist.

If a path does not exist, apache will display your custom error 404 page. If this page contains the above code, then you will be able to find the suffix of the URL, containing the "shrink-code".

If you're using a web host then they usually have their own custom UI for selecting the error pages. Simply put the path of your PHP script in here. If you're hosting and configuring apache yourself, then see the manual.

jwa
  • 3,239
  • 2
  • 23
  • 54