CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge -> Tips & Solutions

 Hit Counter in your Default.asp With Manual Edit of CCS Form

Print topic Send  topic

Author Message
Pete
Posted: 10/10/2005, 11:20 AM

Hi,
Getting the hit counter run around and want you own custom hit counter that you control. Here is the ASP code to add to your home page with some notes.
In case you end up with other clients that need something like this here is the ASP code:

'Beginning of custom counter code
<%
'Dimension variables
Dim fsoObject 'File System Object
Dim tsObject 'Text Stream Object
Dim filObject 'File Object
Dim lngVisitorNumber 'Holds the visitor number
Dim intWriteDigitLoopCount 'Loop counter to display the graphical hit count
'Create a File System Object variable
Set fsoObject = Server.CreateObject("Scripting.FileSystemObject")
'Initialise a File Object with the path and name of text file to open
Set filObject = fsoObject.GetFile(Server.MapPath("hit_count.txt"))
'Open the visitor counter text file
Set tsObject = filObject.OpenAsTextStream
'Read in the visitor number from the visitor counter file
lngVisitorNumber = Clng(tsObject.ReadAll)
'Increment the visitor counter number by 1
lngVisitorNumber = lngVisitorNumber + 1
'Create a new visitor counter text file over writing the previous one
Set tsObject = fsoObject.CreateTextFile(Server.MapPath("hit_count.txt"))
'Write the new visitor number to the text file
tsObject.Write CStr(lngVisitorNumber)
'Reset server objects
Set fsoObject = Nothing
Set tsObject = Nothing
Set filObject = Nothing
'Loop to display graphical digits
For intWriteDigitLoopCount = 1 to Len(lngVisitorNumber)
'Display the graphical hit count
Response.Write("<img src=""counter_images/")
Response.Write(Mid(lngVisitorNumber, intWriteDigitLoopCount, 1) & ".gif""")
Response.Write("alt=""" & Mid(lngVisitorNumber, intWriteDigitLoopCount, 1) & """>")
Next
%>
'End of custom counter code

'This is the very end of the web page and the closing /body at the end
 <p align="center">
 <p align="center">
 <p align="center">
 </p>

</body>

</html>


Please note with the above code:
1. You need a folder to hold 10 seperate gif images of all the numbers 0 to 9. In my case these images are located under the root in "counter_images" and pointed to in the line above, Response.Write("<img src=""counter_images/")
2. As I found out, this worked on my home server, but I am using ASPNET account to login via IIS and I have ASPNET account setup as an administrator account, so I had no conflict over-writing the hit_count.txt file here. On my ISP hosting server I had to go into my domain via the admin control panel allowed me to set the permissions on hit_count.txt so the logged in account could overwrite it..
3. Just paste the above code into the bottom of any web page just prior to the ending of the body, ensure you got a hit_count.txt in the same folder as the page you want the counter to work on, and rename your page so it has an .asp extension.

What I really like about this very simple approach is now when I republish I am not running into the FrontPage issue of resetting my counter back to zero and then not being able to reset it to the correct number again. I have had quite a bit of frustration on hit counters on two servers and now I go this way all the time.

Pete,



Damian Hupfeld
Posted: 10/11/2005, 5:32 AM

create a table as follows:

counters
cid
name
value

for each page that you wish counters for create an entry eg home, products,
contact

add the code below as custom code substituting
$cname for the name above
server for the server name/address
username for the sql user
password for the sql user password
database for the sql database name.

This will only log the visitor once per session per page you are tracking -
you cant inflate your counters by hitting refresh.


//Custom Code @80-B6E9E578
// -------------------------
global $counter;
// Write your own code here.
//Change These Values for counter and Database
//Set counter
$cname = "home";

//open database
$conn = mysql_connect(server', 'username', 'password');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database');

//Don't Change anything below
//Connect to sessions
session_start();
//Determine if seesion exists
If (!isset($_SESSION[$cname])) {
//create the session
$_SESSION[$cname]="set";

//Query Database for current counter
$sql="SELECT * FROM counters WHERE name='".$cname."'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$count = $row['value'];
//Increment Counter
$count = $count + 1;

//Update Database
$sql="UPDATE counters SET value=".$count." WHERE name='".$cname."'";
$result = mysql_query($sql);
}

//End Custom Code



regards
Damian Hupfeld
http://www.nexthost.com.au/services.php





Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.