【问题标题】:How to correct HTTPS out URL error with preg-match?如何使用 preg-match 纠正 HTTPS out URL 错误?
【发布时间】:2020-08-09 21:34:57
【问题描述】:

这可能是一个非常基本的问题,我什至不知道该怎么问。我有顶级列表网站,人们可以通过访问我的网站的人获得“输出”数字。
如果用户使用“http://”添加网站,则一切正常,但是,如果用户使用“https://”添加网站,则链接不起作用。

链接将简单地以 'https//' 形式打开。但冒号不会出现在 HTTPS 的基本 URL 中。因此链接无法正确打开。

有谁知道我如何使用preg_matches 来解决这个问题?

if(isset($_GET['action']) && $_GET['action'] == "out" && !empty($_GET['key']))
{
    //echo "<pre>";
    $site = ORM::for_table('topsite')->where_equal('hash_key',$_GET['key'])->find_one();

    //print_r($site);
    //echo $site->url;die();
    $count = ($site->hits_out) + 1;
    //echo $count;

    $query = ORM::get_db()->prepare("update `topsite` set hits_out='".$count."' where id='".$site->id."'");
    if(!preg_match('/http:/',$site->url))
    {
        $site->url = "http://".$site->url;
    }
    if( $query->execute() ) 
    {
        header("Location:$site->url");
    } 
    else
    {
        header("Location: $base_url");
    }
    exit;

【问题讨论】:

    标签: php url https preg-match out


    【解决方案1】:

    s 添加到协议中,并使用? 使其成为可选。然后在header 中使用找到的匹配项,这样您就知道要使用哪个协议了。

    if(!preg_match('/https?:/', $site->url, $protocol))
        {
            $site->url = $protocol[0] . '//' . $site->url;
        }
    

    (您可能可以更改定界符并将//s 也包含在协议中,这样连接会少一些)

    不相关但附加说明,你准备好的声明是不安全的。

    $query = ORM::get_db()->prepare("update `topsite` set hits_out='".$count."' where id='".$site->id."'");
    

    应该写成:

    $query = ORM::get_db()->prepare("update `topsite` set hits_out=? where id= ?");
    

    那么应该使用绑定。根据驱动程序,语法会有所不同,对于 PDO,这将起作用:

    $query->execute(array($count, $site->id))
    

    还有一点不相关,hits_out 的递增应该是在 SQL 中,而不是 PHP。多个用户可能同时点击该页面,而您当前的方法将失去计数。我推荐:

    set hits_out = hits_out + 1
    

    而不是selecting 然后:

    $count = ($site->hits_out) + 1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      相关资源
      最近更新 更多