Mohammad
Mohammad

Reputation: 100

PHP Fatal error when I access my localhost through my Network IP

I have apache/mysql installed on my OSX ElCapitan. and I enabled virtual host in httpd.config file since I have so many projects on my systme and I want to access any of them at any time.

I my httpd-vhost.config file, I configured the first site as follows:

<VirtualHost *:80>
DocumentRoot "Users/me/Sites/test"
servername test.local
serveralias 192.168.1.20
.....

</VirtualHost>

and of course a record in /etc/hosts to redirect test.local to 127.0.0.1

Now, everything works fine when I put test.local in safari. But when I put 192.168.1.20, I get

PHP Fatal error:  Call to a member function prepare() on a non-object

I have no idea what is wrong.

any help would be appreciated

thanks

Upvotes: 0

Views: 112

Answers (3)

Mohammad
Mohammad

Reputation: 100

Thanks all of you for the help

the problem was with the credentials to connect to the database. In my config file I have:

if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') { // test envirnonmet
    define("DB_USER", "x");
    define("DB_PASS", "x");
    define("DB_NAME", "y");
} else { // production site
    define("DB_USER", "p");
    define("DB_PASS","w");
    define("DB_NAME", "xyz");
}

The first line I changed it to:

if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1' || strstr($_SERVER['REMOTE_ADDR'],"192.168") ) {

And now both are working.

Upvotes: 0

phreakv6
phreakv6

Reputation: 2165

I think it is a mysql DB privileges issue. user@localhost may have access to the DB but not [email protected]. Try GRANTing access to [email protected]

Upvotes: 1

Daniel
Daniel

Reputation: 8657

There can be some reasons:

  • path in document root looks bad. Try use absolute path, for example "/home/users/me/Sites/test"

  • If on 127.0.0.1 it works ok, but on 192.168.1.20 not, so there can be enabled restrictions on connection from different IP.

Remember about apache reloading and cache clearing in website and browser. Always you can debug by vardump on argument of prepare function in both cases.

Upvotes: 1

Related Questions