我的代码在Laravel 5.1上运行得很好
我正在尝试从5.1
升级。到5.7
编译器似乎在这里抱怨此行:
使用App \ Visitor,App \ CURLexec;
meta.blade.php
<meta charset="utf-8">
@if( App::environment('local') )
<title>local</title>
@else
<title>Author</title>
@endif
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Author">
<meta name="csrf-token" value="{{ csrf_token() }}">
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<!-- HTML5 shiv and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="js/plugins/html5shiv.js"></script>
<script src="js/plugins/respond.min.js"></script>
<![endif]-->
<?php
use App\Visitor, App\CURLexec;
if (!function_exists('getallheaders'))
{
function getallheaders()
{
$headers = '';
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
$ip = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com")); Debug::console($ip);
$ip = $_SERVER['REMOTE_ADDR'];
$info = shell_exec('curl ipinfo.io/'.$ip );
$ip_info_array = json_decode($info,true);
$header_info = getallheaders();
$loc = isset($ip_info_array['loc'])? $ip_info_array['loc'] : '';
$url = 'https://maps.google.com/maps/api/geocode/json?latlng='. $loc .'&sensor=false';
$geocode_array = CURLexec::get($url);
$formatted_address = isset($geocode_array['results']['0']['formatted_address'])? $geocode_array['results']['0']['formatted_address'] : '';
$place_id = isset($geocode_array['results']['0']['place_id'])? $geocode_array['results']['0']['place_id'] :'';
$formatted_address = isset($geocode_array['results']['0']['formatted_address'])? $geocode_array['results']['0']['formatted_address'] : '';
$place_id = isset($geocode_array['results']['0']['place_id'])? $geocode_array['results']['0']['place_id'] : '';
//Mac Address + Inet
$env = Request::server ("HTTP_HOST"); //localhost:8888
if ($env == 'localhost:8888'){
//Local
$inet = trim(shell_exec("ifconfig en0 | grep 'netmask' | awk '{print $2}'"),"\n");
$grab_bssid_command = 'arp '.$inet.' | awk \'{print $4}\'';
$bssid = trim(shell_exec($grab_bssid_command), "\n");
}else{
//Other Machine/Server
$inet = $ip;
$grab_bssid_command = 'arp '.$inet.' | awk \'{print $4}\'';
// dd($grab_bssid_command);
$bssid = trim(shell_exec($grab_bssid_command), "\n");
}
// LatLong Map http://maps.google.com/?q=-27.0000,133.0000
// Static Map = http://maps.google.com/maps/api/staticmap?center=40.7421,-74.0018&zoom=13&size=500x300
$static_map_url = 'https://maps.google.com/maps/api/staticmap?center='. $loc.'&zoom=13&size=500x300&maptype=roadmap&scale=2
&markers=size:mid|color:red|'. $loc .'|size:small';
$map_url = 'https://maps.google.com/?q='. $loc;
$ip = isset($ip_info_array['ip'])? $ip_info_array['ip'] : '';
$host_name = isset($ip_info_array['hostname'])? $ip_info_array['hostname'] : '';
$city = isset($ip_info_array['city'])? $ip_info_array['city'] : '';
$region = isset($ip_info_array['region'])? $ip_info_array['region'] : '';
$country = isset($ip_info_array['country'])? $ip_info_array['country'] : '';
$org = isset($ip_info_array['org'])? $ip_info_array['org'] : '';
$postal = isset($ip_info_array['postal'])? $ip_info_array['postal'] : '';
//User Agent
$user_agent_info = isset($header_info['User-Agent'])? $header_info['User-Agent'] : '';
$user_agent_array = ( explode(";",$user_agent_info));
//OS
$os_array = explode(")",$user_agent_array[1]);
$os = str_replace('_','.',$os_array[0]); //
$os_version = preg_replace("/[^\d.]/", '', $os); //10.10.5
if(array_key_exists(1,$os_array)){
if($os_array[1] !== ''){
$webkit_array = explode(" ",$os_array[1]);
}else{
$webkit_array = '';
}
}else{
$webkit_array = '';
}
if($webkit_array == ''){
$web_kit = '';
}else{
$web_kit = $webkit_array[1];
}
/*================================
= Browsers =
================================*/
if(array_key_exists(2,$os_array)){
if($os_array[2] !== ''){
$browsers = explode(")",$os_array[2]);
}else{
$browsers = '';
}
}else{
$browsers = '';
}
if($browsers == ''){
$browser_array = '';
$chrome = '';
$safari = '';
}else{
$browser_array = explode(" ",$browsers[0]);
$chrome = $browser_array[1];
if(array_key_exists(2,$browser_array)){
$safari = $browser_array[2]; //bug
}else{
$safari = '';
}
}
//Agent
$agent_os_array = explode("(",$user_agent_array[0]);
$agent = $agent_os_array[0]; // Mozilla/5.0
$device = $agent_os_array[1]; // Macintosh
// Check for existing
$validator = Visitor::validator($ip);
if ($validator->fails()) {
$ip = Visitor::where('ip', '=', $ip)->firstOrFail();
$id = $ip['attributes']['id'];
if($ip){
$visitor = Visitor::findOrFail($id);
$visitor->visit_count = $visitor->visit_count + 1 ;
$visitor->save();
}
} else {
$visitor = new Visitor;
$visitor->ip = $ip;
$visitor->host_name = $host_name;
$visitor->city = $city;
$visitor->region = $region;
$visitor->country = $country;
$visitor->loc = $loc;
$visitor->org = $org;
$visitor->postal = $postal;
$visitor->static_map_url = $static_map_url ;
$visitor->map_url = $map_url ;
$visitor->address = $formatted_address ;
$visitor->details = $place_id;
$visitor->user_agent = $agent;
$visitor->visit_count = $visitor->visit_count + 1 ;
$visitor->device = $device;
$visitor->os = $os;
$visitor->os_version = $os_version;
$visitor->browser_type = $agent;
$visitor->chrome = $chrome;
$visitor->safari = $safari;
$visitor->web_kit = $web_kit;
$visitor->inet = $inet;
$visitor->bssid = $bssid;
$visitor->save();
}
//dd($visitor);
Debug::console($loc);
if(
$visitor->country == 'US' AND
$visitor->ip != '76.119.248.198'
// $visitor->ip != '72.22.171.98'
// AND substr($visitor->host_name) != 'crawl-'
// AND substr($visitor->host_name) != 'spider'
){
//Send Email to me
$mail = Mail::send('layouts.share.emails.site_visit',
array(
'ip_info_array' => $ip_info_array,
'header_info' => $header_info,
'formatted_address' => $formatted_address,
'static_map_url' => $static_map_url
) ,
function ($ip_info_array) {
$ip_info_array->from(env('MAIL_USERNAME') , 'bheng gmail');
$ip_info_array->to(env('MAIL_TO') , 'User\'s Site ')
->subject( ' Visit Alert : ' . Request::server ("HTTP_HOST"));
});
//Send Text Message to me, use the forward from the mail client, so we don't put weigh on the application index
}
?>
我不断得到
如何进一步调试呢?
我肯定知道我有Visitor
模型,也肯定知道我的数据库中visitors
表上有记录。
答案 0 :(得分:1)
首先,将整个程序放入控制器中。不混合视图和控制器。因此,您可以将函数,方法以及所有计算内容提取到控制器中。
但这不是解决方案。这只是增加代码可读性的建议。
由于要升级laravel版本,因此应查找更改日志。也许某些功能已被描述或已删除。
最后,您应该依次减少代码以找出错误发生的位置。最初删除除静态HTML之外的所有内容。例如通过dd()
或die()
。然后将此功能(dd
或die
)进一步移到代码下方。
如果错误仍然存在,您应该查看use ...;
在.blade.php
中是否合法。如果是这样,则它应与标准库一起使用。因此,请选择Laravel本身提供的东西。除了您自己的库外,什么都没有。之后,最好查看您的一个库中是否没有任何错误。仔细阅读错误消息,直到最后。充其量可以将其复制并张贴到您的问题中。
另外不要在一个句子中导入App\Visitor
和App\CURLexec
。将它们分为两个导入:
use App\Visitor;
use App\CURLexec;