搜尋
首頁php教程PHP源码PHP微博客应用中的URL缩短类

PHP微博客应用中的URL缩短类        

在Unix机上的PHP脚本 .class.php可以复用 下面有4个方法用来缩短 加密 解密 DB中的原始URL 并缩短他 结合Apache的rewrite模块进行路由
如传入一个http://index.php/catagory/python-cookbook_3tdedition.php 重写为..index.php/123/512.html
 在Apache模块中可以定义 catagory请求为123路由地址
 因为是用的单例模式的CONN 处理并发请求 测试的时候MySQL 并发命中会很低  

#!usr/local/bin/php
<?php
/**
 * 使用单例模式 4个方法 编码 解码 测试 压缩URL
 * SQL表urls只有2个字 一个自增长的ID 一个varchar的url字符串
 * 512增加为了防止传入的url太短
 * */
class UrlShortener {
	const OFFSET = 512;
	private $base;
	private $characterMap;

	public function __construct(){
		//初始化私有变量
		$this->characterMap = array(
			&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;,&#39;6&#39;,&#39;7&#39;,&#39;8&#39;,&#39;9&#39;,
			&#39;z&#39;,&#39;x&#39;,&#39;c&#39;,&#39;b&#39;,&#39;v&#39;,
			&#39;f&#39;,&#39;g&#39;,&#39;h&#39;,&#39;j&#39;,&#39;k&#39;,&#39;L&#39;,&#39;m&#39;,&#39;n&#39;,
			&#39;q&#39;,&#39;w&#39;,&#39;r&#39;,&#39;t&#39;,&#39;y&#39;,&#39;p&#39;,&#39;s&#39;,&#39;d&#39;
		);
		$this->base = sizeof($this->characterMap);
	}

	public static function getInstance(){
		static $instance = null;

		if($instance === null){
			$instance = new UrlShortener();
		}
		return $instance;
	}

	public function shorten($conn,$url){
		$escapedUrl = mysql_escape_string($url);

		$res = mysql_query("select &#39;id&#39; from urls where &#39;url&#39; like $escapedUrl",$conn);
		$row = mysql_fetch_row($res);
		mysql_free_result($res);

		//如果有 就进行编码 然后重写进入数据库
		if($row)
			return $this->encode($row[0]+self::OFFSET);
		//如果没有 就先编码后插入
		$res = mysql_query("
			insert into &#39;urls&#39;(&#39;url&#39;) values(&#39;$escapedUrl&#39;)
			",$conn);
		return $this->encode(mysql_insert_id($conn)+self::OFFSET);
	}

	public function encode($val){

		#递归对每一个URL中的字符进行解码
		#@return 解码以后的字符串Url 
		$val += 512;
		if($val < $this->base){
			return $this->characterMap[ $val ];
		}else{
			return $this->encode(floor( $val/$this->base)).
				$this->characterMap[ $val % $this->base];
		}
	}

	public function decode($val){
		$decodeMap = array_flip($this->characterMap);#翻转字典
		$parts = array_reverse(str_split( $val ));

		$index = 0;
		$i = 0;
		foreach($parts as $part){
			$index += $decodeMap[$part] * pow($this->base,$i++);	
		}
		return $index-512;
	}

	public function expand($conn,$index){
		$id = $this->decode($index)-self::OFFSET;
		$res = mysql_query(&#39;
			select "url" from "urls" where &#39;id&#39; = $id
			&#39;,$conn);
		$value = (($row = mysql_fetch_row( $res)))?$row[0]:null;
		mysql_free_result($res);
		return $value;
	}
}#end class
?>

2. [文件] test4Url.php

#!/usr/local/bin/php

<?php //测试一下上面的class

include ( "UrlShortener.class.php" )
$shortener = UrlShortener::getInstance();
set_time_limit(0);

$stdin = fopen("php://stdin",&#39;r&#39;);

while(1){
	$index = trim(fgets($stdin)); 
	return $shortener->expand(getDb(),$index).&#39;\n&#39;;
}

function getDb(){
	static $db = null;
	if(!$db||!mysql_ping($db){
		$db = mysql_connect(&#39;localhost&#39;,&#39;user&#39;,&#39;password&#39;);
		mysql_select_db("test");
	}
	return $db;
}

?>

 以上就是PHP微博客应用中的URL缩短类 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境