系統環境前提:
php
apache2
linux
生成php短網址的操作:
把代碼複制到index.php放在一個只有1個字符(如u)作爲文档夾名的二級目錄中。
爲此目錄增加寫權限,圖省事就chmod 777 u (根目錄也行,爲避免影響別的文档可能要改改代碼)
網址生成結果:
把
http://www.wkwkk.com/wkwkk/
變成
http://127.0.0.1/u/1
php生成短網址的原理:
1,通過form post獲取要變短的url
2,把url放在一個javasc
代碼示例:
<html> <head> <meta charset="utf-8" /> <title>Shorten URL</title> </head> <body> URL to be shortened: (must include protocol like http:// or https:// etc.)<br /> <form method="post"> <textarea rows='3' name="url" ></textarea><br /> <input type="submit" value="submit" /> </form> <br /> <?php if (isset($_POST['url'])) { $origin = $_POST['url']; if (strlen($origin) > 10) { $filename = count(scandir('.')) - 3;// strip php self . .. file_put_contents($filename, '<script type="text/javascript">location.href="'.$origin.'"</script>'); $shortened = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'.$filename; echo 'Original URL is<br /><a href="'.$origin.'">'.$origin.'</a><br />' . 'Shortened URL is<br /><a href="'.$shortened.'">'.$shortened.'</a>'; } else { echo "The URL you entered is NOT valid."; }} ?> </body> </html>
<script type="text/javascript">location.href="http://www.wkwkk.com.com/wkwkk/"</script>