This article mainly introduces the method of PHP generation short connection, has a certain reference value, now share to everyone, the need for friends can refer to
I'm all right. Research on PHP generated short connection Many people spend money to buy Sina's interface to generate short connections, I wrote a course core code or with the help of the network, I was only responsible for the integration of a bit.
I tested it locally, and I'll say it in detail next.
All I do is put the domain name information in the database, the database table is like this
1. Create a database table
CREATE TABLE ' links ' ( ' id ' int (one) unsigned not null auto_increment, ' url ' varchar (255) DEFAULT NULL, ' CTime ' Int (one) default NULL, PRIMARY KEY (' id ')) engine=myisam auto_increment=111 default Charset=utf8;
2. Configure the local domain name
I use Phpstudy, you use Wamp, or xampp are the same, according to my phpstudy.
I phpstudy a right button can be set up, such as (right-click-site domain name management)
Arrows several positions are to note, you set the domain name and then choose the location of the code you write, the port you look at the fixed, finished the point added, the right side appears you set up.
There must be some with xammp or with wamp friends this time, how to do, I find a way to install a xampp, to you intercept code it,
<virtualhost *:80> documentroot "E:/project/short" ServerName d.cn <directory "e:/project/ Short "> directoryindex index.html index.php allowoverride all Order deny,allow </Directory></VirtualHost> <virtualhost *:80> documentroot "E:/project/short" ServerName localhost <directory "E:/project/short" > directoryindex index.html Index. PHP allowoverride All Order deny,allow-from </Directory></VirtualHost>
This generation of code written in which file, write in xampp/apache/conf/extra/httpd-vhosts.conf your xampp to which disk to go to which disk to find, and DocumentRoot also write your corresponding location OH
3. Change the domain name configuration file
Location in
C:\Windows\System32\drivers\etc\hosts
That's what I wrote.
4. Configuring pseudo-static files
Create a. htaccess file in the root directory of the project, with the code inside
<ifmodule mod_rewrite.c>rewriteengine onrewriterule ^ (\s{1,7}) $ index.php?code=$1 [L]</IfModule>
5. Write system public functions
func.php
<?phpfunction B64dec ($b 64) {//64 binary converted to 10 binary $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 ($b 64); for ($i = 0; $i < $len; $i + +) {$b = $map [$b 64{$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 binary converted to 64 binary 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=> ' = ', '); $b 64 = "; do {$b = $map [($dec% 64)]. $b 64; $dec/= 64; } while ($dec >= 1); return $b 64; }
6. Write the Core method
Create index.php
<?phpinclude ' func.php ';d efine ("HOST", "localhost");d efine ("db_name", "Test");d efine ("USER", "root");d efine (" 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 = Fetch () $rs; 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 = Fetch () $rs; if ($row ==false) {print "link error"; Exit }else{return "http://" $row [' url ']; The}}//parameter is received with a short link that returns a partial 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);}
The last browser test
Paste the generated address into the browser address bar a visit jumps to www.cctv.com.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!