1

I want to get current IP address from OS. Required to remove dependancy of hardcoding url path in 'scripUrl' for 'UrlManager' component in console.php so that cron controller can send emails with proper hyperlinks

Rahul
  • 13
  • 2
  • There is no reliable way to do this. Host could have multiple interfaces with multiple IPs and multiple domains attached to it. Such shortcuts will give you more trouble than introducing some local config files and configuring this for each host individually. – rob006 Apr 23 '18 at 14:36

1 Answers1

0

The yii Request component is unavailable in console-app

You'll need to rely on php built in functions

From CLI

PHP < 5.3.0

$myIp= getHostByName(php_uname('n'));
echo $myIp;

PHP >= 5.3.0

$myIp = getHostByName(getHostName());
echo $myIp;

see this answer

csminb
  • 2,382
  • 1
  • 16
  • 27
  • how can i get the port on which apache server is running, any function in php to get the port number?? – Rahul Apr 23 '18 at 12:33
  • you can't do that with php alone. this is dependent on the setup of your system, environment, services you're using and what not, on most linux machines you can parse trough the output of `system('lsof -'i)` and find what port apache is using – csminb Apr 23 '18 at 12:49