php connecting to mysql on localhost, request from remote - javascript

I have a wamp server running on my local machine that is exposed to the outside world. I have several php scripts that allow an android app to access information contained in a mySQL database running on the same machine as the WAMP server. When tested from inside my local network everything works fine. However when I try to run the php scripts from outside my local network there appears to be an error connecting to mySQL.
connect.php
<?php
header('Access-Control-Allow-Origin: *');
/*** mysql hostname ***/
$hostname = '127.0.0.1';
/*** mysql username ***/
$username = 'chaosjr_user';
/*** mysql password ***/
$password = 'pass';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=chaos_jr", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
I'm almost certain it has something to do with the fact I'm referencing localhost from the outside world, but when I replace 127.0.0.1 with the IP address on my internal network or the external IP address it doesn't work from anywhere. Any suggestions?
EDIT:
Just to clarify, I can get to my php scripts from the outside (I do have a static IP address) and get a response from them as long as that particular script does't have to query MySQL. For example I can connect to XXX.XXX.XXX.XXX/myPHPscript.php just fine and everything looks good. But menus that are supposed to populate with SQL data are empty.

You are correct - the address 127.0.0.1 is your local IP address and not the one you would connect with from the outside world.
If you have a static IP address, you'll need to use that. You may find things tricky if you don't have a fixed IP address, or if you have a firewall that blocks the port you are using.

You should still use 127.0.0.1 to connect to the DB since the PHP script and the DB runs in the same server as far as I understood. It doesn't care if your script is called from the outside, if the script and the DB are in the same server, 127.0.0.1 is the correct address.

In your PHP code it should stay 127.0.0.1 because the PHP and the MySQL run on the same server, which is your localhost at that IP. But contacting it is a lot harder.
Okay, so your local host is running on your computer right, so this can be reached by contacting 127.0.0.1 from that same computer only.
If you try to contact it from another device, you have to look up the private IP address of the device that you want to contact. This can be something like 192.168.x.x in most cases.
Now contacting this computer from outside your network is even harder because you will connect to your router first (via its public IP), and that router should forward you to your pc on a certain port (probably port 80). You have to make sure that you configured your router so it can forward to the correct computer in your network and to the correct port on that computer.
But wait, there's more! Most ISPs (internet service providers) change your public IP address every few days for security reasons, making it even harder to maintain the right reference to this computer you're trying to reach.
You can find your WAN IP (public IP) by visiting http://www.whatismyip.com and check your local IP by executing "ifconfig" in the shell, or "ipconfig" in the windows command prompt

If you do want your database to be available from outside your network (please be aware that your machine will need to be powered on at all times in order for the database to be available), you will need to configure a so called port forward on your router to your internal address. Then in your app, make sure to use your external IP address, your router will then reroute the request to your machine.
Also be aware that your MySQL will need to accept external connections, by default it binds to your localhost and will not listen to remote connections. This is because localhost is bound to a special loopback interface of your OS and is not actually bound to the network card in your computer. You will need to bind MySQL to the IP address that is bound to the network card, so it can communicate to the outside world.
In other words, what you want is possible, but takes many different steps to work, mainly network/access related and how to configure this depends on your setup. So there's no ready-to-go answer for this, it going to take some trial and error. But basically it comes down to these steps:
Configure a port forward for your MySQL port (default: tcp 3306) on your router.
Make sure that your MySQL server is bound to your "external" (in most LAN's this is a 192.168.0.x address) IP address, so not to localhost or 127.0.0.1 (this is a setting in the my.ini configuration file).
Make sure your app attempts a database on your remote IP address (the one you got from your ISP, so not your 127.0.0.1 or 192.168.0.x address!).
For the last step, it's also important that your ISP has given you a static IP adress. Many ISP's use DHCP adresses, meaning every time your modem resets, your IP address can be different when it boots up again. You can verify this with your ISP. Always remember that hosting stuff in-house is usually not ideal, it's OK for micro-projects or startup projects, but consider off-site hosting if things get serious.

Related

client IP is different each page reload in heroku

I'm trying to get the client IP as a way to save a particular user so the server knows who they are next time they visit, without having the need to login/signup for anything. This is a React front end with a NodeJS backend.
I tried my app locally and it seems to work fine. But I tried deploying it to Heroku and now I'm getting different IP addresses each time I reload. It keeps the same IP for the duration of the visit, but once I reload (refresh) the page, my IP changes..
[Method: 'POST'] [Path: '/api/posts'] [IP '::ffff:***.63.***.219']
[Method: 'POST'] [Path: '/api/posts'] [IP '::ffff:***.47.***.144']
(actual ip modified)
this is my console, as you can see the IP is completely different, and it looks nothing like my IP. I'm getting the IP from the request object (request.ip).
Why is the IP different each time on Heroku but stable on my local machine? is there another method for getting the client IP that I should be using? or is this a Heroku problem? I've looked for answers about this but I have come up empty which makes me think this is specific to Heroku.
According to the Heroku Documentation, all requests are going through a Proxy which acts mainly as a load balancer (If I have it correct in mind). You can use the custom HTTP-Headers to get the client ip address, but it is not recommended!
Additional:
For security reasons you should avoid to use the IP to identify a user, because that can cause session hijacking. Use technologies like cookies instead!

How can we access Node server outside the network if the app is listening on 'localhost'?

Using Express when we use app.listen(port) , the app location is localhost:port/
In local machine I completely understand how we can access to this address as we use a local browser running on same machine . Even other clients running on same network can access the server.
As per my knowledge localhost or 127.0.0.1 IP can be accessed on same or other machines in same network.
But if we deploy to cloud like Heroku without adding IP option like app.listen(port, IP_ADDRESS) instead we use app.listen(port), the only thing that varies is PORT number(process.env.PORT) but IP is still localhost. So how can clients from other networks access the server?
You can use port forwarding on your router to forward router_ip:port to local_ip:port allowing you to access it externaly
Just because you are connecting to your local instance via localhost doesn't mean it is not also exposed via IP. Localhost basically says don't resolve any IP, just loop back to this computer, but your node server will still be deployed to an actual IP address. Try looking up your computer's IP address and connecting to your node server through that instead of localhost, and you'll find you're still able to communicate with the server.
When deploying to a cloud service, or any other hosting service, you'll be given an IP address associated with that instance which is what will be used for resolving. Heroku in particular will blackbox a lot of the domain-space and port-forwarding process for you.

Nodejs connection bug with ip redirection

I have a problem with NodeJS (I think) using a kafka node on a node-red instance installed on a RPI3.
Scenario:
I have a cluster with a running instance of Kafka. The real ip of the machine which host the kafka broker is private so I have a public ip with a public port which I can use. Then, the requests are redirected to the broker.
Testing my environment with a broker installed on my laptop (so knowing the real ip of the broker) everything works fine.
Performing an attempt on the real cluster node-red shows the problem described in the terminal:
So, I took a look in js file representing the kafkaNode I used and I find this:
I'm quite sure the problem is in these line and in the use of the ip redirection. Anyway, honestly I'm a newbie of Nodejs and javascript so I don't know if there are some bugs about use of it.
Any ideas?
P.S.: I'm sure that kafka broker is correctly running and installed. the problem is exactly in js.
I also tried to reach the "fake ip" with telnet, and it works fine.
Thanks in advance
Kafka does not work with default values configured if you use a NAT, VM, or other proxy for "ip redirection" because the clients dynamically discover the private IPs of the real kafka brokers and will try to connect directly to those IP addresses and not just the one you configure in your client for the initial connection and meta-data request.
You need to make sure that the broker is setup to advertise the hostname or IP of the machine doing the redirection or your producers will not work.

How can i know what is the host name of my database on my server

I was use my host name as local server when i run my php code on my laptop but now i register my website on web so what host name now i use for my sql connection i give my site ip address to as a host in sql but the site gives me an error "Host '31.170.163.50' is not allowed to connect to this MySQL
It looks like you're trying to connect to your database from a different server. Is your PHP script still running on your own PC?
The message you get is because you try to reach the database from the computer at address 31.170.163.50, which is not allowed. Quite often, when you have 'normal' simple, shared hosting, you are not allowed to use the database from other computers directly. Only the scripts on the server itself are allowed to use the database.
So, the solution would be: Put everything, scripts and database, on your web hosting platform, and use localhost or 127.0.0.1 when your PHP script needs to connect to your database.

Know local ip server from client in node.js

I have a Node.js server, the client is a mobile app. I want to know which would be the best way to know the local ip of the server. I mean...
Server runs in localhost:8080 and the client has to connect to the server like this 192.168.0.107:8080 (pc's local ip).
I want to guest this IP, 192.168.0.107 without writing it in client code because it could change.
My guess is that you have a server with a dynamic ip, you want to connect your client to this IP without hardcoding that to the client's code. I'd say have many options:
Set up DNS, so that you connect to a known hostname and then translate that into an IP with your DNS server.
Implement/use a discovery service using your network's broadcast address. (A server could regularly broadcast information to a broadcast IP, then your client will receive this information and use it to show the user a list of available servers)
Let the client have an input box where he can enter the IP address (this is the easiest way, just have a guy input the ip address, maybe save the address for future autocomplete etc)
IMHO this is not possible in a really reliable way. There are several reasons for this, including:
A server may have more than one IP, e.g. multiple network adapaters.
A server has an IPv4 and an IPv6 address.
…
So either you need to narrow down what you want to achieve or you have to live with some kind of uncertainness.
The easiest way then would be to do a DNS lookup, using the hostname, to get the well-known IP addresses, and then select one of them (however you want to do this, that's up to you).
PS: I'm not perfectly sure that I really got your question. If I understood you correctly, you want to find out the IP address of the client on the client, because you need to send it to the server, right?
If you are looking for a simple answer, there is a npm module that does this for you, it's really small so just install it and use the instructions to get the ip of the client:
https://www.npmjs.com/package/request-ip
Getting Started:
var requestIp = require('request-ip');
// inside middleware handler
var ipMiddleware = function(req, res, next) {
var clientIp = requestIp.getClientIp(req); // on localhost > 127.0.0.1
next();
};
Depending upon the network topology details of your actual implementation (which aren't included in your question), you have at least three different options:
Server Directly on Public Interent
On a public network (e.g the open internet), DNS is the normal answer to this type of question. You follow the following steps:
Make sure you have a public IP address. As you may or may not know 192.168.0.107 is not a public IP address. It is a local internet address and cannot be routed on the open, public internet.
With a DNS service, create a DNS record that establishes a link between a host name such as "google.com" and your public IP address.
Code the client to connect to your "name", not your IP address.
Server and Mobile App Only on Private Network
If your mobile app runs only on your private network (perhaps via WiFi), then you can configure your own DNS server on your private network that will do essentially the same thing as the steps above, but will work for a private address like 192.168.0.107 on your private network.
Server Lives on Home Network, Want to Access From Internet
If your server lives in your private network (and thus only has a private IP address), but you want to be able to reach it from the public network, then you need to configure some sort of routing or port forwarding at the boundary between your private network and your public network. In a home router situation, this would involve the following steps:
Get the public IP address of your home network connection.
Set up a public DNS entry on the open internet for that IP address.
If your public IP address is not a static IP address (e.g. it could change over time), then you may be able to use one of the "dynamic DNS" services that are built for this type of situation.
At your home router, configure port forwarding so that incoming requests from the internet on the port your web server is running on (usually port 80) or forwarded to the private IP address of your actual node.js server on your home network.
I got your question as this,
Your client have a private & dynamic IP because it is behind some NAT( i.e firewall),
mobile app can request some STUN server to get its public IP and Port, then mobile app can send these data to server to identify the client. Similar to peer-to-peer connection establishment.
Read more...

Categories

Resources