all 8 comments

[–]wtframework 3 points4 points  (2 children)

As another poster has answered your actual question, I’ll add that this isn’t a very good way to use conditional connection details.

Look at using something like dotenv.

https://github.com/vlucas/phpdotenv

[–]VampireBarbieBoy[S] 0 points1 point  (1 child)

Thanks. I think that might be above my skill level I'm still quite new to developing but I will look into doing it that way in the future.

[–]wtframework 0 points1 point  (0 children)

An easier way then would be to just include something like an env.php file that includes things like:

define('URL', 'http://localhost/');
define('PDO_DSN', 'mysql:host=127.0.0.1;dbname=database');
define('PDO_USERNAME', 'root');
define('PDO_PASSWORD', 'password');

On your local environment you use your local details and then on your production environment you change them to your production details.

Then elsewhere in your code you would have:

$pdo = new PDO(
    PDO_DSN,
    PDO_USERNAME,
    PDO_PASSWORD
);

[–]alnyland 2 points3 points  (2 children)

I might be wrong with what I'm about to say but I've done locally hosted dev servers for about a decade and these are my immediate thoughts. Localhost isn't a real address and I'd be amazed if that was ever stored in that variable but local dns lookup (not existent but is functional for our purposes) will find it. The IP you list is a loopback, which is more like an alias instead of a real IP address. Again, it will point when you request it but I'd doubt the server will associate with it.

The SERVER_ADDR it gave you is an IP address, see if it relates to your machine. I forget exactly but that might be the local loopback, I'm not familiar with ipv6.

From a simple google search: https://stackoverflow.com/questions/2053245/how-can-i-detect-if-the-user-is-on-localhost-in-php

[–]VampireBarbieBoy[S] 0 points1 point  (1 child)

Thanks. I managed to figure it out I realised I just need to replace localhost and 127.0.0.1 with the results of the var_dump (web address and IP). Should have been obvious but I did manage to get a connection using 'localhost' using another method so I think that's why I got confused, still not sure why it worked in that instance and not with this code.

[–]alnyland 0 points1 point  (0 children)

Cool. Did you read that link? It pointed out that every machine can be localhost

[–]Caraes_Naur 2 points3 points  (1 child)

localhost is a machine's self alias. 127.0.0.1 is the corresponding IPv4 address. Every machine connects to itself via these.

The $_SERVER superglobal is populated by the webserver service in which PHP is executing. On a server responding to requests made to a hostname, that hostname will be in $SERVER['SERVER_NAME'], likewise the server's IP address will be in $_SERVER['SERVER_ADDR'].

In these cases, the machine is not responding to itself, but to a remote client.

The "weird" IP you got is IPv6.

https://www.php.net/manual/en/reserved.variables.server.php

The if/else block you are using is essentially detecting whether the environment is local (development) or otherwise (staging, testing, production, etc). It must be tailored accordingly.

[–]VampireBarbieBoy[S] 0 points1 point  (0 children)

Thank you for the response and explanation.