I am facing some problems here. Firstly, I have created a form, a date form. Prompting user to enter the date in. After which, using a function to read date and place the marker into the Google Map API. Within which, I was given a log file(report.txt), I would like to use the function to 'catch' the same date(in the log fil开发者_StackOverflowe) and then get the ip address in it, using web services to retrieve the lat & long, and place the marker in the Google Map API.
PS, I know it sounds complicated, but after all, I would just like to know how am I able to 'match' the date(function that I have created) with the date(In the log file) and then grep the IP address. Thanks!
Below is my code.
<script type="text/javascript">
var locations = [[0,0],[1,1]];
<?php
//opening the report.txt
$report = fopen('C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\report.txt', 'r') or exit("Unable to open report.txt");
//while not the end of the file
while(!feof($report))
{
//if the fgets($report) which is a line in the file matches the regular expression, the matched text will be stored into $matches, which is an array.
if (preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/", fgets($report), $matches))
{
//opening a URL now, joining the IP address with a part of the URL to call the web service, and reading it
$ipaddrloc = fopen('http://freegeoip.appspot.com/json/'.$matches[0], 'r');
//while not the end of the page
while(!feof($ipaddrloc))
{
$var = fgets($ipaddrloc); //a line of the page, what is returned is in JSON format
$lat = json_decode($var)->latitude; //the "latitude part" of the returned info
$long = json_decode($var)->longitude; //the "longitude part" of the returned info
print "locations.push([".$lat.",".$long."]);"; //push both the values into the array that you created
}
}
}
fclose($report); //close the report.txt
fclose($ipaddrloc); //close the URL
?>
</script>
<head>
//Read date value
function checkDate(Date)
{
var date = /^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/;
if (date.test(Date))
{
for (var i = 0; i < locations.length; i++) {
new google.maps.Marker({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
map: map,
title:"Specified Location"
});
}
}
else
{
alert("Invalid Date");
}
}
</head>
<body>
<form onsubmit ="return false;">
<input type="text" name="Date" placeholder="YYYY-MM-DD">
<input type="button" value="Search Date" onclick="checkDate(this.form.Date.value);">
</form>
</body>
精彩评论