Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 05-10-2008, 05:51 PM   #1 (permalink)
Right Off the Assembly Line
 
muralikrishnan's Avatar
 
Join Date: Aug 2005
Location: Chennai
Posts: 17
Default Need Help About Webpage Counter Using PHP / JS


Hi Friends,

I need webpage counter code using PHP/JavaScript.I don't need the site i need code which is run for my localhost or with in LAN area for that i need the counter program is there any site avilable i tried and i didn't get the source program many sites provide only their site to paste and that site logo with the counting image is shown in the page so i need the general script with out any site support..

Thanks in advance............
__________________
P B MURALIKRISHNAN

"Born Today Newly & Learn More."
"Impossible' is not a scientific term"
"Don't waste your time on a man/woman, who isn't willing to waste their time on you"
muralikrishnan is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 05-10-2008, 09:23 PM   #2 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: Need Help About Webpage Counter Using PHP / JS

For PHP you will need a server. Here are the steps:
1. Create a text file that shall store the page count.
2. When a page is visited, run a PHP script that shall take the value from the text file, increment by 1, and put it back into the text file(open->read->erase all data->write new data).
3. Display the incremented count on the webpage.

With JS, you can only store it in a cookie on the visitors' browser and the count shall be specific to that user. Also, the cookies can be cleaned(deleted) at any time by the user. So JS will be a poor candidate.
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता
victor_rambo is offline  
Old 05-10-2008, 09:47 PM   #3 (permalink)
Human Spambot
 
toofan's Avatar
 
Join Date: May 2008
Location: Haldwani(Nainital)
Posts: 2,124
Default Re: Need Help About Webpage Counter Using PHP / JS

In place of a text file mysql database file will be more suitable.
toofan is offline  
Old 05-10-2008, 10:31 PM   #4 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: Need Help About Webpage Counter Using PHP / JS

^+1,
Infact, incrementing the count would take just a single SQL statement.
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता
victor_rambo is offline  
Old 05-10-2008, 10:36 PM   #5 (permalink)
Human Spambot
 
toofan's Avatar
 
Join Date: May 2008
Location: Haldwani(Nainital)
Posts: 2,124
Default Re: Need Help About Webpage Counter Using PHP / JS

I had just created a small php code for displaying the hit counter. Its working. Please give suggestion about any loopholes in this code. As i am in learning stage.

PHP Code:

<?php 
// connect to mysql
mysql_connect("localhost""root""password") or die("Can not connect: ".mysql_error());
/* connecting to the database. create a database name hitcounter in mysql */
mysql_select_db("hitcounter") or die("Error connecting database: ".mysql_error());

/* first create a table in mysql named hitcounter with two columns id and counter with values 1 and 0 */
// geting the stored value of counter which is 0 from the table
$query "select counter from hitcounter";
$result mysql_query($query);
$row mysql_fetch_row($result);
$counter $row[0];
$hitcounter $counter +1;

//now submitting the updated counter again to database
$query_submit "update hitcounter set counter = '$hitcounter'
                where id=1 "
;
$result_submit mysql_query($query_submit);

?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>title</title>
</head>

<body>
<h3> Hit counter</h3>
<input type="text" size="6" value="<?php echo "$hitcounter?>">
</body>
</html>
toofan is offline  
Old 06-10-2008, 09:44 AM   #6 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: Need Help About Webpage Counter Using PHP / JS

^^

PHP Code:
<?php
mysql_connect
('localhost',"$username","$password");
mysql_select_db('hitcounter');
/*Lets update the table named 'counter' that holds the count in a field name 'count'. The field 'count' is of INT type!!*/
mysql_query('UPDATE hitcounter SET count=count+1');
/*Lets fetch the updated value of the table*/
$resource=mysql_query('SELECT count FROM hitcounter');
$count=mysql_result($resource,0,'count');
echo 
'This page has served paged '.$count.' times.';
?>
Also, if you want to have to store separate counts for each webpage, then we will have to
the 'ID' field that should be linked to the web page name or path. The way of calling the counter functions also would need to be modified.

Since you have asked for suggestions:
1. in PHP, understand when you should use double quotes and when single quotes. A string should be enclosed in double quotes when it contains a variable that should be parsed while outputting it. Enclosing string in double quotes means that PHP has to do extra work to search if there is any variable in the string. If its there, fine, else waste of time and energy for the parser. So if your string contains no variable, better to enclose it within single quotes.

2. In a MySQL table, have the appropriate database fields. There is no need to have 2 fields. Once will suffice. Also, since we need mathematical manipulations, we should SET the type of field to 'INT'. This way, we won't have to use PHP's math ability to update the count.

3. Since we are selecting only one column from the table, we can use 'mysql_result()' rather than 'mysql_fetch_*()' functions.

4. The ' or die(mysql_error())' tip: You already know it. So no need stressing it.
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता

Last edited by victor_rambo; 06-10-2008 at 09:59 PM.
victor_rambo is offline  
Old 06-10-2008, 03:48 PM   #7 (permalink)
Human Spambot
 
toofan's Avatar
 
Join Date: May 2008
Location: Haldwani(Nainital)
Posts: 2,124
Default Re: Need Help About Webpage Counter Using PHP / JS

Thanks roshan,

You have made my long code to just few lines and I got the each and every word you said. Its really helpful to learn when someone guides you. I will be looking forward for more helpful tips.

One suggestions
There isn't any thread for PHP. Everyone talks about Java, C++, C, C# but no one for php. Could you start a new thread for php tutorials. In which you and other fellows can post tutorials, tips and tricks which comes only by experience. I know it will be a hard task to do so and devote your time to it but It will help many many.

Thanks once again.
toofan is offline  
Old 06-10-2008, 06:43 PM   #8 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: Need Help About Webpage Counter Using PHP / JS

^^
The name is Rohan and not Roshan
As about me starting a thread on PHP tutorials, I guess that would amount to reinventing the wheel. For the interested ones, tips and tutorials are a just a Google search away! Of course, you can ask for help if you are held up somewhere!

And of course, Thanks

btw if you are interested, you can check the relevant categories on my website: http://www.w3hobbyist.com/
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता

Last edited by victor_rambo; 06-10-2008 at 07:14 PM.
victor_rambo is offline  
Old 06-10-2008, 08:58 PM   #9 (permalink)
Human Spambot
 
toofan's Avatar
 
Join Date: May 2008
Location: Haldwani(Nainital)
Posts: 2,124
Default Re: Need Help About Webpage Counter Using PHP / JS

Oh sorry Rohan but from starting I always read it Roshan, this is the good example of "we see that what we want to see" bug in human brain coding.

I am checking your website.
toofan is offline  
Old 07-10-2008, 12:35 AM   #10 (permalink)
spice it up
 
kapsicum's Avatar
 
Join Date: Apr 2004
Location: mumbai
Posts: 106
Default Re: Need Help About Webpage Counter Using PHP / JS

hey guys this is my suggestion as per what i believe that counters must increment only once per user session ....
we need to update the DB counter only ONCE for a user's particular session. since i believe dats the Fair User Visiting Counter

So as to avoid the query execution & counter increments every time the page is refereshed by a user, following is a small addition/modification to ur code:

PHP Code:
<?php
session_start
();
if(!isset(
$_SESSION['viewed']))
{
 
$_SESSION['viewed']=1;
 
//rohan's code except last line 
}

echo 
'This page has served paged '.$count.' times.';
?>
let me know if u think I am wrong and/or my code is not proper ...since its long time i have worked on PHP

Last edited by kapsicum; 07-10-2008 at 01:58 AM.
kapsicum is offline  
Old 07-10-2008, 04:55 AM   #11 (permalink)
MMO Addict
 
amitava82's Avatar
 
Join Date: Jul 2004
Location: Bangalore
Posts: 1,474
Default Re: Need Help About Webpage Counter Using PHP / JS

^^ Yeah thats what I had in my mind. Good one!
__________________
Steam Profile || Personal Page
Warp drive active. Approaching stargate.
amitava82 is offline  
Old 07-10-2008, 10:02 AM   #12 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: Need Help About Webpage Counter Using PHP / JS

@Kapsicum,
He has asked for webpage count, not visitor count, though visitor count is more significant that page count .
Since you have focused on visitor count, I will suggest that you use a tracking cookie instead of sessions. As you will know that session cookies are cleared at the end of session, tracking cookies can be set to expire to a given time. So every time the visitor requests the page after closing the browser and restarting it will increment the counter.

PHP Code:
<?php
if(!isset($_COOKIE['my_tracking_cookie']))
{
//set cookie with a long expiry
//now update the cookie
}
?>
Of course, you can't prevent error due visitors clearing their cookies prematurely.
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता
victor_rambo is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help...!!no webpage will open.. himadri_sm Software Q&A 10 04-10-2008 06:46 AM
Webpage Ishan QnA (read only) 8 05-03-2007 03:26 PM
how 2 add irc to webpage santu_29 QnA (read only) 4 17-03-2006 09:14 AM
How to save the URL of a webpage within the webpage? arvlee QnA (read only) 3 07-09-2005 04:59 PM
saving webpage john_the_ultimate QnA (read only) 11 31-08-2005 11:08 AM

 
Latest Threads
- by clinton
- by soumya
- by Sujeet
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 11:14 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2