Index.php 852 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace app\index\controller;
  3. use app\index\model\ShortUrlModel;
  4. use think\Controller;
  5. use think\Validate;
  6. class Index extends Controller
  7. {
  8. /**
  9. * 短链接进入接口
  10. */
  11. public function index(){
  12. $params = $this->request->param();
  13. $rule = [
  14. 'to'=>'require'
  15. ];
  16. $validate = new Validate($rule);
  17. if(!$validate->check($params)){
  18. header('HTTP/1.1 400 [to]');
  19. exit;
  20. }
  21. $shortUrlModel = new ShortUrlModel();
  22. $res = $shortUrlModel->where(['url_str'=>"{$params['to']}"])->find();
  23. if(empty($res)){
  24. header('HTTP/1.1 400 [url_str]');
  25. exit;
  26. }
  27. $url = base64_decode($res['old_url']);
  28. header("HTTP/1.1 301 Moved Permanently");
  29. header("Location: $url");
  30. exit;
  31. }
  32. }