Yii gets the current URL and domain name method _php instance

Source: Internet
Author: User
Tags set time yii

The examples in this article describe how Yii obtains the current URL and domain name. Share to everyone for your reference. Specifically as follows:

Let's say the address of our current page is: http://localhost/CMS/public/index.php?r=news&id=1

One

1, get the current domain name:

Copy Code code as follows:
Echo Yii::app ()->request->hostinfo;

Result: http://localhost

2. Get the current URL (except the domain name):

Copy Code code as follows:
Echo Yii::app ()->request->geturl ();

Result:/cms/public/index.php?r=news&id=1

3, get (except the domain name) home address:

Copy Code code as follows:
Echo Yii::app ()->user->returnurl;

Result:/cms/public/index.php

4, get (except the domain name) root directory address:

Copy Code code as follows:
Echo Yii::app ()->homeurl;

Result:/cms/public/index.php

5,

Copy Code code as follows:
Echo Yii::app ()->request->baseurl;

Result:/cms/public

6, get the site root directory relative address: Echo Yii::app ()->baseurl;
Result:/cms/public (Note: If you point to the site root directly in this way, it is best to add a "/" after it, because sometimes if you do not add "/", you may not be able to correctly point to the site root directory. Application Example: <a href= "<?php Echo Yii::app ()->baseurl;? >/">Home</a>)

Two

7, Yii access to IP address:

Copy Code code as follows:
Yii::app ()->request->userhostaddress;

8, Yii judgment submission method:

Copy Code code as follows:
Yii::app ()->request-ispostrequest;

9, the physical path of the proteced directory:

Copy Code code as follows:
Yii::app ()->basepath;

10, get the URL of the previous page to return:

Copy Code code as follows:
Yii::app ()->request->urlreferrer;

11. Get Current Controller ID:

Copy Code code as follows:
Yii::app ()->getcontroller ()->getaction ()->id;

12. Project Path:

Copy Code code as follows:
DirName (Yii::app ()->basepath);

13, Yii access to get,post data:

Copy Code code as follows:
Yii::app ()->request->getparam (' id ');

Three

14, Yii How to set the time zone:
Can be in config/main.php ' TimeZone ' => ' asia/chongqing ', set time zone

15. Prevent repeated submissions:

Copy Code code as follows:
Ccontroler->refresh ();

I hope this article will help you with the PHP program design based on the YII framework.

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.