Yii2 get correct client IP
I use this function for get client ip:
$client = @$_SERVER['HTTP_CLIENT_IP'];
You can see it here.
http://city.ru.xsph.ru/web/
But this address does not match the IP that use other hosts (the correct IP). For example:
https://hidemyna.me/en/ip/
yii2 ip-address
add a comment |
I use this function for get client ip:
$client = @$_SERVER['HTTP_CLIENT_IP'];
You can see it here.
http://city.ru.xsph.ru/web/
But this address does not match the IP that use other hosts (the correct IP). For example:
https://hidemyna.me/en/ip/
yii2 ip-address
do mark the correct answer that worked for you
– Muhammad Omer Aslam
Jan 30 at 12:52
add a comment |
I use this function for get client ip:
$client = @$_SERVER['HTTP_CLIENT_IP'];
You can see it here.
http://city.ru.xsph.ru/web/
But this address does not match the IP that use other hosts (the correct IP). For example:
https://hidemyna.me/en/ip/
yii2 ip-address
I use this function for get client ip:
$client = @$_SERVER['HTTP_CLIENT_IP'];
You can see it here.
http://city.ru.xsph.ru/web/
But this address does not match the IP that use other hosts (the correct IP). For example:
https://hidemyna.me/en/ip/
yii2 ip-address
yii2 ip-address
edited Nov 16 '18 at 12:32
Muhammad Omer Aslam
13.5k72548
13.5k72548
asked Nov 15 '18 at 18:18
rand_triestrand_triest
659
659
do mark the correct answer that worked for you
– Muhammad Omer Aslam
Jan 30 at 12:52
add a comment |
do mark the correct answer that worked for you
– Muhammad Omer Aslam
Jan 30 at 12:52
do mark the correct answer that worked for you
– Muhammad Omer Aslam
Jan 30 at 12:52
do mark the correct answer that worked for you
– Muhammad Omer Aslam
Jan 30 at 12:52
add a comment |
3 Answers
3
active
oldest
votes
You can use the Yii2 default method available in YI2
Yii::$app->getRequest()->getUserIP()
or if you are looking to save the current user ip into the database table i would recommend you to use the IPbehavior
within the model like
public function behaviors()
return [
...
'ip' => [
'class' => IpBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip',
],
]
...
];
Or like this. where value can be a string or an anonymous function that will return a string.
public function behaviors()
return [
...
'ip' => [
'class' => IpBehavior::className(),
'createdIpAttribute' => 'created_ip',
'updatedIpAttribute' => 'updated_ip',
'value' => '127.0.0.1',
]
...
];
add a comment |
I think to get client IP address you have to use this code:
Yii::$app->request->userIP
add a comment |
The prefered way of getting client ip is with Yii::$app->request-remoteIp
This method uses either $_SERVER['REMOTE_ADDR']
or ip headers (like X-Forwarded-For) that can be added by load balencers or proxy services.
there are however still situations where your public ip (https://hidemyna.me/en/ip/) and the ip that your appication is showing (http://city.ru.xsph.ru/web/) will be different - if you're on the same network as your app for example
But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?
– rand_triest
Nov 15 '18 at 21:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325630%2fyii2-get-correct-client-ip%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the Yii2 default method available in YI2
Yii::$app->getRequest()->getUserIP()
or if you are looking to save the current user ip into the database table i would recommend you to use the IPbehavior
within the model like
public function behaviors()
return [
...
'ip' => [
'class' => IpBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip',
],
]
...
];
Or like this. where value can be a string or an anonymous function that will return a string.
public function behaviors()
return [
...
'ip' => [
'class' => IpBehavior::className(),
'createdIpAttribute' => 'created_ip',
'updatedIpAttribute' => 'updated_ip',
'value' => '127.0.0.1',
]
...
];
add a comment |
You can use the Yii2 default method available in YI2
Yii::$app->getRequest()->getUserIP()
or if you are looking to save the current user ip into the database table i would recommend you to use the IPbehavior
within the model like
public function behaviors()
return [
...
'ip' => [
'class' => IpBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip',
],
]
...
];
Or like this. where value can be a string or an anonymous function that will return a string.
public function behaviors()
return [
...
'ip' => [
'class' => IpBehavior::className(),
'createdIpAttribute' => 'created_ip',
'updatedIpAttribute' => 'updated_ip',
'value' => '127.0.0.1',
]
...
];
add a comment |
You can use the Yii2 default method available in YI2
Yii::$app->getRequest()->getUserIP()
or if you are looking to save the current user ip into the database table i would recommend you to use the IPbehavior
within the model like
public function behaviors()
return [
...
'ip' => [
'class' => IpBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip',
],
]
...
];
Or like this. where value can be a string or an anonymous function that will return a string.
public function behaviors()
return [
...
'ip' => [
'class' => IpBehavior::className(),
'createdIpAttribute' => 'created_ip',
'updatedIpAttribute' => 'updated_ip',
'value' => '127.0.0.1',
]
...
];
You can use the Yii2 default method available in YI2
Yii::$app->getRequest()->getUserIP()
or if you are looking to save the current user ip into the database table i would recommend you to use the IPbehavior
within the model like
public function behaviors()
return [
...
'ip' => [
'class' => IpBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip',
],
]
...
];
Or like this. where value can be a string or an anonymous function that will return a string.
public function behaviors()
return [
...
'ip' => [
'class' => IpBehavior::className(),
'createdIpAttribute' => 'created_ip',
'updatedIpAttribute' => 'updated_ip',
'value' => '127.0.0.1',
]
...
];
answered Nov 15 '18 at 19:54
Muhammad Omer AslamMuhammad Omer Aslam
13.5k72548
13.5k72548
add a comment |
add a comment |
I think to get client IP address you have to use this code:
Yii::$app->request->userIP
add a comment |
I think to get client IP address you have to use this code:
Yii::$app->request->userIP
add a comment |
I think to get client IP address you have to use this code:
Yii::$app->request->userIP
I think to get client IP address you have to use this code:
Yii::$app->request->userIP
answered Nov 15 '18 at 19:49
MehranMehran
1831412
1831412
add a comment |
add a comment |
The prefered way of getting client ip is with Yii::$app->request-remoteIp
This method uses either $_SERVER['REMOTE_ADDR']
or ip headers (like X-Forwarded-For) that can be added by load balencers or proxy services.
there are however still situations where your public ip (https://hidemyna.me/en/ip/) and the ip that your appication is showing (http://city.ru.xsph.ru/web/) will be different - if you're on the same network as your app for example
But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?
– rand_triest
Nov 15 '18 at 21:17
add a comment |
The prefered way of getting client ip is with Yii::$app->request-remoteIp
This method uses either $_SERVER['REMOTE_ADDR']
or ip headers (like X-Forwarded-For) that can be added by load balencers or proxy services.
there are however still situations where your public ip (https://hidemyna.me/en/ip/) and the ip that your appication is showing (http://city.ru.xsph.ru/web/) will be different - if you're on the same network as your app for example
But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?
– rand_triest
Nov 15 '18 at 21:17
add a comment |
The prefered way of getting client ip is with Yii::$app->request-remoteIp
This method uses either $_SERVER['REMOTE_ADDR']
or ip headers (like X-Forwarded-For) that can be added by load balencers or proxy services.
there are however still situations where your public ip (https://hidemyna.me/en/ip/) and the ip that your appication is showing (http://city.ru.xsph.ru/web/) will be different - if you're on the same network as your app for example
The prefered way of getting client ip is with Yii::$app->request-remoteIp
This method uses either $_SERVER['REMOTE_ADDR']
or ip headers (like X-Forwarded-For) that can be added by load balencers or proxy services.
there are however still situations where your public ip (https://hidemyna.me/en/ip/) and the ip that your appication is showing (http://city.ru.xsph.ru/web/) will be different - if you're on the same network as your app for example
edited Nov 16 '18 at 0:21
answered Nov 15 '18 at 19:56
csminbcsminb
1,88211318
1,88211318
But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?
– rand_triest
Nov 15 '18 at 21:17
add a comment |
But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?
– rand_triest
Nov 15 '18 at 21:17
But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?
– rand_triest
Nov 15 '18 at 21:17
But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?
– rand_triest
Nov 15 '18 at 21:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325630%2fyii2-get-correct-client-ip%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
do mark the correct answer that worked for you
– Muhammad Omer Aslam
Jan 30 at 12:52