Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数据库中存储客户端ip_Php_Laravel 5 - Fatal编程技术网

Php 在数据库中存储客户端ip

Php 在数据库中存储客户端ip,php,laravel-5,Php,Laravel 5,我想在数据库中保存客户端的ip地址。以下是我执行此操作的代码: //app/Error.php 使用Illumb\Database\Elount\Model; 使用\Http\Request; 类错误扩展模型{ 受保护的$fillable=数组('code','user','error','client','call'); 公共静态函数日志($code=“unset”,$message,$call=“unset”){ $kyu=“foobar”; $uri=“unset”; $ip=“未设置”;

我想在数据库中保存客户端的ip地址。以下是我执行此操作的代码:

//app/Error.php
使用Illumb\Database\Elount\Model;
使用\Http\Request;
类错误扩展模型{
受保护的$fillable=数组('code','user','error','client','call');
公共静态函数日志($code=“unset”,$message,$call=“unset”){
$kyu=“foobar”;
$uri=“unset”;
$ip=“未设置”;
$agent=“unset”;
如果(isset($\u服务器)){
如果(数组\密钥\存在('请求\ URI',$\服务器)){
$uri=$\u服务器['REQUEST\u uri'];
}
如果(数组\密钥\存在('远程\地址',$\服务器)){
$ip=$\u服务器['REMOTE\u ADDR'];
}
如果(数组\密钥\存在('HTTP\用户\代理',$\服务器)){
$agent=$\u服务器['HTTP\u用户\u代理'];
}
}
$callOnUri=$call.'on:'。$uri;
//创建数据库条目
$user=错误::创建([
“代码”=>$code,
“用户”=>$kyu,
'ip'=>$ip,//此字段在迁移中可为空
“客户端”=>$agent,
“错误”=>$message,
'call'=>$callOnUri//从请求中获取调用
]);
}
}
调用此代码时,$client和$uri被正确保存,但$ip始终为:NULL。 我使用以下代码在浏览器上进行了检查,根据相同的请求,在浏览器上显示了IP,但在数据库中仍然为空

公共函数后寄存器()
{
$validator=$this->register->validator(\Input::all());
$returnVal=$this->register->create(\Input::all());
if(is_bool($returnVal)){
//福巴
}否则{
错误::日志('1234',“注册过程中失败。”,_函数);
return response()->json([“Error”=>$returnVal],“server”=>$\u server);//转储$\u服务器
}
}

如何将客户端(远程地址)的ip正确保存在数据库中?

您需要将ip添加到$filleble

protected $fillable = array( 'code', 'user', 'error', 'ip', 'client', 'call');

签出第二个答案-还有,你用什么字段类型来保存它?它需要是varchar,而不是int,因为您正在将
$ip
变量设置为
“unset”
,并且当您从数据库检索行时,会得到
NULL
,在我看来,您的数据类型有问题。就像上面提到的@Styphon一样,我建议您检查存储IP的列的类型。“IP”字段至少是一个varchar(15)吗?您可以尝试将$ip附加到$message,以检查它是否实际获得了ip地址……我总是喜欢看到熟练的程序员能够在一堆程序垃圾中转换面向对象的框架。您没有使
ip
可填充。另一个“问题”是,您访问超全局文件时没有正当理由。每个
请求
对象都包含一个IP地址。所有这些都可以用两行代码编写。。