Home > php教程 > PHP源码 > body text

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

PHP中文网
Release: 2016-05-25 17:14:42
Original
1129 people have browsed it

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
?>
Copy after login

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

?>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!