How can I get client's IP address? - php

I am writing a web application with PHP and Javascript that uses Ratchet WebSockets for communication data between server and clients, but I need to get client's IP address.
In Ratchet WebSockets have a function "onOpen" that it give an object of "ConnectionInterface":
public function onOpen(ConnectionInterface $conn){
$this->clients->attach($conn);
echo "new Connection is connected...({$conn->resourceId})\n";
}
It only has a resource id. How can I get the IP address of this connection?

$conn->remoteAddress
Try this.

$conn variable contains following two fields:
$conn->resourceId and $conn->remoteAddress
Reference URL

Try this.
$ClientIP = $_SERVER['REMOTE_ADDR']; echo $ClientIP; echos the client's IP Address 98.1.xx.x You can find the reference on $_SERVER at http://php.net/manual/en/reserved.variables.server.php

Related

How can I know when my client login to my app from differnt device

I am working on how can I know my client change their device when they login to my app, I am using Laravel framework so I was used $ip = $request->ip(); to get an IP address, it's not working as I need, it's become when user login from the different IP address as you know when I connect/disconnect IP address change, so my question how can I detect user device I know there is no way to get a mac address of my client device.
login.php
public function login()
{
//..
auth()->user()->update(['trusted_device' => request()->ip()]);
//..
}
You can check USER-AGENT, try:
echo $_SERVER['HTTP_USER_AGENT'];
To see some more info try:
var_dump($_SERVER);

php Get IP from Server Mikrotik

i m sorry im newbie here,.. so i want to ask about, how to get the IP from the server,.
.
first i user PEAR2 to get an api from Mikrotik, then i want to get an IP for making a log, to log what an IP that Connect or Dissconnect from the network itself... the reason why i make the log from PEAR2, i want to make a notification android based, that if the client from the mikrotik's is disconnect or connect, it will send a notif to an android apps..
.
in those case PHP i use this script,
echo $server_ip = gethostbyname($_SERVER['SERVER_NAME']);
?> is this a correct..?
.
many thanks for you all. im sorry for My bad English
You want echo $server_ip = $_SERVER['SERVER_ADDR']
What you have gives you the host name.
Like this for the server ip:
If you mean getting the user's IP address, you can do something like :
<?php
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else{
$ip=$_SERVER['REMOTE_ADDR'];
}
?>
<?php echo "<br />".$ip;?>

How to use $_SERVER["REMOTE_ADDR"] in PHP websocket server

I have am using websocket and trying to merge with my custom PHP app and Mysql database. In Database I store IP, ClientID and username when user connects to server. These infromation are to be used when user disconnect from websocket server or sends message. I run server.php with php server.php
The server page is https://github.com/Flynsarmy/PHPWebSocket-Chat/blob/master/server.php
As mentioned above I stored necessary information in Mysql database to identify which users sends message, disconnect and connect.
So for that I need to identify my own mechine IP for further identification with various clients. So when I try to use
<?php
echo $_SERVER["REMOTE_ADDR"];
in server.php. It gives an error saying undefined.
You don't.
WebSockets does not deal with web requests. The $_SERVER superglobal does not get populated because it does not make sense to populate it.
Deal with the socket connections directly.
You most likely want to play around with socket_getsockname().
Please try this,
var_dump($_SERVER);
and check if it prints... [REMOTE_ADDR] => .......
But if you are after clients IP,
as a PHP developer I use following code
$ip=NULL;
if (!empty($_SERVER['HTTP_CLIENT_IP']))
$ip = $_SERVER['HTTP_CLIENT_IP'];
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
$ip = $_SERVER['REMOTE_ADDR'];

Get user ip address for geolock (not 192.168 address)

Im geo locking our program, we have a service that returns the country code for us. But if someone is behind a router, they have a local 192.168.x.x address, and this tells me nothing. Is there a way to find out their address at the router? I could do a trace, but then i need to program logic myself about which ip is useful and which are not. I'm doing this code fromt he server in php, and our client web app is in javascript. Please advise.
Thanks
function get_real_ip() {
if (!empty($_SERVER['HTTP_X_FORWARED_FOR']))
{
$client_ip = $_SERVER['HTTP_X_FORWARED_FOR'];
}
elseif (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$client_ip = $_SERVER['HTTP_CLIENT_IP'];
}
else
{
$client_ip = $_SERVER['REMOTE_ADDR'];
}
return $client_ip;
}
For server based software the PHP variable $_SERVER['REMOTE_ADDR'] will give the address of the client. For clients behind a router their address will be translated by the router firmware, so the server will see the router's public IP address, not the client's local private address.
All this is further confused by the possible presence of intervening proxy servers which may or may not tell you what their client address is. You can look at $_SERVER['HTTP_X_FORWRDED_FOR'] or $_SERVER['HTTP_CLIENT_IP'], if they're present. If not, you'll have to make do with the proxy address.

i want know system system localhost IP address in PHP?

here i am using $_SERVER['remote_addr'] i am getting the IP address INTERNET providers. please help me out.
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
It would seem that the question is looking for the server's IP address, and the problem is that the supplied code is returning the remote IP. If you want the IP address of the server, try $_SERVER['SERVER_ADDR'] . See $_SERVER for more details, or clearly define what you mean by "system IP".
You will not be able to get the client's local IP address from PHP like this, you will only ever get the
common gateway IP address
as you put it. This is because the request will appear to be coming from the router or whatever is between your client and the web server

Categories