Cara Membuat Short URL – Pemendek URL atau Link PHP. Shortener Url atau sering dikenal dengan pemendek url sangat banyak sekali. Salah satu contoh situs yang menyediakan pemendek url seperti misalnya adf.ly, bit.ly, goo.gl dan masih banyak lagi yang lainya. Cara Membuat Short URL Pemendek Link PHP itu mudah banget caranya.
Pemendek url atau link bertujuan untuk membuat link tidak lebih panjang. Biasanya link panjang sangat membosankan bila dilihat. Maka dari itu shortener url berguna untuk membuat link yang panjang tadi menjadi lebih pendek dan singkat. Url yang pendek dan singkat diasumsikan lebih baik dilihat dan tidak terkesan membosankan di pandangan mata. Nah kali ini saya akan membagi cara membuat aplikasi pemendek url untuk Anda. Aplikasi ini sangat mudah sekali dibuat.
Script php pemendek url sangat banyak di internet. Kali ini saya menggunakan script yang dibuat oleh HarryJerry. Penggunaan url pendek biasanya dipakai untuk mengarahkan link yang panjang, link yang panjang salah satunya adalah link produk afiliasi.
Cara kerja sistem pemendek url
Cara kerja script php ini adalah menggenerasi link atau url yang dimasukkan melalui form, Kemudian link tersebut di simpan kedalam tabel dalam database. Selanjutnya bersamaan dengan penyimpanan tersebut pemendek link baru dihasilkan. Kemudian proses di keluarkan menjadi sebuah link pendek yang mengarah ke link asli. Jadi cara kerjanya adalah apabila link yang sudah di pendekkan tersebut di akses maka secara otomatis link tersebut redirect atau mengarah ke link asli yang sudah di masukkan tadi.
Langkah membuat shortener url
Pada tutorial ini lengkapi dengan script php pemendek url. Yang perlu Anda lakukan adalah membuat database pada server Anda Jika belum tahu,
Pemendek url ini juga dapat Anda letakkan pada server webhosting Anda. Jadi bisa langsung di akses online secara langsung.
Tips pemendek url: tipsnya adalah cari domain sebagus mungkin dan sependek mungkin. Namannya saja pemendek jadi kalau bisa yang paling pendek agar terlihat lebih baik dan menarik.
Berikut file script pemendek link:
1. Index.php
Script ini berisi form yang digunakan untuk menginputkan link atau url yang akan di pendekkan.
<?php require_once 'includes/conf.php'; // Pengaturan khusus require_once 'includes/hjurl.php'; // class url $lilurl = new lilURL(); $msg = ''; # Jika form di submit if ( isset($_POST['longurl']) ) { // escape bad characters from the user's url $longurl = trim(mysql_escape_string($_POST['longurl'])); // set the protocol to not ok by default $protocol_ok = false; // if there's a list of allowed protocols, // check to make sure that the user's url uses one of them if ( count($allowed_protocols) ) { foreach ( $allowed_protocols as $ap ) { if ( strtolower(substr($longurl, 0, strlen($ap))) == strtolower($ap) ) { $protocol_ok = true; break; } } } else // if there's no protocol list, screw all that { $protocol_ok = true; } // add the url to the database if ( $protocol_ok && $lilurl->add_url($longurl) ) { if ( REWRITE ) // mod_rewrite style link { $url = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']).'/'.$lilurl->get_id($longurl); } else // regular GET style link { $url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?id='.$lilurl->get_id($longurl); } $msg = '<p class="success">URL is: <a href="'.$url.'">'.$url.'</a></p>'; } elseif ( !$protocol_ok ) { $msg = '<p class="error">Invalid protocol!</p>'; } else { $msg = '<p class="error">Creation of your lil´ URL failed for some reason.</p>'; } } else // if the form hasn't been submitted, look for an id to redirect to { if ( isSet($_GET['id']) ) // check GET first { $id = mysql_escape_string($_GET['id']); } elseif ( REWRITE ) // check the URI if we're using mod_rewrite { $explodo = explode('/', $_SERVER['REQUEST_URI']); $id = mysql_escape_string($explodo[count($explodo)-1]); } else // otherwise, just make it empty { $id = ''; } // if the id isn't empty and it's not this file, redirect to it's url if ( $id != '' && $id != basename($_SERVER['PHP_SELF']) ) { $location = $lilurl->get_url($id); if ( $location != -1 ) { header('Location: '.$location); } else { $msg = '<p class="error">Sorry, but that lil´ URL isn\'t in our database.</p>'; } } } // print the form ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>SISTEM PHP</title> <style type="text/css"> body { font: .8em "Trebuchet MS", Verdana, Arial, Sans-Serif; text-align: center; color: #333; background-color: #fff; margin-top: 5em; } h1 { font-size: 2em; padding: 0; margin: 0; } h4 { font-size: 1em; font-weight: bold; } form { width: 28em; background-color: #eee; border: 1px solid #ccc; margin-left: auto; margin-right: auto; padding: 1em; } fieldset { border: 0; margin: 0; padding: 0; } a { color: #09c; text-decoration: none; font-weight: bold; } a:visited { color: #07a; } a:hover { color: #c30; } .error, .success { font-size: 1.2em; font-weight: bold; } .error { color: #ff0000; } .success { color: #000; } </style> </head> <body onload="document.getElementById('longurl').focus()"> <h1>SISTEMPHP SHORT URL</h1> <?php echo $msg; ?> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <fieldset> <label for="longurl">URL:</label> <input type="text" name="longurl" id="longurl" /> <input type="submit" name="submit" id="submit" value="SHORT" /> </fieldset> </form> <h4>Contoh : http://www.sistemphp.com/</a></h4> </body> </html>
2. Conf.php
Ini adalah file yang berisi script konfigurasi pemendek url berikut scriptnya:
<?php /* conf.php ( config file ) */ // page title define('PAGE_TITLE', 'lil´ URL Generator'); // MySQL connection info define('MYSQL_USER', 'root'); define('MYSQL_PASS', ''); define('MYSQL_DB', 'url'); define('MYSQL_HOST', 'localhost'); // MySQL tables define('URL_TABLE', 'lil_urls'); // use mod_rewrite? define('REWRITE', true); // allow urls that begin with these strings $allowed_protocols = array('http:', 'https:', 'mailto:'); // uncomment the line below to skip the protocol check // $allowed_procotols = array(); ?>
3. hjurl.php
Script yang berisi class yang digunakan untuk memproses pemendekan url. Script ini menggunakan beberapa fungsi di alam class tersebut, berikut isi scriptnya.
<?php /* lilurl.php ( lilURL class file ) */ class lilURL { // constructor function lilURL() { // open mysql connection mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die('Could not connect to database'); mysql_select_db(MYSQL_DB) or die('Could not select database'); } // return the id for a given url (or -1 if the url doesn't exist) function get_id($url) { $q = 'SELECT id FROM '.URL_TABLE.' WHERE (url="'.$url.'")'; $result = mysql_query($q); if ( mysql_num_rows($result) ) { $row = mysql_fetch_array($result); return $row['id']; } else { return -1; } } // return the url for a given id (or -1 if the id doesn't exist) function get_url($id) { $q = 'SELECT url FROM '.URL_TABLE.' WHERE (id="'.$id.'")'; $result = mysql_query($q); if ( mysql_num_rows($result) ) { $row = mysql_fetch_array($result); return $row['url']; } else { return -1; } } // add a url to the database function add_url($url) { // check to see if the url's already in there $id = $this->get_id($url); // if it is, return true if ( $id != -1 ) { return true; } else // otherwise, put it in { $id = $this->get_next_id($this->get_last_id()); $q = 'INSERT INTO '.URL_TABLE.' (id, url, date) VALUES ("'.$id.'", "'.$url.'", NOW())'; return mysql_query($q); } } // return the most recent id (or -1 if no ids exist) function get_last_id() { $q = 'SELECT id FROM '.URL_TABLE.' ORDER BY date DESC LIMIT 1'; $result = mysql_query($q); if ( mysql_num_rows($result) ) { $row = mysql_fetch_array($result); return $row['id']; } else { return -1; } } // return the next id function get_next_id($last_id) { // if the last id is -1 (non-existant), start at the begining with 0 if ( $last_id == -1 ) { $next_id = 0; } else { // loop through the id string until we find a character to increment for ( $x = 1; $x <= strlen($last_id); $x++ ) { $pos = strlen($last_id) - $x; if ( $last_id[$pos] != 'z' ) { $next_id = $this->increment_id($last_id, $pos); break; // <- kill the for loop once we've found our char } } // if every character was already at its max value (z), // append another character to the string if ( !isSet($next_id) ) { $next_id = $this->append_id($last_id); } } // check to see if the $next_id we made already exists, and if it does, // loop the function until we find one that doesn't // // (this is basically a failsafe to get around the potential dangers of // my kludgey use of a timestamp to pick the most recent id) $q = 'SELECT id FROM '.URL_TABLE.' WHERE (id="'.$next_id.'")'; $result = mysql_query($q); if ( mysql_num_rows($result) ) { $next_id = $this->get_next_id($next_id); } return $next_id; } // make every character in the string 0, and then add an additional 0 to that function append_id($id) { for ( $x = 0; $x < strlen($id); $x++ ) { $id[$x] = 0; } $id .= 0; return $id; } // increment a character to the next alphanumeric value and return the modified id function increment_id($id, $pos) { $char = $id[$pos]; // add 1 to numeric values if ( is_numeric($char) ) { if ( $char < 9 ) { $new_char = $char + 1; } else // if we're at 9, it's time to move to the alphabet { $new_char = 'a'; } } else // move it up the alphabet { $new_char = chr(ord($char) + 1); } $id[$pos] = $new_char; // set all characters after the one we're modifying to 0 if ( $pos != (strlen($id) - 1) ) { for ( $x = ($pos + 1); $x < strlen($id); $x++ ) { $id[$x] = 0; } } return $id; } } ?>
Dengan ketiga file script di atas, silahkan Anda letakkan dalam satu folder di server Anda. Kemudian silahkan Anda Akses root folder tersebut maka akan menghasilkan tampilan seperti berikut ini.
Hasil script pemendek url
Gambar berikut menampilkan url yang dihasilkan pada link yang berwarna biru. Script ini saya masukkan pada folder url.
Dalam pengebangannya membuat short url dapat di ekplorasi menjadi lebih maksimal. Yaitu dengan menambahkan fitur-fitur tertentu misalnya memberikan kostumisasi pada link yang dibuat, menampilkan jumlah visitor url yang telah di pendek kan atau di singkat, kemudian membuat grafik pengelolaan link yang banyak diakses, periode waktu tertentu pembuatan short url dan masih banyak lagi pengebangan yang bisa dilakukan. Demikian Cara Membuat Short URL semoga bermanfaat.
Postingan berikutnya
- cara membuat short url sendiri
cara ngasih tombol skip ads kaya adf.ly gimana?
Untuk pemberian skip ads itu terletak pada halaman di mana redirect akan di arahkan mas. Kalau di adf.ly sebelum di arahkan ke url tujuan maka ada timer sekitar 5 detik. nah di halaman pengarahan tersebutlah terdapat tombol skip. Untuk contoh short url yang saya kembangkan bisa coba di cek disini http://www.kasitau.com
I am impressed with this web site, rattling I am a fan.
Maaf yang mau di tanyakan apa nya ya mas obuy?.. bisa di perdetail