PHP 与C++检索用户IP
我有一个 C++ 控制台应用程序,它使用 wininet.h 访问 URL,并下载网页的内容。
内容通常只是一个 IP 地址。它在这里: http://www.whatismyip.com/automation/n09230945.asp
一切都很好。
然后我决定用 PHP 创建自己的 IP 检查器,使用以下代码:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
header("Cache-Control: private");
header("Content-Type: text/html");
echo $ip;
?>
这在浏览器中看起来是正确的,与 Whatismyip.com 的结果相同,但 C++ 程序只是在 IP 后面添加一堆垃圾,然后重复 IP 一半被切断,然后添加更多垃圾。
是什么原因造成的?我尝试分析标题,但无法发现差异。
另外,我尝试将一个纯 txt 文件放到服务器上,C++ 程序可以完美读取它。
我还尝试将标题更改为纯文本/文本和文本/纯文本。相同的结果。
感谢您的帮助!
编辑:这是 C++ 代码的一部分:
HINTERNET OpenAddress = InternetOpenUrl(connect,"http://www...", NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION, 0);
char DataReceived[16] = " ";
DWORD NumberOfBytesRead = 0;
while (InternetReadFile(OpenAddress, DataReceived, 16, &NumberOfBytesRead) && NumberOfBytesRead)
{
cout << DataReceived;
}
I have a C++ console app that uses wininet.h to go out to a URL, and download the contents of a web page.
The contents are usually just a single IP address. It goes here: http://www.whatismyip.com/automation/n09230945.asp
Everything works great.
Then I decided to create my own IP checker in PHP, using the following code:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
header("Cache-Control: private");
header("Content-Type: text/html");
echo $ip;
?>
This looks correct in the browser, identical whatismyip.com's results, but the C++ program just adds bunch of junk after the IP, then repeats the IP half cut off, and then adds more junk.
What is causing this? I tried analyzing the headers, but I can't spot the difference.
Also, I tried putting a plain txt file on to the server, and the C++ program reads it perfect.
I also tried changing my headers to both plain/text and text/plain. Same result.
Thank you for your help!
Edit: Here's a portion of the C++ code:
HINTERNET OpenAddress = InternetOpenUrl(connect,"http://www...", NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION, 0);
char DataReceived[16] = " ";
DWORD NumberOfBytesRead = 0;
while (InternetReadFile(OpenAddress, DataReceived, 16, &NumberOfBytesRead) && NumberOfBytesRead)
{
cout << DataReceived;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
cout
将期望字符串以 null 结尾。因为您只是将字节读入缓冲区,而不是在最后以空终止它们,所以cout
将继续执行您已读取的字节末尾,并继续转储内存,直到它遇到空指针或内存保护启动。根据您的代码,猜测发生的情况是这样的:
cout
。这是一个 char 数组,因此cout
期望它是一个以 null 结尾的字符串。它输出从缓冲区开头到“127.0.01”之后的每个字符,直到在内存中找到 0。对
InternetReadFile()
一无所知,但我猜想,如果您只抓取其中包含 IP 地址的单行,这样的方法应该可行:但从根本上来说,我认为你遇到的主要问题是混淆了一个只是一堆字节的缓冲区和一个友好的空终止字符串,你应该理解这一点,并且也许寻找一些使用 InternetReadFile 的现有示例,看看它们是如何使用的工作,以及你需要做什么。
cout
will expect a string to be null-terminated. Because you're just reading bytes into a buffer, and not null-terminating them at the end,cout
will carry on past the end of the bytes you've read and just carry on dumping memory out until it hits a null pointer or memory protection kicks in.With your code, what's happening is this, at a guess:
cout
with DataReceived. This is a char array, andcout
therefore expects it to be a null-terminated string. It outputs every character from the start of the buffer, right past "127.0.01" and onwards until it finds a 0 in memory.Don't know anything about
InternetReadFile()
, but I'd guess an approach like this should work if you're only grabbing a single line with an IP address in it:But fundamentally, I think the main problem you're having is in confusing a buffer that's just a bunch of bytes with a nice friendly null-terminated string, and you should understand that, and maybe look for some existing examples of using InternetReadFile with that in mind to see how they work, and therefore what you need to do.