PDA

View Full Version : Site Counters ...



pcbrainbuster
03-01-2007, 08:12 PM
Hello all :),

Well again as usual I need to know something and this time it is to do with site counters... I simply just want to know how to do that :)

Any help will be greatly appreciated :)

(by site counters i mean that every time the page is visited the counter goes up by one and anyone can see the updated value)

Blake
03-01-2007, 09:05 PM
It would need to be done server side. I would keep an entry that stores the number of hits somewhere in a database. Then when someone views the page, I would increment the count in the database, and then display the new count.

I would probably also use a php session to keep track of users, so that the counter only gets incremented once if someone is browsing through the page.

I could help you out setting one up if you want.

mburt
03-01-2007, 09:09 PM
One of the very basic PHP counters (doesn't read I.P):

<?php
$cdata = file_get_contents("counter.txt");
$ndata = ($cdata+1);
$handle = fopen("counter.txt","w+");
fwrite($handle,$ndata);
fclose($handle);
echo "\n<br>".$cdata." visits";
?>

Blake
03-01-2007, 09:22 PM
One of the very basic PHP counters (doesn't read I.P):

<?php
$cdata = file_get_contents("counter.txt");
$ndata = ($cdata+1);
$handle = fopen("counter.txt","w+");
fwrite($handle,$ndata);
fclose($handle);
echo "\n<br>".$cdata." visits";
?>

Wow, that's nifty. I never considered using a file.

To expand on that, here's how you would use sessions to only increase the count once per visit:




// do this stuff from here ...
session_start();

$cdata = file_get_contents("counter.txt");
$ndata = $cdata;

if (!isset($_SESSION['user_id']))
{
$_SESSION['user_id'] = 'user';
$ndata = ($cdata+1);
$handle = fopen("counter.txt","w+");
fwrite($handle,$ndata);
fclose($handle);
}
//... to here BEFORE sending any data to the browser.

echo "\n<br>".$ndata." visits";

pcbrainbuster
03-01-2007, 09:30 PM
Wow,
you guys are great at this ! :)

Except there may be a few problems - i do not know how to conect a php file to html :( or how php works (but i will start learning that to - just not yet)

Blake
03-01-2007, 09:34 PM
The file name needs to end in .php, and any php script needs to go between php tags (<?php and ?>).

For example,




<html>
<head>

<?php

echo '<title>This is php!</title>';

?>

</head>
<body>

<?php

echo 'this is more php!';

?>

</body>
</html>



The server will parse this and send the following to your web browser:




<html>
<head>

<title>This is php!</title>

</head>
<body>

this is more php!

</body>
</html>

pcbrainbuster
03-01-2007, 09:46 PM
Right, So how would add one of the above php codes into the html file :)

Blake
03-01-2007, 09:53 PM
Put this in your file:



<html>
<head>

<?php

echo '<title>This is php!</title>';

?>

</head>
<body>

<?php

echo 'this is more php!';

?>

</body>
</html>


Then save the file as something.php, upload it to your server, and then pull it up (from your server) in a web browser.

It won't be an html file, it will be a php file, but it can still have html in it.

For example, your main page would be index.php instead of index.html.

pcbrainbuster
03-01-2007, 09:58 PM
Thats not what i ment :)
You gave the counter script and thats what i want combined wit hsome html down below :)

pcbrainbuster
03-01-2007, 09:59 PM
Before i do that though do i need to download php to my ("borrowed") server and my computer ?
Do you know were to ge it from ? - i am using talktalk's server

Blake
03-01-2007, 10:02 PM
Ok. Here's an example:



<?php
session_start();

$cdata = file_get_contents("counter.txt");
$ndata = $cdata;

if (!isset($_SESSION['user_id']))
{
$_SESSION['user_id'] = 'user';
$ndata = ($cdata+1);
$handle = fopen("counter.txt","w+");
fwrite($handle,$ndata);
fclose($handle);
}
?>

<html>
<head>
<title>This page has a counter!</title>
</head>
<body>
Welcome to my nifty web page! It has a nifty counter!<br><br>

This page has been visited <?php echo $ndata ?> times.

</body>
</html>


Now save that as a file name ending in .php, upload it to your server, and try it out.

If you want to see that code working, go to www.blake-foster.com/demo/count.php

Don't forget that you need a file called counter.txt in the same folder, with the initial count in it.

pcbrainbuster
03-01-2007, 10:13 PM
Well blake i tried what you said except the server does not recognise the php file and when i try to view it in ie7 it ends up showing the code for the page.

And also your link is currently dead... :)

mburt
03-01-2007, 10:54 PM
Make sure that the file ends with .php, if else the browser will try to parse the php code as text.

mburt
03-01-2007, 10:57 PM
Your server also has to support PHP.

pcbrainbuster
03-01-2007, 11:40 PM
Oh poopy !!! :)

pcbrainbuster
03-01-2007, 11:41 PM
Can't javascript be used i have seen examples before of it but don't remembber were ...

Blake
03-01-2007, 11:53 PM
Some of it could be done with javascript, but at least part of the code needs to be server side.

I host my site at www.clawz.com. I have a paid account, but even their free accounts support php and mysql.

Also, I fixed that link.

mburt
03-01-2007, 11:55 PM
Unless it's some sort of iframe type thing...
like statcounter.com: http://www.statcounter.com/

pcbrainbuster
03-02-2007, 07:49 AM
Are you both really sure that javascript can't be completely be used :(
(once i actually took that script and tried it and it worked (was javascript)
and it continued to work on my hard drive - i am sure there were no cookies used as the counter was already 10,000+...)

pcbrainbuster
03-02-2007, 11:28 AM
Hello ?

Blake
03-02-2007, 02:07 PM
Can't be done. You could probably do something that would work on your computer using cookies, but making a counter that works on the internet requires server-side scripting.

pcbrainbuster
03-02-2007, 02:16 PM
Yeah...
I see what you mean...
Maybe that javascript was somehow connected to the server/php-program

BLiZZaRD
03-02-2007, 02:26 PM
You will need some type of server side script. That's it. You can write to a flat file, but you will need some script that will write to a file, plus you will need global read/write permissions on that file.

Is it possible without php? Yes. Reliable? No.

If you are really that insistent on not using php or another server side language, search google for something like: client side hit counter. or no php counter, etc.

See what it out there.

Just remember if you run an all client side script you are relying on the fact that each and every user will have that language turned on and enabled on their browser. If it is off, disabled, or not installed, it won't work for them and you won't ever know they were there.

pcbrainbuster
03-03-2007, 07:19 PM
Hey blizz :),

Can javascript be server-side as well as client-side or is it client-side only ?

pcbrainbuster
03-04-2007, 02:26 AM
???:confused:

thetestingsite
03-04-2007, 02:31 AM
Javascript is all client side. PHP (as well as ASP and any other language that is parsed on the server) is server side. No ands, ifs, or buts about it.

Sorry, but hope this helps.

mburt
03-04-2007, 03:23 AM
Simply put:
Client-side (javascript): can only interact dynamically with the user's browser on the fly, or as you see it
Server-side (php, asp, etc.): can save files, to database queries and many more things that involve "saving" as it may be called.

thetestingsite
03-04-2007, 03:29 AM
Also, as far as client side goes for a counter script, you may want to look into AJAX. But even then, you would need something server side to have it save to a database or something along those lines.

Hope this helps.

pcbrainbuster
03-04-2007, 01:50 PM
Thanks ALOT :)
So... One day i will try and understand PHP (tutorials) - what is a recomended site and will php be hard to understand? :confused:

mburt
03-04-2007, 01:57 PM
PHP syntax is very similar to JavaScript. It uses if () {}, and else if, else the same way. It's while loop is the same, and so is the for-loop. But instead of assigning variables with the "var" keyword you do like so:

<?php
$myvar = "hi";
if ($myvar == "hi") {
echo "you said hi.";
}
else {
echo "you didn't say hi.";
}
?>
As for the "echo": it basically produces html output on the page.
For a good php tutorial, use the real reference: http://www.php.net/
All the functions are on that site as well.

pcbrainbuster
03-04-2007, 06:33 PM
Thanks :)

pcbrainbuster
03-04-2007, 06:47 PM
Uhhh help please :),

I REALLY don't think that the server (talktalk's) i am using has php installed on it...

What should i do ?

mburt
03-04-2007, 06:53 PM
Use the example given.

pcbrainbuster
03-04-2007, 07:44 PM
What do you meen :confused:,

If you meen put the example in a file then upload it to the server - i have tried that.

Here be the code -
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>

Thanks :)

mburt
03-04-2007, 07:53 PM
You must understand that php doesn't work locally on your computer (unless you have l/wamp installed). You have to upload them to your server and try them. The example I gave (http://www.dynamicdrive.com/forums/showpost.php?p=78176&postcount=3) does work.

thetestingsite
03-04-2007, 08:10 PM
You must understand that php doesn't work locally on your computer (unless you have l/wamp installed). You have to upload them to your server and try them. The example I gave (http://www.dynamicdrive.com/forums/showpost.php?p=78176&postcount=3) does work.

As already said before:


I REALLY don't think that the server (talktalk's) i am using has php installed on it...


The other option for you (pcbrainbuster) would be to set up your own "testing" server. For a good tutorial on how to do this, look at the article "How to set up WAMP" on Mike's wiki.

Hope this helps.

Blake
03-04-2007, 08:14 PM
What do you meen :confused:,

If you meen put the example in a file then upload it to the server - i have tried that.

Here be the code -
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>

Thanks :)

Make sure the file name ends in .php

pcbrainbuster
03-04-2007, 09:43 PM
Thanks for your posts :),

I made the extension with .php i did try to upload it to "my" server but i still need to try the whole "l"amp thing :)

mburt
03-04-2007, 09:45 PM
http://www.prophp.org - Free php hosting, uses ftp. Make sure to put your files in the www/ directory.

pcbrainbuster
03-04-2007, 11:18 PM
Thanks for the post :),
I will try that site and report back.

pcbrainbuster
03-04-2007, 11:37 PM
Lol its very confusing :), how do i logon into the ftp service to "add" files?

mburt
03-04-2007, 11:41 PM
Type this into your browser:
ftp://username:password@ftp.prophp.org/
If you use IE, it should would just like a folder on your computer. Go into the www folder first, and put your files there.

thetestingsite
03-04-2007, 11:41 PM
Open your FTP client (or Internet Explorer), go to the ftp site and login using the username and password that you were given.

Hope this helps.

EDIT: Sorry mike, cross-posted.

pcbrainbuster
03-05-2007, 12:33 AM
Lol :),
Ok great now the ftp part is working correctly :)
Now how do I actually access the site eg www.whatisit.com

Blake
03-05-2007, 12:40 AM
If you're using windows, you can also make an ftp folder on your desktop. I can tell you how to do that if you want.

mburt
03-05-2007, 12:43 AM
Your ProPhp domain goes as follows:

http://username.prophp.org/

pcbrainbuster
03-05-2007, 12:54 AM
Well mburt i tried what you said and it seems to not work -

http://pp_414729.prophp.org/www/index.htm

Thats the page for one of my lame tests a long time ago (there is no need to say anything about it)

But the thing is that page does not load up - do you know the problem ?

thetestingsite
03-05-2007, 01:28 AM
The link you posted above just redirects me to

http://partners.22web.net/a/a/index.php

even when I take out the www part. Not sure if this is what you have set up or not. If you would like, I could set you up a folder on my testing server. Let me know and I can set up the FTP access and all that other good stuff.

(PM me if you have any questions regarding this.)

pcbrainbuster
03-05-2007, 01:30 AM
Thanks for your post :),

And no that is no supposed to happen the redirection thing -
And plz explain about the server thing you said above :)

thetestingsite
03-05-2007, 01:34 AM
Just sent you a PM.

Also, there are no ads on my server.

pcbrainbuster
03-05-2007, 01:35 AM
Uhhhh, what is a PM :confused:

thetestingsite
03-05-2007, 01:41 AM
PM is a Private Message. In the top right hand corner of your screen, you should see something that says "Private Messages". Click on that link.

pcbrainbuster
03-05-2007, 01:45 AM
Wow, thanks for your offer :),

I will accept and as a "pay back" you can login to "my" account and take anything that you see fit (not that it is my choice in the first place - i simply will not pur any copy right on it) and please set the password and username as you see fit :)

By..the..way.....Is it your server ?

thetestingsite
03-05-2007, 01:47 AM
By..the..way.....Is it your server ?

Yes, it is my server (in fact, I am looking at it right now). I will PM you the details in a few seconds about the login info.


Added Later: Ok, all set up, and sent the PM to you with your login info. Let me know if you need any more help.

pcbrainbuster
03-05-2007, 07:43 AM
Wow, its a very very good server - its got everything and thanks i cwill report back soon :)

pcbrainbuster
03-05-2007, 07:57 AM
Hmmmm :confused:,

When I uploaded a php file and opened it by right clicking it's link in the file manager it did not open instead it asked me if i wanted to save it or run it or cancel it - Any idea on what went wrong :confused:

thetestingsite
03-05-2007, 02:56 PM
Well, the file manager is weird (spelling?). If you go to the url of the file itself, it is fine. For some reason, the FM uses an internal script thingy to either view certain files and download others.

Anyways, hope this helps.

pcbrainbuster
03-05-2007, 04:44 PM
OK i see :),

Well i have now uploaded a file index.php as mensioned above and now just need to know how to enter that file - evrything from there would be clear then :)

thetestingsite
03-05-2007, 04:59 PM
Ok, so to access the file itself, go to the url that I sent you in that PM. That will take you to your directory. If you need help around the File Manager, click on the link in the top right corner that says "Help".

Let me know if you need anything else.

Added Later:

Look at the 2 screenshots below for more info.

pcbrainbuster
03-05-2007, 07:58 PM
Lol :),
Sorry i deleted both the pm and the php file in the account ! :)
Mind jhelpin me out :)
The account again was called (you know what)

thetestingsite
03-05-2007, 08:01 PM
Sent you a pm with the login information and the urls.

pcbrainbuster
03-05-2007, 08:09 PM
Lol :0,
I checked again and i did have the information - thanks :)
It is now working perfectly and the php test is also working :)

pcbrainbuster
03-05-2007, 08:19 PM
Does it have MySQL ? (the server) :confused:

thetestingsite
03-05-2007, 08:28 PM
I do have MySQL installed. If you would like, I can set up a username and password for you. Just let me know.

pcbrainbuster
03-05-2007, 08:57 PM
Thanks again :),
But didn't you just give me a user name and password for it :confused:

thetestingsite
03-05-2007, 09:01 PM
Ah yes, this is true. The only thing I forgot to do is set it up on the server itself. :).

Anyways, it is all set up. The database is the same as your username and if you try to connect to it using a PHP script on the same server, then use "localhost" as the server variable (if that makes sense), otherwise use the url I sent in the PM.

pcbrainbuster
03-05-2007, 10:42 PM
Lol i still barely know about php so i barley understand wht your saying :)

pcbrainbuster
03-06-2007, 08:37 AM
OK :),
Now that generously from thetestingsite i have a php server that can obviously read php files - now can anyone give me counter script that goes up by one every visit :)

pcbrainbuster
03-06-2007, 04:38 PM
Please :)

mburt
03-06-2007, 04:49 PM
When x page is visited:

<?php
$cdata = file_get_contents("counter.txt");
$ndata = ($cdata+1);
$handle = fopen("counter.txt","w+");
fwrite($handle,$ndata);
fclose($handle);
echo "\n".$cdata." visits";
?>

Blake
03-06-2007, 05:35 PM
A slightly modified version of mburt's script that only increases the count once per session:



<?php
session_start();

$cdata = file_get_contents("counter.txt");
$ndata = $cdata;

if (!isset($_SESSION['user_id']))
{
$_SESSION['user_id'] = 'user';
$ndata = ($cdata+1);
$handle = fopen("counter.txt","w+");
fwrite($handle,$ndata);
fclose($handle);
}
?>

<html>
<head>
<title>This page has a counter!</title>
</head>
<body>
Welcome to my nifty web page! It has a nifty counter!<br><br>

This page has been visited <?php echo $ndata ?> times.

</body>
</html>

pcbrainbuster
03-06-2007, 09:18 PM
Thanks :),

To thetestingsite = ":) I tried to keep getting into the site but it keeps saying that my internet connection is not connected - but it is and i tested it :(

mburt
03-06-2007, 09:25 PM
Your internet connection is connected. Obviously, because you are still posting on the forum :)

pcbrainbuster
03-06-2007, 09:41 PM
Yup thats what i meant by tested it :)

edit//= But what makes you think i may not be at my cousin's house using his computer ? :) Though i am on the computer i always use

pcbrainbuster
03-06-2007, 10:00 PM
Never mind it now works :)

pcbrainbuster
03-06-2007, 10:18 PM
Hello and thanks to all :),

I tested the two hit counters and they both work :),
Now I need to understand how this works - i understand the certain things like fopen and fclose are beong used mind explaining :) (also please explain the use of operators in this(espically for echo like adding 1+1 how))

Thanks :)

mburt
03-07-2007, 12:10 AM
It can be easily broken down:

$cdata = file_get_contents("counter.txt");
Get the contents of the file which stores the number of hits.

if (!isset($_SESSION['user_id']))
Check to see if the user has already been on the page.

$ndata = ($cdata+1);
The new data ($ndata) is the $cdata plus one, which is the number of hits plus one.

$handle = fopen("counter.txt","w+");
fwrite($handle,$ndata);
fclose($handle);
These are all part of the "writing to file" process. The handle initiates the opening of the file, fwrite() actually writes the data, and fclose() closes the connection.

thetestingsite
03-07-2007, 02:05 AM
Thanks :),

To thetestingsite = ":) I tried to keep getting into the site but it keeps saying that my internet connection is not connected - but it is and i tested it :(


Sorry about that, I just found out that earlier this morning (while I was at work) my daughter came in and turned the computer off. It was only for a brief minute and shouldn't happen again unless I am doing maintenence on it.

Again sorry. Let me know if you need any more help.

Blake
03-07-2007, 02:53 AM
I just added a hexadecimal counter to my website :D It display the count in hex, with the 0x prefix.

It's easy to do with php's built-in dechex function.

pcbrainbuster
03-07-2007, 08:05 AM
To thetestingsite - What do you mean by the computer being turned off (is a server supposed to be on FOREVER !!!) And its alright :)

To mburt - thanks for the explanation but i already understand howcthe plus one and that works but may i please have a detalie explanation on all fopen, fwrite and fclose..... (like why the letter f before the open, close, write) :)

Thanks :)

tech_support
03-07-2007, 08:47 AM
Flick the switch.

pcbrainbuster
03-07-2007, 04:10 PM
What switch ?

thetestingsite
03-07-2007, 08:31 PM
What do you mean by the computer being turned off (is a server supposed to be on FOREVER !!!)

A server is just a computer, it can be turned off (or disconnected from the internet) like so. In my case, I run both a home server and some other servers for the ISP I work for. You are currently being hosted on my home server until I can move some stuff around to fit you on one of the other servers. Yesterday, when you could not access the server, it was due to my daughter pressing the power button on the computer (thus turning it off :)).

pcbrainbuster
03-07-2007, 08:35 PM
I see. What ISP you work for ?

thetestingsite
03-07-2007, 11:29 PM
I just work for a local ISP in the town that I live in. Not a big name company.

mburt
03-08-2007, 01:23 AM
Yeah, it seems like you really know what you're doing :).

thetestingsite
03-08-2007, 03:19 AM
Yeah, it seems like you really know what you're doing :).

Thanks, I learn fast. ;)

tech_support
03-08-2007, 06:00 AM
What switch ?

Logic.

Computers need power. What happens when you switch off the power? The computer goes off aka. "Flicking the switch"

pcbrainbuster
03-08-2007, 07:15 AM
lol :)