开发者

where better to get user IP in cakephp [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 8 years ago.

Improve this question

Where is it better in bootstrap.php or 开发者_Python百科app_controller.php

it's needed for geolocation.


In 1.x use

RequestHandlerComponent::getClientIP()

it already covers all problems you might be facing

and yes, "at the point in time at which at which you need it"

For 2.x it's best to use

$this->request->getClientIp() 

from inside the controller as documented on http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html#requesthandlercomponent


It really depends on what you are using the IP for. If you need it in all parts of your code, then the app_controller is the place to put it. If you only need it for log in for example, then put it in the users_controller.

That being said, it may be better to write a session variable and then it is available wherever you need it without the code overhead.


echo $_SERVER['REMOTE_ADDR'];

or

$remIP = $_SERVER['REMOTE_ADDR'];

at the point in time at which at which you need it. That way you can be sure it's current, although I'm not sure if it could get stale. It's there in the server all of the time, so there's no concept of when to assign it - just get it when you need it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜