Gå til innhold

Hvor får jeg ordnet meg en teller til min hjemmeside?


Anbefalte innlegg

Videoannonse
Annonse

Finner mange "ferdig" løsninger ute på nettet som er rimlig enkel og bruke.

 

Jeg vet ikke hvor dreven du er på dette men la ut et par guider jeg kom over.

 

Guide for å lage en HitCounter i ASP (engelsk)

 

This tutorial will show you how to build a simple hit counter. It does not use any sql or databases and stores the hits in a text file.

 

Al you need to create for this script is your asp file and a text file. In the text file, simply enter the number 0 and save it in the same directory as count.txt. Take a look at the basic source code.

 

<%@ Language="VBScript" %>

<% Response.Expires= -1

Response.AddHeader "Cache-Control", "no-cache"

Response.AddHeader "Pragma", "no-cache" %>

<%

if Session("ct") = "" then

fp = Server.MapPath("db\count.txt")

Set fs = CreateObject("Scripting.FileSystemObject")

Set a = fs.OpenTextFile(fp)

ct = Clng(a.ReadLine)

ct = ct + 1

Session("ct") = ct

a.close

Set a = fs.CreateTextFile(fp, True)

a.WriteLine(ct)

a.Close

Set a = Nothing

Set fs = Nothing

else

ct = Clng(Session("ct"))

end if

%>

 

Now lets break it down into 3 sections.

 

<%@ Language="VBScript" %>

 

This just states that the page is a VB script page.

 

<% Response.Expires= -1

Response.AddHeader "Cache-Control", "no-cache"

Response.AddHeader "Pragma", "no-cache" %>

 

This section stops the user refreshing the page to clock up hits.

 

<%

if Session("ct") = "" then

fp = Server.MapPath("count.txt")

Set fs = CreateObject("Scripting.FileSystemObject")

Set a = fs.OpenTextFile(fp)

ct = Clng(a.ReadLine)

ct = ct + 1

Session("ct") = ct

a.close

Set a = fs.CreateTextFile(fp, True)

a.WriteLine(ct)

a.Close

Set a = Nothing

Set fs = Nothing

else

ct = Clng(Session("ct"))

end if

%>

 

This is the main section which adds the hits.

 

fp = Server.MapPath("count.txt")

 

This tells the server where to find the file. You can modify the file location by changing count.txt. So for instance if you wanted to to be called hitcounter.txt and in the directory db you would use:

 

fp = Server.MapPath("db\hitcounter.txt")

 

All you have to do is alter the file path in the quote marks.

 

Set fs = CreateObject("Scripting.FileSystemObject")

Set a = fs.OpenTextFile(fp)

ct = Clng(a.ReadLine)

 

This section opens the file using FileSystemObject and reads the first line. It then sets the varialbe ct to the amount of hits it has already had.

 

ct = ct + 1

 

This line adds one hit to the total number of visitors.

 

Session("ct") = ct

a.close

 

This bit saves a session variable as the new click through with the new amount of visitors and closes the text file.

 

Set a = fs.CreateTextFile(fp, True)

a.WriteLine(ct)

a.Close

 

This code creates a new textfile over the old one and adds in the new amount of visitors to it. Then it closes the text file.

 

Now you have a working hit counter. All you need to do is add the hit counter into your page:

 

You are a visitor number <%=ct%>!

 

This would display the amount of visitors. Now to save confusion, here is the full source code for the page:

 

<%@ Language="VBScript" %>

<% Response.Expires= -1

Response.AddHeader "Cache-Control", "no-cache"

Response.AddHeader "Pragma", "no-cache" %>

<%

if Session("ct") = "" then

fp = Server.MapPath("db\count.txt")

Set fs = CreateObject("Scripting.FileSystemObject")

Set a = fs.OpenTextFile(fp)

ct = Clng(a.ReadLine)

ct = ct + 1

Session("ct") = ct

a.close

Set a = fs.CreateTextFile(fp, True)

a.WriteLine(ct)

a.Close

Set a = Nothing

Set fs = Nothing

else

ct = Clng(Session("ct"))

end if

%>

<HTML>

<BODY>

You are a visitor number <%=ct%>!

</BODY>

</HTML>

 

 

Guide for å lage en HitCounter i PHP (engelsk)

 

 

1. Create a counter text file of the name hitcounter.txt and save it to the same directory you will put your hit counter in.

 

2. Open your php editor. Save the document as counter.php. Set your opening statement for PHP script.

 

<?php

?>

 

3. Set the variable for the file named hitcounter.txt and place this between your PHP tags. remember to place the filename in quotes. place the semicolon on the end of the line to end the command.

 

<?php

$count_my_page = ("hitcounter.txt");

?>

 

4. Set the number of visits to the current value of the contents of the hitcounter.txt file:

 

<?php

$count_my_page = ("hitcounter.txt");

$hits = file($count_my_page);

?>

 

5. When the script is accessed as the page loads, it not only reads the current number of hits on the page in the hitcounter.txt, it must increase the value by 1 for the current hit to be recorded. Add 1 to your value of $hits and call it $hits.

 

<?php

$count_my_page = ("hitcounter.txt");

$hits = file($count_my_page);

$hits[0] ++;

?>

 

6.Open the file that is keeping count so we can write to it

 

<?php

$count_my_page = ("hitcounter.txt");

$hits = file($count_my_page);

$hits[0] ++;

$fp = fopen($count_my_page , "w");

?>

 

7. Replace the value in the file with the new $hits number after it was incremented.

 

<?php

$count_my_page = ("hitcounter.txt");

$hits = file($count_my_page);

$hits[0] ++;

$fp = fopen($count_my_page , "w");

fputs($fp , "$hits[0]");

?>

 

8. You must close the file next.

 

<?php

$count_my_page = ("hitcounter.txt");

$hits = file($count_my_page);

$hits[0] ++;

$fp = fopen($count_my_page , "w");

fputs($fp , "$hits[0]");

fclose($fp);

?>

 

9. Set the code to display your hit number.

 

<?php

$count_my_page = ("hitcounter.txt");

$hits = file($count_my_page);

$hits[0] ++;

$fp = fopen($count_my_page , "w");

fputs($fp , "$hits[0]");

fclose($fp);

echo $hits[0];

?>

 

10. Save your php file in same directory as the text file.

 

11. To use this script you just have to add a php statement that calls that php file. Use the following include:

 

<?php

include ("counter.php");

?>

 

 

Endret av Dain Bramaged
Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...