php域验证

时间:2013-02-09 06:52:52

标签: php url-shortener

我在面对两个问题时需要帮助。

  1. 不存在域扩展名的链接(.com,.net,ect)将存储在 数据库为单词

  2. 脚本允许自我缩短较短的网址,这是一个主要的 问题。

  3. 我怎么能

    1. 检查域扩展名,否则提交失败

    2. 检查用户是否正在尝试缩短我自己的链接并失败。

    3. 我的代码:

      function remove_http($url) 
      {
        $disallowed = array('http://', 'https://', 'http//', 'https//');
        foreach($disallowed as $d) {
          if(strpos($url, $d) === 0) {
           return str_replace($d, '', $url);
           }
        }
        return $url;
      }
      
      $url_to_shorten = get_magic_quotes_gpc() ? stripslashes(trim($_REQUEST['url'])) : trim($_REQUEST['url']);
      
      if(!empty($url_to_shorten) || parse_url($url_to_shorten, PHP_URL_SCHEME) )
      {
          require('framework/core/config.xml.php');
      
          // check if the URL has already been shortened
          $already_shortened = mysql_result(mysql_query('SELECT id FROM ' . DB_TABLE. ' WHERE long_url="' . mysql_real_escape_string(remove_http($url_to_shorten)) . '"'), 0);
          if(!empty($already_shortened))
          {
              // URL has already been shortened
              $shortened_url = getShortenedURLFromID($already_shortened);
          }
          else
          {
              // URL not in database, insert
              mysql_query('LOCK TABLES ' . DB_TABLE . ' WRITE;');
              mysql_query('INSERT INTO ' . DB_TABLE . ' (long_url, created, creator) VALUES ("' . mysql_real_escape_string(remove_http($url_to_shorten)) . '", "' . time() . '", "' . mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . '")');
              $shortened_url = getShortenedURLFromID(mysql_insert_id());
              mysql_query('UNLOCK TABLES');
          }
          echo BASE_HREF . $shortened_url;
      }
      
      
      function getShortenedURLFromID ($integer, $base = ALLOWED_CHARS)
      {
         $length = strlen($base);
         while($integer > $length - 1)
         {
          $out = $base[fmod($integer, $length)] . $out;
          $integer = floor( $integer / $length );
         }
         return $base[$integer] . $out;
      }
      

0 个答案:

没有答案