php生成短網(wǎng)址/短鏈接原理和用法實例分析
本文實例講述了php生成短網(wǎng)址/短鏈接原理和用法。分享給大家供大家參考,具體如下:
需求在我們的項目當(dāng)中,如果需要更好傳播我們的活動鏈接,但是鏈接太長1來是不美觀,2來是太過于“笨重”,例如拼多多,淘寶聯(lián)盟,他們的推廣鏈接都是有短鏈接的,還有新浪微博。
但是,這些始終都是別人的,我們調(diào)用別人的API進(jìn)行生成,不穩(wěn)定,所以可以自己做一個,注冊一個稍微短一些的域名就行。
生成源碼api.php<?phpheader('Content-type:application/json');//GET URL$url = $_GET['url'];//過濾數(shù)據(jù)if (trim(empty($url))) { echo '{'code':'1','url':'未傳入URL'}';}else{ //定義數(shù)據(jù)庫配置 $dbhost = 'xxx';//數(shù)據(jù)庫服務(wù)器地址 $dbuser = 'xxx';//數(shù)據(jù)庫賬號 $dbpwd = 'xxx';//數(shù)據(jù)庫密碼 $dbname = 'xxx';//數(shù)據(jù)庫名 //連接數(shù)據(jù)庫 $con = mysql_connect($dbhost,$dbuser,$dbpwd); if (!$con) { die(’Could not connect: ’ . mysql_error()); } mysql_select_db($dbname, $con); //檢查數(shù)據(jù)庫是否已經(jīng)存在該URL $check = mysql_query('SELECT * FROM 表名 WHERE long_url = ’$url’'); $check_result = mysql_num_rows($check); //如果已經(jīng)存在,則直接返回之前生成的鏈接 if ($check_result) { while ($row_yicunzai = mysql_fetch_array($check)) { $yicunzai_key = $row_yicunzai['dwz_key']; //返回KEY echo '{'code':'0','url':'域名'.$yicunzai_key.''}'; } }else{ //生成KEY $key_str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; $key = substr(str_shuffle($key_str),mt_rand(0,strlen($key_str)-11),4); //生成短鏈接 mysql_query('INSERT INTO lkydwz (long_url, dwz_key) VALUES (’$url’, ’$key’)'); //返回結(jié)果 echo '{'code':'0','url':'域名'.$key.''}'; } //斷開數(shù)據(jù)庫連接 mysql_close($con);}?>訪問源碼index.php
<?phpheader('Content-Type:text/html;charset=utf-8');//獲得當(dāng)前傳過來的KEY$key = $_GET['id'];echo '<title>正在跳轉(zhuǎn)</title>';//過濾數(shù)據(jù)if (trim(empty($key))) { echo '鏈接不存在';}else{ //解析KEY //定義數(shù)據(jù)庫配置 $dbhost = 'xxx';//數(shù)據(jù)庫服務(wù)器地址 $dbuser = 'xxx';//數(shù)據(jù)庫賬號 $dbpwd = 'xxx';//數(shù)據(jù)庫密碼 $dbname = 'xxx';//數(shù)據(jù)庫名 //連接數(shù)據(jù)庫 $con = mysql_connect($dbhost,$dbuser,$dbpwd); if (!$con) { die(’Could not connect: ’ . mysql_error()); } mysql_select_db($dbname, $con); //查詢數(shù)據(jù)庫,通過KEY獲取長鏈接進(jìn)行跳轉(zhuǎn) //檢查數(shù)據(jù)庫是否存在該KEY $check = mysql_query('SELECT * FROM 表名 WHERE dwz_key = ’$key’'); $check_result = mysql_num_rows($check); //如果存在,則解析出長鏈接并跳轉(zhuǎn) if ($check_result) { while ($row_long_url = mysql_fetch_array($check)) { $long_url = $row_long_url['long_url']; // echo '<script>location.href='http://www.cgvv.com.cn/bcjs/7743.html'.$long_url.'';</script>'; header('Location: $long_url'); } }else{ echo '鏈接不存在'; }}?>Apache規(guī)則.htaccess
RewriteEngine On#RewriteBase / RewriteRule ^(w+)$ index.php?id=$1數(shù)據(jù)庫字段
id(int)自增dwz_key(varchar)long_url(text)creat_time(TIMESTAMP)
1、訪問api.php?url=長鏈接,即可生成短鏈接,例如返回JSON
{'code':'0','url':'http://xxx.cn/Hp8R'}
2、新建.htaccess,把上面規(guī)則復(fù)制進(jìn)去,保存3、新建index.php,把上面代碼拷貝進(jìn)去,配置好數(shù)據(jù)庫。訪問http://xxx.cn/Hp8R,就會自動跳轉(zhuǎn)到你的長鏈接
PS:這里為大家推薦一款本站短網(wǎng)址生成工具(也是使用的第三方API接口生成的短網(wǎng)址)
短鏈(短網(wǎng)址)在線生成工具:http://tools.jb51.net/password/dwzcreate
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《PHP中json格式數(shù)據(jù)操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章:
1. 基于 Python 實踐感知器分類算法2. Python如何批量生成和調(diào)用變量3. ASP.Net Core對USB攝像頭進(jìn)行截圖4. ajax動態(tài)加載json數(shù)據(jù)并詳細(xì)解析5. Python 中如何使用 virtualenv 管理虛擬環(huán)境6. python利用opencv實現(xiàn)顏色檢測7. 通過CSS數(shù)學(xué)函數(shù)實現(xiàn)動畫特效8. ASP.Net Core(C#)創(chuàng)建Web站點的實現(xiàn)9. ASP.NET MVC實現(xiàn)橫向展示購物車10. windows服務(wù)器使用IIS時thinkphp搜索中文無效問題
