2012-01-05 50 views
0

我有用於縮短URL的PHP​​代碼。我想將它與CodeIgniter集成。我怎樣才能做到這一點?
我有3頁:index.php,page.php.htaccess文件和一個數據庫備份文件。集成微小的URL與codeigniter

index.php

<?php 
mysql_connect("localhost","root",""); 
mysql_select_db("test"); 

$url=$_GET['url']; 
$url_details=mysql_fetch_array(mysql_query("select * from tinyurls where shorturl like ('".$url."')")); 
if(!empty($url_details)){ 
    header("location: ".$url_details['actualurl']); 
}else{ 
    echo '<h2 style="color:red">Error 404: Page not found</h2>'; 
} 
?> 


page.php

<?php 
if(isset($_POST['url'])){ 
mysql_connect("localhost","root",""); 
mysql_select_db("test"); 

    $exist=mysql_fetch_array(mysql_query("select * from tinyurls where actualurl='".$_POST['url']."'")); 
    if(empty($exist)) 
    { 
     function get_random_word($length=6){ 
      $chars='abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
      $word=""; 
      for($i=0;$i<$length;$i++){ 
       $word.=substr($chars,rand(1,strlen($chars)),1); 
      } 

      $isexist=mysql_fetch_array(mysql_query("select id from tinyurls where shorturl like ('".$word."')")); 
      if(empty($isexist)){ 
       return $word; 
      }else{ 
       return ""; 
      } 
     } 

     $tiny_word=""; 
     while($tiny_word==""){ 
      $tiny_word=get_random_word(); 
     } 
     mysql_query("insert into tinyurls set shorturl='".$tiny_word."', actualurl='".$_POST['url']."'"); 
    }else{ 
     $tiny_word=$exist['shorturl']; 
    } 
    echo "TinyURL: http://".$_SERVER['SERVER_NAME']."/tinyurl/".$tiny_word."<br/><br/>"; 
} 
?> 
<html> 
    <head><title>Shorten URL</title></head> 
    <body> 
     <form method="post" action=""> 
      Enter the URL:<br/> 
      <input type="text" name="url" style="padding:5px;width:500px"/><br/><br/> 
      <input type="submit" style="padding:5px" value="Shorten URL"> 
     </form> 
    </body> 
</html> 

.htaccess文件

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteRule ^p\/(.*)$ page.php?q=$1 [L] 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 
</IfModule> 

我如何使用CodeIgniter整合呢?

+0

那麼是什麼問題...? – 2012-01-05 06:06:24

+0

當iam將此與codeigniter集成時,控制器會轉到我網站的主頁而不是原始網址 – Kichu 2012-01-05 06:08:21

回答

0

亞姆時使用CodeIgniter控制器進入我的網站的主頁而不是原來的URL

在你的代碼集成這你只是echo荷蘭國際集團的URL不是重定向它的:

header('Location: http://' . $_SERVER['SERVER_NAME'] . '/tinyurl/' . $tiny_word);