/    Sign up×
Community /Pin to ProfileBookmark

i just started learning how to program a few months ago and im stuck. I have to create a Counter for a Web site and all the tutorials i come accross dosnt seem to help me out any. Now i have only been on PHP for about a week and i was wondering if anyone knew how to make a simple hit counter? It will really help me out if i can get it created by thursday (end of 9 weeks) because it is due then. and i just got it today. Plz. help in any way that you can.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@Markbad311Feb 20.2006 — To put you one the right road a couple things you need to know.


1. to make a counter for PHP every file you want the counter to count must have a .php extension.

2. You need to have access to a Database or write it in side of a .txt file.

3. Php must be installed on your server.

here is something simple I use in a seperate .php file called "func.php"
[code=php]
///////////////////////////////////////////////
//Counts All Access By Page Name, Displays ///
/////////////////////////////////////////////
function pagecount($page_title) {
$page_name = $page_title; //gets the name from the page
$IP_address = getenv("REMOTE_ADDR"); //gets the user IP address
$user_agent = getenv("HTTP_USER_AGENT"); //gets the browser
$date_added = date("Y-m-d");// The date in mysql standard format
$time = date("h:i:s a"); //time 1:02:08 pm

// Setup table and DB
$table_name = "page_track";
$db_name = "database"; //set db name
$connection = mysql_connect ("localhost", "username", "password")
or die ('I cannot connect to the database because: ' . mysql_error());//location, username, password
$db = mysql_select_db ($db_name, $connection)
or die ('I cannot connect to the database because: ' . mysql_error());//data base select
$sql = "INSERT INTO $table_name VALUES ('$page_name', '$IP_address', '$user_agent', '$date_added', '$time')";//inserts the whole thing into your database
$result = mysql_query($sql, $connection) or die (mysql_error());

// Show this area selects all the mattching pages out of the DB and counts them then displays
$count_sql = "SELECT COUNT('$page_title') FROM $table_name WHERE '$page_title' = '$page_name'";
$count_res = mysql_query($count_sql, $connection) or die (mysql_error());
global $count;
$count = @mysql_result($count_res, 0, "count('$page_title')");//$count is = hits
//

}
[/code]


and I call it from each page like this.
[code=php]
<? require("locationoffunction/func.php");
$page_title = "Name of Page";
pagecount($page_title);
?>
<html>
<head></head>
<body>

<!--somewhere in a footer -->
<? echo "$count"; ?>
</body>
</html>
[/code]



hope it helps
Copy linkTweet thisAlerts:
@CliffordauthorFeb 22.2006 — it does a little but how do i create a unique counter? and can i do it without a database (MYSQL)? I havnt learnt anything about MYSQL yet, wont get to it for another 2 weeks
Copy linkTweet thisAlerts:
@Pixel-ArtistFeb 22.2006 — You can do it in a txt file also
Copy linkTweet thisAlerts:
@CliffordauthorFeb 22.2006 — how would i go about doing that?
×

Success!

Help @Clifford spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...