将微小的 url 与 codeigniter 集成

发布于 2024-12-24 17:10:05 字数 2522 浏览 5 评论 0原文

我有用于缩短 URL 的 PHP 代码。我想将它与 CodeIgniter 集成。我该怎么做?
我有 3 个页面:index.phppage.php 和一个 .htaccess 文件以及一个数据库备份文件。

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 集成?

I have PHP code for shortening a URL. I want to integrate it with CodeIgniter. How can I do this?
I have 3 pages: index.php, page.php and a .htaccess file and one database backup file.

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 file

<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>

How can I integrate it with CodeIgniter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云之铃。 2024-12-31 17:10:05

当我将其与 codeigniter 集成时,控制器会转到我网站的主页而不是原始网址

在您的代码中,您只是回显 URL 而不是重定向它:

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

when iam integrating this with codeigniter the controller goes to the home page of my site not to the original url

In your code you're just echoing the URL instead of redirecting it:

header('Location: http://' . $_SERVER['SERVER_NAME'] . '/tinyurl/' . $tiny_word);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文