首页 > 代码库 > yii2_自定义Component
yii2_自定义Component
1
namespace common\components; use Yii; use yii\base\Component; class ReadHttpHeader extends Component { public function RealIP() { $ip = false; $seq = array(‘HTTP_CLIENT_IP‘, ‘HTTP_X_FORWARDED_FOR‘ , ‘HTTP_X_FORWARDED‘ , ‘HTTP_X_CLUSTER_CLIENT_IP‘ , ‘HTTP_FORWARDED_FOR‘ , ‘HTTP_FORWARDED‘ , ‘REMOTE_ADDR‘); foreach ($seq as $key) { if (array_key_exists($key, $_SERVER) === true) { foreach (explode(‘,‘, $_SERVER[$key]) as $ip) { if (filter_var($ip, FILTER_VALIDATE_IP) !== false) { return $ip; } } } } } }
2在common/config/main-local.php
// 新添加的 ‘ReadHttpHeader‘ => [ ‘class‘ => ‘common\components\ReadHttpHeader‘ ],
3调用自定义组件backend\controllers\SiteController.php
public function actionIndex() { //自定义组件 echo Yii::$app->ReadHttpHeader->RealIP(); return $this->render(‘index‘); }
yii2_自定义Component
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。