简体   繁体   English

存储获取和存储IP地址

[英]Storing getting and storing IP address

I currently have a function that stores a query that has been entered into a form. 我目前有一个函数,用于存储已输入表单的查询。 I am trying to save the IP address of the user that submits the form along with the input. 我正在尝试保存与输入一起提交表单的用户的IP地址。

Currently this is the function: 当前这是函数:

 function userIP(){
    $ip = $_SERVER['REMOTE_ADDR'];     
    if($ip){
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

        $ipis = ip2long($ip);
        }
        return $ip;
    }
    return false;
}

 public function __construct( $data = array() ) {
     if( isset( $data['text'] ) ) $this->text = stripslashes( strip_tags( $data['text'] ) );
     if( isset( $data['ipis'] ) ) $this->ipis = stripslashes( strip_tags( $data['ipis'] ) );
 }

 public function storeFormValues( $params ) {
    $this->__construct( $params ); 
 }


     public function store() {
    $correct = false;
        try {
            $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
            $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            $sql = "INSERT INTO tbl(content, ip) VALUES(:text, :ipis)";

            $stmt = $con->prepare( $sql );
            $stmt->bindValue( "text", $this->text, PDO::PARAM_STR );
            $stmt->bindValue( "ipis", $this->ipis, PDO::PARAM_STR );
            $stmt->execute();
            return "Added";
        }catch( PDOException $e ) {
            return $e->getMessage();
        }
 }

} }

However it currently only stores the "text" that was input and just adds NULL to the database for IP. 但是,它当前仅存储输入的“文本”,并且仅将NULL添加到IP数据库。 The MYSQL for the IP row is set up as an unsigned INT 11. Any ideas on fixing the issue? IP行的MYSQL设置为无符号INT11。有关解决此问题的任何想法?

There is an error in your function returning the long value. 返回长值的函数出错。 You have placed the return statement in the elseif condition. 您已将return语句置于elseif条件中。 Also, you are returning the actual $ip value as is. 另外,您将按原样返回实际的$ip值。

function userIP(){
$ip = $_SERVER['REMOTE_ADDR'];   
if($ip){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    $ipis = ip2long($ip);
    return $ipis;
}
return false;
}

使用字符串将IP存储在数据库中

Another possible option: 另一个可能的选择:

function userIP(){
  $ip = $_SERVER['REMOTE_ADDR'];     
  if( !$ip )
    return false;
  if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
    $ip = $_SERVER['HTTP_CLIENT_IP'];
  if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  return sprintf( '%u' , ip2long( $ip ) );
}

After reading the PHP Manual on ip2long() I found something interesting about how it handles large integers on some systems. 阅读ip2long()PHP手册后,我发现了一些有趣的信息,说明它如何在某些系统上处理大整数。

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

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