Shell with signature request, YII processes signed request

Source: Internet
Author: User
Tags dsn flock yii

Processing RequestsclassTestControllerextendscontroller{ Public functioninit () {if(!Yii_env_dev) {            Throw  NewExitexception (201, ' can not access not ' in dev mode '); }    }        /**     *     */     Public functionActionclientip () {if(Yii_env_dev) {$request= Yii::$app-getrequest (); $keyStr= ' key_123 '; $timestamp=$request->get (' timestamp '); $sign=$request->get (' sign '); if(MD5($timestamp.$keyStr) ==$sign){                $databaseIp=$request-Getuserip (); //$databaseIp = ' 127.0.0.1 ';                $conainer=NewContainersavefordynamicdata (); $dbComponetConfig= [                    ' Class ' = ' yii\db\connection ', ' dsn ' = ' mysql:host= '.$databaseIp.‘; Port=3306;dbname=database1 ', ' username ' = ' username1 ', ' password ' = ' passwor ' D1 ', ' charset ' = ' utf8 ', ' tableprefix ' = ' prefix_ '                ]; $conainer->setdatabykey (containersavefordynamicdata::D b_component_config,$dbComponetConfig); return' Success '; }            return' Fail '; }        Else{            return' Fail,not in dev mode '; }}} non-standard path cache management UseYii\helpers\filehelper;//$conainer = new \common\components\containersavefordynamicdata ();//$conainer->getdatabykey (' db4dynamic '); /$conainer->setdatabykey (' db4dynamic ', ' 127.0.0.1 ');classcontainersavefordynamicdata{ConstDb_component_config = ' Dbcomponetconfig ';  Public $cacheFileSuffix= '. Bin ';  Public $cachePath= './dynamic-data-cache ';  Public $dirMode= 0775;  Public $keyPrefix= ' ';  Public $fileMode;  Public function__construct () {$this->cachepath = __dir__. ' /.. /.. /dynamic-data-cache '; if(!Is_dir($this-CachePath)) {Filehelper:: CreateDirectory ($this->cachepath,$this->dirmode,true); }    }        /** * The value of the specified key * @param unknown $key * @return Boolean|unknown*/     Public functionGetdatabykey ($key)    {        $key=$this->buildkey ($key); $cacheFile=$this->getcachefile ($key); if(!file_exists($cacheFile)){            return false; }                if(@Filemtime($cacheFile) > Time()) {            $fp= @fopen($cacheFile, ' R '); if($fp!==false) {                @Flock($fp,lock_sh); $cacheValue= @stream_get_contents($fp); @Flock($fp,Lock_un); @fclose($fp); $cacheValue=unserialize($cacheValue); return $cacheValue; }        }                return false; }    /** * Set Value*/     Public functionSetdatabykey ($key,$value,$duration=0)    {        $key=$this->buildkey ($key); $cacheFile=$this->getcachefile ($key); $value=Serialize($value); if(@file_put_contents($cacheFile,$value, LOCK_EX)!==false) {            if($this->filemode!==NULL) {                @chmod($cacheFile,$this-FileMode); }            if($duration<= 0) {                $duration= 31536000;//1 Year            }            return@Touch($cacheFile,$duration+ Time()); } Else {            $error=Error_get_last (); return false; }    }    protected functionBuildkey ($key)    {        $key=MD5(Json_encode ($key)); return $this->keyprefix.$key; }        /** * Cache file name * @param unknown $key * @return String*/    protected functionGetcachefile ($key)    {        return $this->cachepath. Directory_separator.$key.$this-Cachefilesuffix; }} read the database configuration from the cache<?PHPif(Yii_env_dev) {$conainer=NewContainersavefordynamicdata (); if(($data=$conainer->getdatabykey (containersavefordynamicdata::D b_component_config))! =false){        return $data; }    Else{        return [            ' Class ' = ' yii\db\connection ', ' dsn ' = ' mysql:host=127.0.0.1;port=3306;dbname=database1 ', ' u Sername ' + ' username1 ', ' password ' = ' password1 ', ' charset ' and ' utf8 ', ' tablep Refix ' = ' prefix_ '        ]; }    }Else if(yii_env_test) {return [        ' Class ' = ' yii\db\connection ', ' dsn ' = ' mysql:host=127.0.0.1;port=3306;dbname=database1 ', ' username ' = ' username1 ', ' password ' = ' password1 ', ' charset ' = ' utf8 ', ' tableprefix ' = ' prefix_ '    ];}Else if(Yii_env_prod) {} timed request#!/bin/shExport Lang=en_us. UTF-8timestamp=`Date"+%s"' Keystr= "Key_123"Pre_str=$timestamp $keystrSign_str=$(Echo-N$pre _str|md5sum|awk-f ' {print '} ') Req_url_str= "Http://www.xxx.com/index.php?r=test/clientip&sign="$sign _str"Xtamp="$timestampResult_str= ' Curl-s--url$req _url_str`Echo $result _str>>/a/b/c/www.xxx.com/mytasks/logs/log_update_ip.Log

Shell with signature request, YII processes signed request

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.