This yii Get the current IP (Recommended learning: yii framework )
<?php //当前域名 echo Yii::app()->request->hostInfo; //除域名外的URL echo Yii::app()->request->getUrl(); //除域名外的首页地址 echo Yii::app()->user->returnUrl; //除域名外的根目录地址 echo Yii::app()->homeUrl; ?>
Assume that the access address of our current page is :http://localhost/CMS/public/index.php?r=news&id=1
1.
1. Get the current domain name: echo Yii:: app()->request->hostInfo;
Result: http://localhost
2. Get the current URL (except domain name): echo Yii::app() ->request->getUrl();
Result:/CMS/public/index.php?r=news&id=1
3. Get the home page address (except domain name) : echo Yii::app()->user->returnUrl;
Result:/CMS/public/index.php
4. Get the root directory (except domain name) Address: echo Yii::app()->homeUrl;
Result:/CMS/public/index.php
5, echo Yii::app()->request- >baseUrl;
Result:/CMS/public
6. Get the relative address of the website root directory: echo Yii::app()->baseUrl;
Result :/CMS/public (Note: If you use this method to directly point to the website root directory, it is best to add a "/" after it, because sometimes if you do not add "/", it may not point to the website root directory correctly. Application Example: Home )
二、
7. YII obtains the IP address: Yii::app()->request->userHostAddress;
8. Yii determines the submission method: Yii: :app()->request-isPostRequest;
9. The physical path of the protected directory: Yii::app()->basePath;
10. Get the url of the previous page To return: Yii::app()->request->urlReferrer;
11. Get the current controller ID: Yii::app()->getController()->getAction() ->id;
12. Project path: dirname(Yii::app()->BasePath);
13. Yii gets the data from get and post: Yii:: app()->request->getParam('id');
3.
14. How to set the time zone in Yii:
You can set the time zone 15 in 'timeZone'=>'Asia/Chongqing' in config/main.php to prevent repeated submission: Ccontroler->refresh();
The above is the detailed content of How to obtain ip in yii2.0. For more information, please follow other related articles on the PHP Chinese website!