简体   繁体   English

使用JavaScript将IP数据发送到服务器

[英]Sending IP Data to Server in Javascript

So I am making a web page, and one of the functions I want is to be able to store global IPs into a text file. 因此,我正在制作一个网页,而我想要的功能之一就是能够将全局IP存储到文本文件中。 I am a NOOB as far as this web development is concerned (Hardware dude). 就此Web开发而言,我是NOOB(硬件老兄)。

So far I have on the client side- 到目前为止,我在客户端上

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>IP Grab</title> <meta name="description" content="A privileged app stub"> <meta name="viewport" content="width=device-width"> <link rel="stylesheet" href="css/app.css"> <script type="text/javascript" src="js/app.js" defer></script> <link rel="prefetch" type="application/l10n" href="data/locales.ini" /> <script type="text/javascript" src="js/libs/l10n.js" defer></script> </head> <body> <!-- Find Global IP Address --> <script type="application/javascript"> function getIP(json) { document.write("My public IP address is: ", json.ip); } </script> <script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script> <!-- Send Data to Server --> <script> jQuery.ajax({ method: "POST", url: "get.php", data: json.ip }) </script> </body> </html> 

On the server side- 在服务器端

 <?php data = $_POST['text']; list($type, $data) = explode(';', $data); list(, $data) = explode(',', $data); $data = base64_decode($data); file_put_contents('ips.txt', $data); > 

I KNOW the php portion is VERY wrong... can someone point me in the right direction? 我知道php部分非常错误...有人可以指出我正确的方向吗? I'm having a heard time finding a good tut. 我正在听一段很有趣的话。

Thanks 谢谢

Getting the client ip is easy in PHP . 在PHP中,获取客户端ip很容易。

$your_ip = $_SERVER['REMOTE_ADDR'];  

for more info you can visit PHP's official website http://php.net/manual/en/reserved.variables.server.php 有关更多信息,您可以访问PHP的官方网站http://php.net/manual/zh/reserved.variables.server.php

on a side note , you should read about http request and response cycle . 另外,您应该阅读有关http请求和响应周期的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM