i have tried to implement a counter for my site which was a free hosted one from 000a.biz using ajax.It has worked in my LOCALHOST But it doesnt worked in my server.


The html file is(main.htm):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Tcats Inc.</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script src="ajax.js"></script>
</head>

<body onload="ajaxFunction()">
<font color="#000000"  face="Arial, Helvetica, sans-serif">
<br><br><br><br><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbspYou are visitor number: 


<b><span id="count"></span></b>
<br><br>You are viewing tcats.co.cc.The ultimate site for techies, hi-fi geeks and crazy coders <br><br><br><br>that provides forums which are technical in nature where you can submit your queries which will be answered by our expert panel.<br><br><br><br>Plz support us with feedbacks which we are always happy to hear.<br><br><br><br>So what are you waiting for,happy tcating!
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<hr>
&copy;Tcats inc.All Rights Reserved. 
</font>

</body>
</html>

The javascript file(ajax.js):

var xmlhttp

function ajaxFunction()
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET","counter.php",true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
 
document.getElementById("count").innerHTML=xmlhttp.responseText;
  }
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

The php file(counter.php):

<?php

$file=fopen("counter.txt","r+");

$count= fgets($file); 

fclose($file);
$count++;
echo $count;
$file=fopen("counter.txt","r+");

fputs($file,$count);
fclose($file);
?>

I have also created a counter.txt file for saving the count.Some other ajax applications worked in the server but i dont know why its not working for this case.It is showing some error in the counter part while im accessing main.htm(the site is george.000a.biz/main.htm)
Somebody plz HELP...

What are you getting on main.html?

Vantrax,

Typical reason for difference in local/remote behaviour in something like this is the permissions on the counter.txt file.

Most FTP clients allow you to set a remote (*nix) file's permissions, eg. in Cute FTP right-click > Properties/CHMOD. It's probably 644 by default. 666 should do it.

Airshow

do the counter serverside
that javascript does not run on my pc, security does not let it
something as simple as

<dtd><html><head></head><body>bla bla bla
<?php $file = "../cgi-bin/count.cnt";
if(is_readable($file)) { 
$count= file_get_contents( $file ) + 1 ; 
$fp = fopen($file, "w+"); 
fputs ($fp, $count);
fclose($fp); 
Print "<span id='count' style='text-align:right;'> you are user number $count</span>";} ?>

the user sees only <span id='count' style='text-align:right;'> you are user number 16</span> and of course it still requires the count file to be chmod writeable

Bob makes a good point.

Unless this is an exercise to teach yourself AJAX there's seems to be no point in serving the page then going straight back to the server to get some data that could have been inserted in the page in the first place. It's an unnecessary complication.

(Plus what I said before about permissions)

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.