bobsch Posted June 29, 2008 Share Posted June 29, 2008 Hey there. I'm kinda a PHP novice, so maybe someone here can help me with this URL Shortening script. I currently have a working solution, but it isn't optimal. Here's the current script: <?php class Redirect { /** * Function: add * Adds a new redirect to the database. * * Parameters: * - $url - URL to add * * Returns: * - slug of the URL */ public function add($url) { global $sql, $config; $exists = $this->exists($url); if(!$exists) { // Doesn't exist. Add it. $slug = $this->new_slug(); $sql->query("insert into `".$config->prefix."redirects` " ."(slug, url) VALUES ('".$slug."', '".addslashes($url)."')"); return $slug; } else { // Exists. Return the existing one. return $exists; } } /** * Function: exists * Checks if a URL already exists. * * Parameters: * - $url - URL to check * * Returns: * - false if it doesn't exist * - slug of the URL if it exists */ public function exists($url) { global $sql, $config; $query = $sql->query("select * from `".$config->prefix."redirects` where url='".addslashes($url)."'"); if(mysql_num_rows($query) == 0) { // URL doesn't exist return false; } else { $row = mysql_fetch_array($query); return $row['slug']; } } /** * Function: new_slug * Generates a slug based on the last * slug in the database. * * Returns: * - new slug */ public function new_slug() { global $sql, $config; // 64 characters, here. Nearly 17 million f'ing possibilities. // Once we reach ---_, we better start running, because this'll go boom! $alphabet = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "_", "-"); $final_char = $alphabet[count($alphabet) - 1]; // Get the previous $query = $sql->query("select slug from `".$config->prefix."redirects` order by id desc limit 1"); if(mysql_num_rows($query) == 0) { // This will only happen once. Ever. return $alphabet[0] . $alphabet[0] . $alphabet[0] . $alphabet[0]; } $row = mysql_fetch_array($query); $last = $row['slug']; // Get the individual characters $fourth = $last{3}; $third = $last{2}; $second = $last{1}; $first = $last{0}; //echo($alphabet[array_search("_", $alphabet) +1].".."); // Change where needed. if($fourth == $final_char) { $fourth = $alphabet[0]; if($third == $final_char) { $third = $alphabet[0]; if($second == $final_char) { $second = $alphabet[0]; if($first == $final_char) { // We're screwed. } else { $first = $alphabet[array_search($first, $alphabet) + 2]; } } else { $second = $alphabet[array_search($second, $alphabet) + 2]; } } else { $third = $alphabet[array_search($third, $alphabet) + 2]; } } else { $fourth = $alphabet[array_search($fourth, $alphabet) + 1]; } return $first . $second . $third . $fourth; } /** * Function: url_for_slug * Gets the URL attributed to the slug. * * Parameters: * - $slug - slug to check * * Returns: * - false if the slug doesn't exist * - URL if the slug exists */ function url_for_slug($slug) { global $sql, $config; $query = $sql->query("select url from `".$config->prefix."redirects` where slug='".addslashes($slug)."'"); if(mysql_num_rows($query) == 0) { return false; } $row = mysql_fetch_array($query); return stripslashes($row['url']); } /** * Function: hit * Increases the hit count of a redirect. * * Parameters: * - $slug - slug to increase the hit count of */ function hit($slug) { global $sql, $config; $hits = $this->hits($slug) + 1; $query = $sql->query("update `".$config->prefix."redirects` " ."set hits = ".$hits." where slug='".addslashes($slug)."'"); return true; } /** * Function: hits * Gets the hit count of a slug. * * Parameters: * - $slug - slug to check * * Returns: * - hit count of slug */ function hits($slug) { global $sql, $config; $query = $sql->query("select hits from `".$config->prefix."redirects` where slug='".addslashes($slug)."' limit 1"); $row = mysql_fetch_array($query); return $row['hits']; } } $redirect = new Redirect(); ?> And here's what I'm trying to replace it with <?php class Redirect { /** * Function: add * Adds a new redirect to the database. * * Parameters: * - $url - URL to add * * Returns: * - slug of the URL */ public function add($url) { global $sql, $config; $exists = $this->exists($url); if(!$exists) { // Doesn't exist. Add it. $slug = $this->(); $sql->query("insert into `".$config->prefix."redirects` " ."(slug, url) VALUES ('".$slug."', '".addslashes($url)."')"); return $slug; } else { // Exists. Return the existing one. return $exists; } } /** * Function: exists * Checks if a URL already exists. * * Parameters: * - $url - URL to check * * Returns: * - false if it doesn't exist * - slug of the URL if it exists */ public function exists($url) { global $sql, $config; $query = $sql->query("select * from `".$config->prefix."redirects` where url='".addslashes($url)."'"); if(mysql_num_rows($query) == 0) { // URL doesn't exist return false; } else { $row = mysql_fetch_array($query); return $row['slug']; } } /** * Function: new_slug * Generates a slug based on the last * slug in the database. * * Returns: * - new slug */ public function new_slug() { global $sql, $config; $num_char = 2; $all_char = array(); // 64 characters, here. Nearly 17 million f'ing possibilities. // Once we reach ---_, we better start running, because this'll go boom! $alphabet = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "_", "-"); $final_char = $alphabet[count($alphabet) - 1]; // Get the previous $query = $sql->query("select slug from `".$config->prefix."redirects` order by id desc limit 1"); if(mysql_num_rows($query) == 0) { // This will only happen once. Ever. $all_char[0] = $alphabet[0]; return $all_char[0]; } $row = mysql_fetch_array($query); $last = $row['slug']; $all_char = $last; $char_to_increment = -1; for($num = $num_char; $num > 0; $num ++) { if($all_char[$num] != $final_char) $char_to_increment = $num; } if($char_to_increment = -1) { $num_char = $num_char + 1; for($num = 0; $num < $num_char; $num ++) $all_char[$num]=$alphabet[0]; } else { $all_char[$char_to_increment] = array_search($all_char[$char_to_increment], $alphabet) + 1; } for($num = 0; $num < $num_char; $num ++) $first = $first + $all_char[$num]; return $first; } /** * Function: url_for_slug * Gets the URL attributed to the slug. * * Parameters: * - $slug - slug to check * * Returns: * - false if the slug doesn't exist * - URL if the slug exists */ function url_for_slug($slug) { global $sql, $config; $query = $sql->query("select url from `".$config->prefix."redirects` where slug='".addslashes($slug)."'"); if(mysql_num_rows($query) == 0) { return false; } $row = mysql_fetch_array($query); return stripslashes($row['url']); } /** * Function: hit * Increases the hit count of a redirect. * * Parameters: * - $slug - slug to increase the hit count of */ function hit($slug) { global $sql, $config; $hits = $this->hits($slug) + 1; $query = $sql->query("update `".$config->prefix."redirects` " ."set hits = ".$hits." where slug='".addslashes($slug)."'"); return true; } /** * Function: hits * Gets the hit count of a slug. * * Parameters: * - $slug - slug to check * * Returns: * - hit count of slug */ function hits($slug) { global $sql, $config; $query = $sql->query("select hits from `".$config->prefix."redirects` where slug='".addslashes($slug)."' limit 1"); $row = mysql_fetch_array($query); return $row['hits']; } } $redirect = new Redirect(); ?> Currently, the modified code just makes the web-page turn blank, and, as you may be able to see, the only code I've modified is contained in the "new_slug" section. Thanks for looking, and thanks for any help! Link to comment https://forums.phpfreaks.com/topic/112477-help-with-a-url-shortening-script/ Share on other sites More sharing options...
dannyb785 Posted June 29, 2008 Share Posted June 29, 2008 I'm not the greatest at reading code and knowing what you're trying to do. And it's especially hard if i don't know what you're trying to do and you don't explain what your problem is. Because we don't know what errors to look for. Can you give us a) What you wish to accomplish(by explaining what you mean by shortening the url code) b) what problems you are having with your code Link to comment https://forums.phpfreaks.com/topic/112477-help-with-a-url-shortening-script/#findComment-577520 Share on other sites More sharing options...
bobsch Posted June 29, 2008 Author Share Posted June 29, 2008 Hey. Basically, what I'm trying to accomplish is this: Take a long URL (Think Google Map Link), assign it a specific suffix on my site (ie http://somesite.com/6), and have that suffix redirect to the Google Map page. Think http://tinyurl.com/. The problem that I am having is, is that it breaks my webpage, and because of that I'm not sure if it works. When I change the current code to mine, the main page of my site, which calls this code I'm using, simply turns blank. No HTML, no text, no error, just blank. Link to comment https://forums.phpfreaks.com/topic/112477-help-with-a-url-shortening-script/#findComment-577559 Share on other sites More sharing options...
dannyb785 Posted June 30, 2008 Share Posted June 30, 2008 Someone correct me if I'm wrong... this requires the .htaccess file to be modified I have a similar setup with my page. Are you going to be making a form for you to submit the google map url? If so, you just submit the url(the part after '.com'), you just insert that into a table. The index of that database entry will be your "domain.com/index". Then you'd modify your .htaccess to convert "domain.com/something" into "domain.com/viewmap.php?id=something" and then on your viewmap.php page, you'd take in the $_GET['id'] variable and do a query for that row in your table. Then just use an iframe, or redirect or whatever to show the user the map. Link to comment https://forums.phpfreaks.com/topic/112477-help-with-a-url-shortening-script/#findComment-577743 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.