代码之家  ›  专栏  ›  技术社区  ›  user9632001

Cron和Curl删除或关闭会话用户

  •  0
  • user9632001  · 技术社区  · 6 年前

    你好,我已经为ubuntu实现了一个cron系统;由于我需要每30秒运行一次的原因,我不得不结合PHP进行一些调整,以达到必要的范围。

    问题是,在实施之前,一切都很顺利;在实现之后,我注意到用户的php会话在1小时后被随机删除。当它应该持续8小时。。。

    已执行CRON文件:

    #!/bin/sh
    cd /opt/lampp/htdocs/control/cronos/
    php cron.php
    

    CRON PHP处理程序:

    <?php
    $j = 0;
    while ($j <= 1) {
        $url    = 'http://127.0.0.1/index.php';
        $fields = array(
            'idprocess' => 'p-cronjobs',
            'idform'    => 'p-cronjobs',
        );
        $postvars         = http_build_query($fields);
        $COOKIE_FILE_PATH = "/tmp/cookiescron.txt";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
        curl_exec($ch);
        curl_close($ch);
        $j++;
        sleep(25);
    }
    ?>
    

    用于PHP Manager Cron执行的Sistem处理程序:

    switch ($idform) {
        case "p-cronjobs":
                $stmtpre      = "SELECT TListIdCJ FROM ListCronJobs WHERE TListCJST=1 AND TListCJSTR=0;";
                $data         = $this->DBMANAGER->BDquery($stmtpre, DB_N_LIST);
                $result_exist = $data->num_rows;
    
                if ($result_exist != 0) {
                    while ($row = mysqli_fetch_assoc($data)) {
    
                        $fields = array(
                            'idprocess' => $row['TListIdCJ'],
                            'idform'    => 'p-runcronjobs',
                        );
                        $postvars = http_build_query($fields);
                        $ch       = curl_init();
                        curl_setopt($ch, CURLOPT_URL, LOCALURL);
                        curl_setopt($ch, CURLOPT_POST, count($fields));
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
                        curl_setopt($ch, CURLOPT_TIMEOUT, 2);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
                        curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
                        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
                        curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
                        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
                        curl_exec($ch);
                        curl_close($ch);
                    }
                }
                break;
    }
    

    如何避免cron和使用curl发出的多个请求出现会话问题?

    1 回复  |  直到 6 年前
        1
  •  0
  •   user9632001 user9632001    6 年前

    问题可能与使用以下设备有关:

    session_set_cookie_params(time() + 3600);
    or
    setcookie('name', 'id', time()+3600, "/");
    

    PHP Garbage Collection clarification

    需要解决这个问题,并在读取数据后使用 Session 如果你有权访问 php.ini 您可以将过期时间设置为0,以防止垃圾收集器终止会话。