CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Tips & Solutions

 PHP Hit Counter

Print topic Send  topic

Author Message
Damian Hupfeld
Posted: 10/11/2005, 5:32 AM

create a table as follows:

counters
cid
name
value

for each page that you wish counters for create an entry eg home, products,
contact

add the code below as custom code substituting
$cname for the name above
server for the server name/address
username for the sql user
password for the sql user password
database for the sql database name.

This will only log the visitor once per session per page you are tracking -
you cant inflate your counters by hitting refresh.


//Custom Code @80-B6E9E578
// -------------------------
global $counter;
// Write your own code here.
//Change These Values for counter and Database
//Set counter
$cname = "home";

//open database
$conn = mysql_connect(server', 'username', 'password');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database');

//Don't Change anything below
//Connect to sessions
session_start();
//Determine if seesion exists
If (!isset($_SESSION[$cname])) {
//create the session
$_SESSION[$cname]="set";

//Query Database for current counter
$sql="SELECT * FROM counters WHERE name='".$cname."'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$count = $row['value'];
//Increment Counter
$count = $count + 1;

//Update Database
$sql="UPDATE counters SET value=".$count." WHERE name='".$cname."'";
$result = mysql_query($sql);
}

//End Custom Code



regards
Damian Hupfeld
http://www.nexthost.com.au/services.php





DonB
Posted: 10/11/2005, 7:14 AM

Might be better to simply set a session var, and eliminate the database
accesses following the 1st UPDATE that occurs.

If (CCGetSession(session_id(),"") != session_id()) {

$sql="UPDATE counters SET value = value + 1 WHERE name='".$cname."'";
$result = mysql_query($sql);

CCSetSession(session_id(), session_id());

}

Thus, one database 'hit' per session, vs. 1 per every page-load.

--
DonB

http://www.gotodon.com/ccbth


"Damian Hupfeld" <damian.hupfeld@itng.com.au> wrote in message
news:digbdf$jou$1@news.codecharge.com...
> create a table as follows:
>
> counters
> cid
> name
> value
>
> for each page that you wish counters for create an entry eg home,
products,
> contact
>
> add the code below as custom code substituting
> $cname for the name above
> server for the server name/address
> username for the sql user
> password for the sql user password
> database for the sql database name.
>
> This will only log the visitor once per session per page you are
tracking -
> you cant inflate your counters by hitting refresh.
>
>
> //Custom Code @80-B6E9E578
> // -------------------------
> global $counter;
> // Write your own code here.
> //Change These Values for counter and Database
> //Set counter
> $cname = "home";
>
> //open database
> $conn = mysql_connect(server', 'username', 'password');
> if (!$conn) {
> die('Could not connect: ' . mysql_error());
> }
> mysql_select_db('database');
>
> //Don't Change anything below
> //Connect to sessions
> session_start();
> //Determine if seesion exists
> If (!isset($_SESSION[$cname])) {
> //create the session
> $_SESSION[$cname]="set";
>
> //Query Database for current counter
> $sql="SELECT * FROM counters WHERE name='".$cname."'";
> $result = mysql_query($sql);
> $row = mysql_fetch_assoc($result);
> $count = $row['value'];
> //Increment Counter
> $count = $count + 1;
>
> //Update Database
> $sql="UPDATE counters SET value=".$count." WHERE name='".$cname."'";
> $result = mysql_query($sql);
> }
>
> //End Custom Code
>
>
>
> regards
> Damian Hupfeld
> http://www.nexthost.com.au/services.php
>
>
>
>
>
>

datadoit.com
Posted: 10/11/2005, 4:26 PM

Is this code for Page BeforeShow? AfterInitialize? What's better? Thanks.

-MikeR

DonB
Posted: 10/11/2005, 4:41 PM

Won't matter.

The database connection object is instantiated before any event fires. So
you could bump the counter in any of these page events, OnInitialize,
AfterInitialize or BeforeShow. Of course, if your page has no Grid or
Record control, you'll have to declare a connection object in your code
yourself, as there won't be a database connection built for the page
automatically.
--
DonB

http://www.gotodon.com/ccbth


"datadoit.com" <mike@datadoit.com> wrote in message
news:dihhms$5l0$1@news.codecharge.com...
> Is this code for Page BeforeShow? AfterInitialize? What's better?
Thanks.
>
> -MikeR
>
>

datadoit.com
Posted: 10/13/2005, 6:21 AM

Seems there ought to be a way to take this wonderful idea and condense into
a single function within the Common file, so that it doesn't have to be
'inserted' into every page. Can $FileName be retrieved within the Common
file?


Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.