php生成短連接


 

自己沒事研究研究  PHP生成短連接 好多人都花錢買新浪的接口來生成短連接,我自己就寫了一個 當然核心代碼還是借助網絡,我只是負責整合了一下而已。

我是在我本地測試的,接下來詳細說下。

我做的是把域名信息都存在數據庫里面,數據庫表是這樣的

 1.創建個數據庫表

  

CREATE TABLE `links` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `url` varchar(255) DEFAULT NULL,
  `ctime` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=111 DEFAULT CHARSET=UTF8;
 

 

2.配置本地域名

我用的是phpstudy,你用wamp,或者 xampp都一樣,根據我的phpstudy來就行

我phpstudy一個右鍵就可以設置了,如下圖(右鍵-站點域名管理)

箭頭幾個位置是要注意的,自己設置域名 然后選擇你寫的代碼的位置,端口你看着定,寫完了 點新增,右側就出現你設置的了。

肯定有一些用xammp或者用 wamp的朋友這個時候懵逼了,怎么辦 ,我想辦法裝一個xampp,給你們截代碼吧,

<VirtualHost *:80>
    DocumentRoot "E:/project/short"
    ServerName d.cn
    <Directory "E:/project/short">
        DirectoryIndex index.html index.php 
        AllowOverride All
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>
 
 
<VirtualHost *:80>
    DocumentRoot "E:/project/short"
    ServerName localhost
    <Directory "E:/project/short">
        DirectoryIndex index.html index.PHP 
        AllowOverride All
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

這代代碼 寫在哪個文件呢,寫在xampp/apache/conf/extra/httpd-vhosts.conf 你的xampp裝哪個盤就去哪個盤找了,還有 DocumentRoot也要寫你的對應的位置哦

 

3.改域名配置文件

 

位置在
C:\Windows\System32\drivers\etc\hosts

我寫的是這樣的

 4.配置偽靜態文件

在項目的根目錄下創建一個.htaccess文件,里面代碼是

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(\S{1,7})$ index.php?code=$1 [L]
</IfModule>

 

5.寫系統公共函數

func.php

<?php
function b64dec($b64) { //64進制轉換成10進制
    $map = array(
        '0'=>0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,'9'=>9,
        'A'=>10,'B'=>11,'C'=>12,'D'=>13,'E'=>14,'F'=>15,'G'=>16,'H'=>17,'I'=>18,'J'=>19,
        'K'=>20,'L'=>21,'M'=>22,'N'=>23,'O'=>24,'P'=>25,'Q'=>26,'R'=>27,'S'=>28,'T'=>29,
        'U'=>30,'V'=>31,'W'=>32,'X'=>33,'Y'=>34,'Z'=>35,'a'=>36,'b'=>37,'c'=>38,'d'=>39,
        'e'=>40,'f'=>41,'g'=>42,'h'=>43,'i'=>44,'j'=>45,'k'=>46,'l'=>47,'m'=>48,'n'=>49,
        'o'=>50,'p'=>51,'q'=>52,'r'=>53,'s'=>54,'t'=>55,'u'=>56,'v'=>57,'w'=>58,'x'=>59,
        'y'=>60,'z'=>61,'_'=>62,'='=>63
    );
    $dec = 0;
    $len = strlen($b64);
    for ($i = 0; $i < $len; $i++) {
        $b = $map[$b64{$i}];
        if ($b === NULL) {
            return FALSE;
        }
        $j = $len - $i - 1;
        $dec += ($j == 0 ? $b : (2 << (6 * $j - 1)) * $b);
    }
    return $dec;
}
function decb64($dec) { //10進制轉換成64進制
    if ($dec < 0) {
        return FALSE;
    }
    $map = array(
        0=>'0',1=>'1',2=>'2',3=>'3',4=>'4',5=>'5',6=>'6',7=>'7',8=>'8',9=>'9',
        10=>'A',11=>'B',12=>'C',13=>'D',14=>'E',15=>'F',16=>'G',17=>'H',18=>'I',19=>'J',
        20=>'K',21=>'L',22=>'M',23=>'N',24=>'O',25=>'P',26=>'Q',27=>'R',28=>'S',29=>'T',
        30=>'U',31=>'V',32=>'W',33=>'X',34=>'Y',35=>'Z',36=>'a',37=>'b',38=>'c',39=>'d',
        40=>'e',41=>'f',42=>'g',43=>'h',44=>'i',45=>'j',46=>'k',47=>'l',48=>'m',49=>'n',
        50=>'o',51=>'p',52=>'q',53=>'r',54=>'s',55=>'t',56=>'u',57=>'v',58=>'w',59=>'x',
        60=>'y',61=>'z',62=>'_',63=>'=',
    );
    $b64 = '';
    do {
        $b64 = $map[($dec % 64)] . $b64;
        $dec /= 64;
    } while ($dec >= 1);
    return $b64;
    
}

6.寫核心方法   

創建index.php

<?php
include 'func.php';
define("HOST","localhost");
define("DB_NAME","test");
define("USER","root");
define("PASS","root");
 
function make_short_url($url){
    $url=str_ireplace("http://","",$url);
    $pdo = new PDO("mysql:host=".HOST.";dbname=".DB_NAME,USER,PASS); 
    $rs = $pdo ->query("select id from links where url='".$url."'");
    $row = $rs -> fetch(); 
    if($row==false){
        $pdo -> exec("insert into links(url,ctime) values('".$url."','".mktime()."')"); 
        $id=$pdo -> lastinsertid(); 
        return "http://d.cn/".decb64($id);
    }else{
        return "http://d.cn/".decb64($row['id']);
    }
}
 
function get_long_url($code){
    $pdo = new PDO("mysql:host=".HOST.";dbname=".DB_NAME,USER,PASS); 
    $rs = $pdo ->query("select url from links where id='".b64dec($code)."'");
    $row = $rs -> fetch(); 
    if($row==false){
        print "鏈接錯誤";
        exit;
    }else{
        return "http://".$row['url'];
    }
}
//參數的接收與短鏈接返回部分
if($_GET['code']){
    $code=trim($_GET['code'],"/");
    $url=get_long_url($code);
    if($url){
        header("location:$url");
    }
}elseif($_GET['url']){
    $url=trim($_GET['url']);
    print make_short_url($url);
}
 

最后瀏覽器測試下

 

把生成的地址 粘貼到瀏覽器地址欄一訪問就跳轉到 www.cctv.com了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM