这篇文章主要介绍了关于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了
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是php產生短連接的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自動化intifications andMarketingCampaigns.1)設置設置yourphpenvenvironnvironnvironmentwithaweberswithawebserverserververandphp,確保themailfunctionisenabled.2)useabasicscruct

發送電子郵件的最佳方法是使用PHPMailer庫。 1)使用mail()函數簡單但不可靠,可能導致郵件進入垃圾郵件或無法送達。 2)PHPMailer提供更好的控制和可靠性,支持HTML郵件、附件和SMTP認證。 3)確保正確配置SMTP設置並使用加密(如STARTTLS或SSL/TLS)以增強安全性。 4)對於大量郵件,考慮使用郵件隊列系統來優化性能。

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP發送郵件可以通過PHPMailer庫實現。 1)安裝並配置PHPMailer,2)設置SMTP服務器細節,3)定義郵件內容,4)發送郵件並處理錯誤。使用此方法可以確保郵件的可靠性和安全性。

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

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

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。