简体   繁体   English

获取访问者IP地址的脚本不起作用

[英]Script for getting IP address of visitor is not working

I am manually storing the below line in mysql database using php myadmin. 我使用php myadmin手动将以下行存储在mysql数据库中。

<div>IP address : '<?php echo $_SERVER['REMOTE_ADDR']?>'</div>

It gets updated as I have confirmed it is in database. 当我确认它在数据库中时,它会更新。 My php page fetches this from database and updates the html. 我的php页面从数据库中获取此信息并更新html。

The output that I get is - 我得到的输出是-
IP address :

I am not an expert in php. 我不是php方面的专家。 Also I cannot write <?php echo $_SERVER['REMOTE_ADDR']?> directly into my php page. 我也不能直接将<?php echo $ _SERVER ['REMOTE_ADDR']?>直接写入我的php页面。 Please help and tell me where is wrong. 请帮助并告诉我哪里出了问题。

When you fetch the text from the DB and output it to the client, it will just be WRITTEN, not EXECUTED - so 当您从数据库中获取文本并将其输出到客户端时,它将只是被写入而不是被执行-因此

$_SERVER['REMOTE_ADDR']

will never be replaced by the real IP adsress. 永远不会被真正的IP广告商所取代。

A solution would be to store 一个解决方案是存储

<div>IP address : '%s'</div> 

in your DB, later fetch it and 在您的数据库中,稍后将其获取并

echo sprintf($txtfromdb,$_SERVER['REMOTE_ADDR'])

to your client. 给你的客户。

You shouldn't store the Variables name as such in the database. 您不应该这样在数据库中存储变量名称。 Because the content of them won't be saved, only its name. 因为不会保存它们的内容,所以仅保存其名称。

If you want the current clients IP to be shown you could (as for every php content to execute) eval it. 如果您希望显示当前的客户端IP,则可以(对于要执行的每个php内容)进行评估。

ob_start();
eval( $the_table_content );
$the_content = ob_get_clean();

Now your right content is stored in $the_content and can for example be output by echo $the_content 现在,您的正确内容存储在$the_content中,例如可以通过echo $the_content输出

But if you only want to output it, you don't need the above code and could try just eval( $the_table_content ) instead of echo $the_table_content 但是,如果只想输出它,则不需要上面的代码,可以尝试使用eval( $the_table_content )代替echo $the_table_content

A better solution is generally Eugen Riecks but in a few cases this is the fastest and easiest. Eugen Riecks通常是一个更好的解决方案,但是在某些情况下,这是最快,最简单的方法。

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

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