www.webdeveloper.com
Results 1 to 2 of 2

Thread: custom URL shortener help

  1. #1
    Join Date
    Aug 2010
    Posts
    41

    custom URL shortener help

    Hey i am building a URL shortener and I have managed to create random strings, but I can't figure out how to create unique custom URLs. I have gotten through the hard part in actually creating custom URLs, but I don't know how to query the database for matches.

    here is my code

    customercreate.php
    Code:
     <?php  
       require("./db_config.php");  
       
       $url = $_REQUEST['url'];  
       
       if(!preg_match("/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i", $url)) {  
         $html = "Error: invalid URL";  
       } else {  
       
         $db = mysql_connect($host, $username, $password);  
       
           $short = $_POST["custom_url"];  
       	   $match = mysql_query("SELECT * FROM url_redirects WHERE short='$short'")
    	   
       if ($match == $short)
       {
    die('Sorry that custom URL is taken already')
       }
          
           if(mysql_query("INSERT INTO `".$database."`.`url_redirects` (`short`, `url`) VALUES ('".$short."', '".$url."');", $db)) {  
             $html = "Your short URL is<br />kyletest.info/".$short;  
           } else {  
             $html = "Error: cannot find database";  
           }  
    	   	  	   	     
       
         mysql_close($db);  
       }  
     ?>  
       
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
     <html>  
       <head>  
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
         <title>Cut your URLs</title>  
         <link type="text/css" rel="stylesheet" href="./css/style.css" />  
         </head>  
         <body>  
          <div id="pagewrap">  
          <h1>shrt<span class="r">r</span>.me</h1>  
       
          <div class="body">  
            <?= $html ?>  
            <br /><br />  
            <span class="back"><a href="./">X</a></span>  
          </div>  
       
          </div>  
         </body>  
     </html>
    I underlined the part that I was having difficulty with. I can't find out how to check for matches in the database. I know I have to query the row 'short' for matches.

    Thanks for helping

  2. #2
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    This would be your query:

    $query = "SELECT * FROM url_redirects WHERE short='".mysql_real_escape_string($short)."'";

    And these are the functions you'll need to understand to make this work:

    http://php.net/manual/en/function.mysql-query.php
    http://www.php.net/manual/en/functio...l-num-rows.php
    and possibly:
    http://www.php.net/manual/en/function.mysql-result.php
    http://www.php.net/manual/en/functio...etch-assoc.php

    The manual contains enough examples.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles