Home > Article > Backend Development > How to get the current url and domain name in Yii_PHP tutorial
This article describes the example of how Yii obtains the current url and domain name. Share it with everyone for your reference. The details are as follows:
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:
The code is as follows:
echo Yii::app()->request->hostInfo;
Results: http://localhost
2. Get the current URL (except domain name):
The code is as follows:
echo Yii::app()->request->getUrl();
Result: /CMS/public/index.php?r=news&id=1
3. Obtain the homepage address (except domain name):
The code is as follows:
echo Yii::app()->user->returnUrl;
Result: /CMS/public/index.php
4. Obtain the root directory address (except domain name):
The code is as follows:
echo Yii::app()->homeUrl;
Result: /CMS/public/index.php
5.
The code is as follows:
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, please add a "/" after it, because sometimes if you don't add "/", you may not be able to point to the website correctly. Root directory. Application example: Home )
2.
7. YII obtains IP address:
The code is as follows:
Yii::app()->request->userHostAddress;
8. Yii determines the submission method:
The code is as follows:
Yii::app()->request-isPostRequest;
9. Physical path of protected directory:
The code is as follows:
Yii::app()->basePath;
10. Get the url of the previous page to return:
The code is as follows:
Yii::app()->request->urlReferrer;
11. Get the current controller ID:
The code is as follows:
Yii::app()->getController()->getAction()->id;
12. Project path:
The code is as follows:
dirname(Yii::app()->BasePath);
13. Yii obtains get and post data:
The code is as follows:
Yii::app()->request->getParam('id');
3.
14. How to set the time zone in Yii:
You can set the time zone in 'timeZone'=>'Asia/Chongqing' in config/main.php
15. Prevent duplicate submissions:
The code is as follows:
Ccontroler->refresh();
I hope this article will be helpful to everyone’s PHP programming based on the YII framework.