找回密码
 注册
搜索
免费空间 免费域名 免费AI 老牌主机商首月仅1美分!27美元/年!Spaceship优惠码 Namecheap优惠码阿里云2核2G3M新老续费同享99元/年!
查看: 60|回复: 0

[程序代码] PHP极简短连接

[复制链接]
发表于 2023-10-1 22:59:44 | 显示全部楼层 |阅读模式
前言
好多短链接网站都消失了,为了安全最好自己建立一个,此代码极为精简。
实现方式
可用于短连接开发
随便找个PHP空间存放即可
  1. <html>

  2. <head>

  3. <meta charset="utf-8"/>

  4. <title>简易·短网址</title>



  5. <!-- 防止post重复提交 -->

  6. <script type="text/javascript">

  7. if(window.history.replaceState)

  8. {

  9.     window.history.replaceState(null, null, window.location.href)

  10. }

  11. </script>

  12. </head>





  13. <?php

  14. //防止频繁提交



  15. session_start();

  16. $seconds = '60';     //时间段[秒]

  17. $refresh = '6';      //提交次数

  18. $blocktime = '600';  //封禁时间



  19. $cur_time = time();



  20. //超过提交频率封禁浏览器 重启浏览器可解封

  21. if($_SESSION['refresh_times'] > 999 && $cur_time - $_SESSION['last_time'] < $blocktime ){

  22.     exit('<p><b>请勿频繁提交,设备已封禁!稍后再试。</b></p>');

  23. }



  24. if(isset($_SESSION['last_time'])){

  25.     $_SESSION['refresh_times'] += 1;

  26. }else{

  27.     $_SESSION['refresh_times'] = 1;

  28.     $_SESSION['last_time'] = $cur_time;

  29. }



  30. if($cur_time - $_SESSION['last_time'] < $seconds){

  31.     if($_SESSION['refresh_times'] >= $refresh){

  32.         //超过提交频率 标记X

  33.         $_SESSION['refresh_times'] = 9999;

  34.     }

  35. }else{

  36.     $_SESSION['refresh_times'] = 0;

  37.     $_SESSION['last_time'] = $cur_time;

  38. }

  39. ?>





  40. <?php

  41. //生成随机文件夹(短链接)名称



  42. function GetRandStr($len)

  43. {

  44. $chars = array(

  45. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",

  46. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",

  47. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",

  48. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",

  49. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",

  50. "3", "4", "5", "6", "7", "8", "9"

  51. );

  52. $charsLen = count($chars) - 1;

  53. shuffle($chars);

  54. $output = "";

  55. for ($i=0; $i<$len; $i++)

  56. {

  57. $output .= $chars[mt_rand(0, $charsLen)];

  58. }

  59. return $output;

  60. }



  61. //$dirabc = xxxxx;

  62. $dirabc = GetRandStr(4);

  63. $dir123 = rand(0,9);



  64. //当文件夹(短链接)存在时 增加一位

  65. if(!is_dir($dirabc)){

  66.     $shortdir = $dirabc;

  67. } else {

  68.     $shortdir = $dirabc.$dir123;

  69. }

  70. ?>





  71. <?php

  72. //判断程序根目录路径



  73. $dirpath = dirname($_SERVER['PHP_SELF']);

  74. $rootpath = dirname($_SERVER['PHP_SELF'])."/";

  75. if ($dirpath == "/"){$rootpath = "/";}

  76. ?>





  77. <body>

  78. <p><b>要缩短的 URL(必须包含 http:// 或 https://)</b></p>



  79. <form method="post">

  80. <textarea rows='3' name="url" style="width:100%"></textarea><br/><br/>

  81. <input type="submit" style="font-size:16px;font-weight:900" value="缩短网址"/>

  82. <form><br/><br/>



  83. <?php

  84. if (isset($_POST['url'])){

  85. $origin = $_POST['url'];



  86.     if (strlen($origin) > 10 && strpos($origin, strval("http"))!==false && !is_dir($shortdir)){

  87.     mkdir(iconv("UTF-8", "GBK", $shortdir),0755,true);

  88.     $filename = $shortdir."/index.html";



  89.     file_put_contents($filename,

  90.     '<script type="text/javascript">location.href="'.$origin.'"</script>');

  91.     $shortened = "http://".$_SERVER['HTTP_HOST'].$rootpath.$shortdir;



  92.     echo '<p>原来的 URL:<br/><a href="'.$origin.'" target="_blank">'.$origin.'</a></p>'

  93.          .'<p>缩短的 URL:<br/><a href="'.$shortened.'" target="_blank">'.$shortened.'</a></p>';



  94.     } else {

  95.             echo "<p><b>您输入的URL无效!</b></p>";

  96.     }

  97. }

  98. ?>



  99. </body>

  100. </html>
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|免费吧论坛

GMT+8, 2024-5-6 01:21 , Processed in 0.023627 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表