Forum Moderators: coopster

Message Too Old, No Replies

visitor counter

         

sqlnew

9:21 pm on Sep 28, 2005 (gmt 0)

10+ Year Member



I need add a visitor counter to show how many total visitors visiting my webpage. Any suggestion is appreciated.

coopster

12:34 am on Sep 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You mean a visitor counter that others can see which will be displayed on your page?

sqlnew

1:32 pm on Sep 29, 2005 (gmt 0)

10+ Year Member



Yes. Whenever there is anyone visit my page, he can see how many visitors have vsited my page.

Thanks.

sqlnew

8:29 pm on Sep 29, 2005 (gmt 0)

10+ Year Member



no matter it is text or graphic.

ergophobe

5:14 pm on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why not just sign up for one of the many free counters that are available?

BTW, unless you are counting downloads or some such thing, my personal opinion is that counters *usually* look amateurish. You may have your reasons, but you may want to examine them and ask what the counter is really doing for you and your visitors.

sqlnew

5:59 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



Thank you for your reply. Actually my supervisor wants to know how many visitors visited his web page I created for him. I want to hearn how to program a counter, that's why I didn't use the free one. Right now I don't have any clue how to do it. Is it very hard?

ergophobe

8:40 pm on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No, it's not hard, depending on how the pages are set up. I tend not to use the auto_preprend option in PHP, but one easy way would be simply to do this

-set up PHP (using .htaccess options) to auto_prepend a file to every PHP page that's requested.

- if the site consists of non-php pages, set up the server to treat them all as PHP and, thus, prepend the file you specified in the previous step.

- the prepended file simply checks for the URL which has been requested and uses that as the key for your database. If the URL exists in the DB, the value gets incremented by one. If it doesn't yet exist, a record gets created for that URL and it gets set to a value of one.

- you'll need to decide what to do with URLs that have query strings as in file.php?query=string (treat separately or all increment file.php record).

That's a general overview and I realize that you may not be able to accomplish any single step yet based on that general info, so let's get you started. First, make sure you have everything you need

- a server that runs PHP and has a database
- adequate permissions to use .htaccess or to modify your php.ini file

Once verified, you'll need to create and test a prepended file.

Read up here to see if you can figure it out on you own'
[us2.php.net...]
[us2.php.net...]
[us2.php.net...]

Just test with something simple in the prepend file like

echo "Prepending";

Once you know it's working, go to the forum library [webmasterworld.com] and read up on the basics of extracting data using MySQL. That should be fun (I'm not being sarcastic - based on the fact that you said you're doing this from a desire to learn something new, it really should be fun).

sqlnew

9:47 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



Thank you very much. I'll follow your suggestions. I have to leave now. I may have questions for you later.

I really appreciate your kind help.

sqlnew

9:21 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



I do have an account on a server which is PHP/MySQL enabled.

When I tried

<?
auto_prepend_file "prefile.php";
?>

I got error message "unexpected T_CONSTANT_ENCAPSED_STRING" on the above line.
I checked the configuration for auto_prepend_file, there is 'no value' for both local value and Master value. Is this the reason for the error message?

I don't know if I have permission to use .htaccess. How can I check whether I have permission or not to use it? Sorry for being such a noob.

Thank you.

coopster

12:40 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You have PHP tags surrounding the statement and are trying to run it in a script which is not going to work because
auto_prepend_file
is of type PHP_INI_PERDIR [php.net], which means the entry can be set in
php.ini
,
.htaccess
or
httpd.conf
.

It is highly unlikely that your host is going to allow you to edit the

php.ini
or
httpd.conf
so you will need to find out if you are allowed to use per-directory override (
.htaccess
) files.

sqlnew

2:26 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Thank you very much for your reply. If I am not allowed to use per-directory override (.htaccess) files, is there another way for me to create a visitor counter script?
Thanks again.

coopster

3:09 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The prepend option offered by ergophobe is just one way of including a php file on every page without having to "hard-code" a PHP include() [php.net] in every page to get your code processed. You can always go that route though.

sqlnew

4:23 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Thanks.
Can you tell me how to get URL? I have tried $SERVER['REQUEST_URI'], but it only gave the file name. How can I get the full path for example "http://somewhere.com/file.php"? As ergophobe suggested, I need use the URL as key to update the the table.
Thanks again.

coopster

4:50 pm on Oct 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can append that path to the SERVER_NAME I suppose.


$_SERVER [php.net] is an array containing information such as headers, paths, and script locations. The entries in this array are created by the webserver. There is no guarantee that every webserver will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the CGI 1.1 specification, so you should be able to expect those.