经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
php基于tp5.1 实现短链接功能 (redis+nginx)
来源:cnblogs  作者:also_think  时间:2020/11/9 16:11:52  对本文有异议
  1. 生成及存储短链接
  1. // 生成短链接
  2. function get_rand_str($len = 12)
  3. {
  4. if (!is_int($len) || $len < 0) {
  5. return false;
  6. }
  7. $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  8. $string = '';
  9. for ($i = $len; $i > 0; $i--) {
  10. $string .= $char[mt_rand(0, strlen($char) - 1)];
  11. }
  12. return $string;
  13. }
  14. // 存储短链接对应的长链接
  15. function set_cache($real_url, $expire = 3600)
  16. {
  17. $key = get_rand_str(6);
  18. // cache 为redis实例, 此处以tp5.1为例, 其他框架需更改为自己的用法
  19. if (cache($key)) {
  20. return self::set_cache($real_url);
  21. } else {
  22. cache($key, $real_url, $expire);
  23. }
  24. return $key;
  25. }

 

  2. 更改nginx配置, 将短链接重定向到处理文件

  1. server{
  2. ....
  3.  
  4. #短链接(http(s)://xxx.com/s/xxx)重定向到 s文件夹下的share.php处理文件
  5. location /s/ {
  6. rewrite ^(/s/)(.*)$ $1/share.php?k=$2;
  7. }
  8. }

 

share.php文件

  1.  
  1. <?php

    /**
    * @desc Class Share 分享处理类
    * @author Ni
    * @date 2020年10月19日10:26:41
    */
    class Share
    {
    private static $instance;

    private function __clone()
    {
    }

    private $config = null;
    private $redisConf = null;
    private $redisHandle = null;
    private $url = null;

    //构造方法私有化,防止外部创建实例
    private function __construct()
    {
    }

    public static function getInstance()
    {
    //判断实例有无创建,没有的话创建实例并返回,有的话直接返回
    if (!(self::$instance instanceof self)) {
    self::$instance = new self();
    }
    return self::$instance;
    }

    private function init()
    {
    $this->getConfig();
    $this->initRedisHandle();
    }

    /**
    * @desc 获取配置
    */
    private function getConfig()
    {
    // $this->config = include_once './config.php';
    $this->config = [
    'redis' => [
    'host' => '127.0.0.1',
    'port' => 6379,
    // 全局缓存有效期(0为永久有效)
    'expire' => 3600,
    // 缓存前缀
    'prefix' => '',
    'password' => 'you_redis_password'
    ]
    ];

    if (isset($this->config['redis'])) {
    $this->redisConf = $this->config['redis'];
    } else {
    $this->return404();
    }
    }

    private function return404()
    {
    $html = file_get_contents('./404.html');
    echo $html;
    exit;
    }

    private function initRedisHandle()
    {
    try {
    if (!$this->redisHandle) {
    $this->redisHandle = new Redis();
    $this->redisHandle->connect($this->redisConf['host'], $this->redisConf['port'], $this->redisConf['expire']);
    $this->redisHandle->auth($this->redisConf['password']);
    } else {
    return true;
    }
    } catch (Exception $e) {
    $this->return404();
    }

    }

    private function getUrl()
    {
    // 获取get参数k
    $k = isset($_GET['k']) ? $_GET['k'] : '';
    if (!$k) {
    $this->return404();
    }

    // 获取缓存信息
    $cache = $this->redisHandle->get($this->redisConf['prefix'] . $k);
    if (!$cache) {
    $this->return404();
    }
    $this->url = $cache;

    }

    private function redirectUrl()
    {
    if ($this->url) {
    header("Location: $this->url");
    } else {
    $this->return404();
    }
    exit;
    }

    function run()
    {
    $this->init();
    $this->getUrl();
    $this->redirectUrl();
    }
    }

    $share = Share::getInstance();
    $share->run();
  1.  

附录: 404.html(页面代码来源于网络)

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.  
  5. <head>
  6.  
  7. <meta charset="UTF-8" http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8.  
  9. <title>404-对不起!您访问的页面不存在</title>
  10.  
  11. <style type="text/css">
  12. .head404{ width:580px; height:234px; margin:50px auto 0 auto; background:url(https://www.daixiaorui.com/Public/images/head404.png) no-repeat; }
  13. .txtbg404{ width:499px; height:169px; margin:10px auto 0 auto; background:url(https://www.daixiaorui.com/Public/images/txtbg404.png) no-repeat;}
  14. .txtbg404 .txtbox{ width:390px; position:relative; top:30px; left:60px;color:#eee; font-size:13px;}
  15. .txtbg404 .txtbox p {margin:5px 0; line-height:18px;}
  16. .txtbg404 .txtbox .paddingbox { padding-top:15px;}
  17. .txtbg404 .txtbox p a { color:#eee; text-decoration:none;}
  18. .txtbg404 .txtbox p a:hover { color:#FC9D1D; text-decoration:underline;}
  19.  
  20. </style>
  21.  
  22. </head>
  23.  
  24.  
  25.  
  26. <body bgcolor="#494949">
  27.  
  28. <div class="head404"></div>
  29.  
  30. <div class="txtbg404">
  31.  
  32. <div class="txtbox">
  33.  
  34. <p>对不起,您请求的页面不存在、或已被删除、或暂时不可用</p>
  35.  
  36. <p class="paddingbox">请点击以下链接继续浏览网页</p>
  37. <p><a href="你的网站首页">返回网站首页</a></p>
  38.  
  39. </div>
  40.  
  41. </div>
  42.  
  43. </body>
  44.  
  45. </html>
  46. </html>

 

原文链接:http://www.cnblogs.com/think-a-lot/p/13930795.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号