2012-01-05 3 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

그래서 문제가 무엇입니까 ...? –

+0

iam이 이것을 codeigniter와 통합하면 컨트롤러가 원래 URL이 아닌 내 사이트의 홈 페이지로 이동합니다. – Kichu

답변

0

는 스피 컨트롤러 코드에서 원래 URL

에 나의 사이트의 홈 페이지로 이동 CodeIgniter는 이것을 통합 할 때 당신은 echo를 리디렉션 대신 URL을 보내고있어 :

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