I have this script in a .html file on my page and im trying to just run the getip.php file from here rather than to rename the file to .php. I tried this ajax call but its not working and not logging the ip in the logfile.txt. Please let me know what I am doing wrong.
Ajax in the head of the .html file:
<script>
$(document).ready(function() {
$.ajax({
type: "GET",
url: "getip.php",
}); // Ajax Call
}); //event handler
</script>
Code from getip.php:
<?php
// location of the text file that will log all the ip adresses
$file = 'logfile.txt';
// ip address of the visitor
$ipadress = $_SERVER['REMOTE_ADDR'];
// date of the visit that will be formated this way: 29/May/2011 12:20:03
$date = date('d/F/Y h:i:s');
// name of the page that was visited
$webpage = $_SERVER['SCRIPT_NAME'];
// visitor's browser information
$browser = $_SERVER['HTTP_USER_AGENT'];
// Opening the text file and writing the visitor's data
$fp = fopen($file, 'a');
fwrite($fp, $ipadress.' - ['.$date.'] '.$webpage.' '.$browser."
");
fclose($fp);
?>
Have you tried $.post()
function instead? Example:
<script>
$(document).ready(function() {
$.post('getip.php'); // Ajax Call
}); //event handler
</script>
You can also add a callback as a second parameter.
I would use the full URL for the ajax call. Depending on your framework you might not be hitting the intended endpoint. You could try echo 'test';
in your php file and adding a done handler to your ajax call to make sure you're hitting the php file in the first place:
.done(function( data ) {
alert(data);
});