CodeIgniter Forums
determining the address of the server? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: determining the address of the server? (/showthread.php?tid=79955)

Pages: 1 2


determining the address of the server? - richb201 - 08-23-2021

The way my app has been designed is that each server has a copy of wordpress, copy of the application, and a copy of the fusionAuth server. When a user reaches my website, researchStudyOnline.com, the AWS load balancer determines which server will be handling this request by displaying my landing page. My landing page has a button: Login to Research Study Online. This will bring them to my app at a certain URL on that server. 

On my laptop "server" the address of the server is localhost. So to login they get sent to localhost/index.php/Login.
Bur how will this work on my AWS server? Will localhost still be the local server or will I use xx.xx.xx.xx/index.php/Login?


RE: determining the address of the server? - InsiteFX - 08-24-2021

On live servers localhost is usually used for MySQL servers so you would need to use the full url address.


RE: determining the address of the server? - richb201 - 08-24-2021

There is a problem. Each server (this is designed to handle a few) will have a different address. But the code running on all of them is the same. I'm seeking a CI function that returns the IP of the current server. Then I can use a var called server_address instead of the hard coded address.


RE: determining the address of the server? - John_Betong - 08-24-2021

@richb201,

Try experimenting with the following to find the Server parameters. I would also prevent rendering the details unless a certain $_GET value is verified to prevent others from seeing sensitive server information.
PHP Code:
// pre adds line-feeds for easier readability
  
echo '<pre>'print_r$_SERVER ); echo '</pre>';  



RE: determining the address of the server? - richb201 - 08-25-2021

Thx. I tried this:
$IP=$_SERVER[SERVER_ADDR];

$IP is 172.19.0.3

But when I try typing that URL in the browser (on my laptop), I get
This site can’t be reached


RE: determining the address of the server? - John_Betong - 08-25-2021

I think the SERVER_ADDR parameter applies to the Internet Service Provider.

Try the REQUEST_URI parameter.


RE: determining the address of the server? - richb201 - 08-25-2021

I tried $IP=$_SERVER[REQUEST_URI];

and this yields
/index.php/Configure/index


RE: determining the address of the server? - captain-sensible - 08-25-2021

on my local dev I have a web dev thats configured to be served on 127.0.0.2

if i put this code in a controller :

Code:
$IP2=  strval($_SERVER['SERVER_ADDR']);
    echo $IP2;

it correctly returns 127.0.0.2


RE: determining the address of the server? - John_Betong - 08-25-2021

@rich2b01,
I have a similar app that at the user point of entry tests for the environment URL, defines a constant then requires a common application. The common application uses the defined constant and sets the relevant MySQL database tables, paths, etc.


RE: determining the address of the server? - InsiteFX - 08-26-2021

He is on AWS and some people have reported problems trying to get the host address see below.

PHP Code:
// From PHP.net - gethostbyaddr()

if the user is sitting behind a proxy serveryou can do this;

if (
$HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"] != "") {
    $IP $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
    $proxy $HTTP_SERVER_VARS["REMOTE_ADDR"];
    $host = @gethostbyaddr($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]);
} else {
    $IP $HTTP_SERVER_VARS["REMOTE_ADDR"];
    $host = @gethostbyaddr($HTTP_SERVER_VARS["REMOTE_ADDR"]);



You should be careful in the use of $_SERVER['HTTP_X_FORWARDED_FOR'], as I discovered,
once using Amazon AWS's Elastic Load Balancer's, this value may be a comma separated list
of IP addresses and will thusly not compare as conceived in almost every example I have
seen posted by users in the comments.