laravel 4将IP地址保存到模型 [英] laravel 4 saving ip address to model

查看:23
本文介绍了laravel 4将IP地址保存到模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Laravel 4 将用户 IP 地址保存到我的数据库中.我找到了以下返回字符串的函数

I'm trying to save a user ip address to my database using Laravel 4. I found the following function which returns a string

Request::getClientIp()

如何将它存储在我的模型中?只是一个字符串还是有更有效的方法?

How would I store this in my model? Just a string or is there a more efficient way?

$table->string('ip_address');

推荐答案

选项 1:使用 VARCHAR(45) 列

考虑另一个 SO 问题中的讨论 最大长度IPv6 地址的文本表示形式?包含 IPv4 隧道功能时,IPv6 的最大长度为 45.

Considering the discussion in another SO question Maximum length of the textual representation of an IPv6 address?, the maximum length of IPv6 is 45 when including the IPv4 tunneling feature.

因此,更安全的迁移命令是:

Thus, a safer migration command would be:

$table->string('ip_address', 45);

优点:

  1. 该列是人类可读的.设置值或查询行查看时无需转换.

缺点:

  1. 它比选项 2 使用更多空间,实际上几乎是 3 倍.但除非您计划它有数百万行,否则我不会太担心.

<小时>

选项 2:使用 BLOB 列

@euantorano 提供了 IP 地址存储在 mysql 数据库中的链接,您可以将 IP 存储为二进制以节省一些空间.

As @euantorano provided the link to IP address storing in mysql database, you may store the IP as binary to save some space.

最简单的答案是使用:

$table->binary('ip_address');

优点:

  1. 以二进制形式存储 IP 地址将为您节省一些空间.

缺点:

  1. 您需要先使用 PHP 的 inet_pton().该列将无法直接读取,因为它以二进制格式存储.如果尝试直接查询,您将看到奇怪的字符或空白.您可能想看看我在下面的选项 3 中存储和检索 IP 地址的方法.

  1. You will need to convert the IP address string to binary first using something like PHP's inet_pton(). The column will not be directly readable since it is stored in binary format. You will see weird characters or blank if tried to query it out directly. You may want to look at my way to store and retrieve the IP address in Option 3 below.

尽管方法被称为二进制,Laravel 中的查询构建器实际上会创建一个BLOB 列 为您服务.BLOB 存储在表格之外, 超出行缓冲区可能意味着性能较低.而且确实没有理由不使用 BINARY 列类型,因为我们知道 IP 地址对于 BLOB 来说并不长.

The query builder in Laravel, despite the method being called binary, will actually create a BLOB column for you. BLOB is stored off the table, out of the row buffer, which possibly means a lower performance. And there really isn't a reason not to use BINARY column type since we know IP addresses aren't that long for BLOB to be necessary.

<小时>

选项 3:使用 VARBINARY(16) 列

Laravel 的查询构建器为选项 2 中的示例生成一个 BLOB 列.如果您使用的是 MySQL,您将希望使用 VARBINARY(16) 而不是 BLOB 以获得更好的性能.

Laravel's query builder produces a BLOB column for the example in Option 2. If you are using MySQL, you will want to use VARBINARY(16) instead of BLOB for better performance.

迁移脚本:

class CreateMyLogsTable extends Migration {

    public function up()
    {
        Schema::create('my_logs', function(Blueprint $table) {
            $table->increments('id');
        });

        DB::statement('ALTER TABLE `my_logs` ADD `ip_address` VARBINARY(16)');
    }

    public function down()
    {
        DB::statement('ALTER TABLE `my_logs` DROP COLUMN `ip_address`');

        Schema::drop('my_logs');
    }
}

显然,上面唯一重要的部分是 DB::statement(...).我们需要按照 Taylor Otwell 的建议使用原始查询.随意按照自己的方式创建表格的其余部分.

Obviously the only important part above is the DB::statement(...). We need to use raw queries as Taylor Otwell suggested. Feel free to create the rest of the table your way.

从这里你可以使用 PHP 的 inet_pton()inet_ntop() 将 IP 地址字符串转换为二进制,反之亦然反之亦然.

From here you can use PHP's inet_pton() and inet_ntop() to convert the IP address strings to binary and vice versa.

优点:

  1. 与选项 1 相比节省空间
  2. 与选项 2 相比,数据库性能更好

缺点:

  1. 与选项 2 一样,您需要手动在二进制字符串和人类可读字符串之间来回转换,或者使用 Eloquent 模型和一对自定义访问器/修改器,我将在下面演示.

<小时>

额外功劳:添加自定义 Eloquent 访问器/修改器(可选):

这是我发现 Eloquent 真正有用的地方.您可以为 Eloquent 模型设置自己的访问器/修改器,并且可以像往常一样通过模型的实例变量获取/设置.

Here is where I find Eloquent really useful. You can set your own accessor/mutator to your Eloquent model and you can get/set via your model's instance variable as usual.

class MyLog extends Eloquent {

    public $timestamps = false;

    public function getIpAddressAttribute($value)
    {
        return inet_ntop($value);
    }

    public function setIpAddressAttribute($value)
    {
        $this->attributes['ip_address'] = inet_pton($value);
    }
}

现在如果你这样做:

$log = new MyLog;
$log->ip_address = '192.168.0.1';
$log->save();

IP 地址将正确保存为二进制.你可以这样做:

The IP address will be saved as binary correctly. And you can do:

$log = MyLog::find(1);
echo $log->ip_address;

它会回显 192.168.0.1.很有用!

And it will echo out 192.168.0.1. Very useful!

这篇关于laravel 4将IP地址保存到模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆