検索
ホームページ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 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。