Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 10-25-2007, 11:03 AM PHP counter.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
okay, i had a bash at making a simple PHP counter and it erm didnt work.

okay heres what i have.

PHP Code:
<?php
include('includes/includes.php');
$ip $_SERVER['REMOTE_ADDR'];
$useR_agent $_SERVER['HTTP_USER_AGENT'];
$result mysql_query("SELECT * FROM counter WHERE ip='$ip'") or die("ERROR select check if exist".mysql_error());
$user_count mysql_fetch_array($result);
$row_num mysql_num_rows($result);
if(
$row_num 1)
{
$user_count $user_count['visit_count']++;
mysql_query("UPDATE counter SET visit_count='$user_count' WHERE ip='$ip'") or die("ERROR updateing".mysql_error());

$result_count mysql_query("SELECT COUNT(*) FROM counter") or die("ERROR select count 1".mysql_error());
$count=mysql_result($result_count0);
echo 
$count;
}

else {
mysql_query("INSERT INTO counter SET ip='$ip',visit_count='1',user_id='0',user_agent='$user_agent'") or die("ERROR inserting".mysql_error());
$result_count mysql_query("SELECT COUNT(*) FROM counter") or die("ERROR select count 2".mysql_error());
$count=mysql_result($result_count0);
echo 
$count;
}
?>
the first include has the database connection...

basically the idea is on load it checks if theres a entry for ip if yes increments
"visit_count" by 1 and echos the total unique visitors.

if there isnt a entry it inserts the users ip and their user agent info (to try and help me see the stats for visitors.) and gives them a visit_count of 1

and then echos total unique visits.

how can i better do this?

i would like it so like many counters it isnt effective if a user sits and refreshes the page

i would like it so it only counts and adds one to users visit count for each time of opening the site if you get me?

i have just confused myself now yet alone you lot!

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 10-25-2007, 11:17 AM Re: PHP counter.
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
I think this should do the trick.
Haven't tested it though.


Code:
<?php
session_start();
include('includes/includes.php');

$ip = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];

//Session check!
if(!$_SESSION['added']){
    $result = mysql_query("SELECT * FROM counter WHERE ip='$ip'") or die("ERROR select check if exist".mysql_error());
    $user_count = mysql_fetch_array($result);
    if(mysql_num_rows){
        $user_count = $user_count['visit_count']+1;
        mysql_query("UPDATE counter SET visit_count='$user_count' WHERE ip='$ip'") or die("ERROR updateing".mysql_error());
    }else{
        mysql_query("INSERT INTO counter VALUES('$ip','1','0','$user_agent'") or die("ERROR inserting".mysql_error());
    }
    $_SESSION['added'] = 1;
}
$result_count = mysql_query("SELECT COUNT(*) FROM counter") or die("ERROR select count 2".mysql_error());
echo mysql_result($result_count, 0);
?>
Why isn't my code tag working properly? :/
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>

Last edited by Insensus; 10-25-2007 at 11:18 AM..
Insensus is offline
Reply With Quote
View Public Profile
 
Old 10-25-2007, 11:28 AM Re: PHP counter.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
nope

sorry i should have said it dont even seem to be talking to the database at all other than to retrieve the count. and im completely lost as to why!

Anyone?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-25-2007, 11:30 AM Re: PHP counter.
hamesy's Avatar
Webmaster Talker

Posts: 639
Name: Steve
Location: Birmingham, England
Trades: 0
You checked to see if your field names are correct?
__________________
Hamesy

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
hamesy is offline
Reply With Quote
View Public Profile
 
Old 10-25-2007, 11:33 AM Re: PHP counter.
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
In your first script there's at least one mistake:
if($row_num = 1)
should of course be
if($row_num == 1)

Maybe try your script again with this correction?
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 10-25-2007, 11:38 AM Re: PHP counter.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
god i love you guy! XD its ALMOST fixed.

PHP Code:
<?php
include('includes/includes.php');
$ip $_SERVER['REMOTE_ADDR'];
$user_agent $_SERVER['HTTP_USER_AGENT'];
$result mysql_query("SELECT * FROM counter WHERE ip='$ip'") or die("ERROR select check if exist".mysql_error());
$user_count mysql_fetch_array($result);
$row_num mysql_num_rows($result);
if(
$row_num == 1)
{
$user_count $user_count['visit_count']++;
mysql_query("UPDATE counter SET visit_count='$user_count' WHERE ip='$ip'") or die("ERROR updateing".mysql_error());
$result_count mysql_query("SELECT COUNT(*) FROM counter") or die("ERROR select count 1".mysql_error());
$count=mysql_result($result_count0);
echo 
$count;
}

else {
mysql_query("INSERT INTO counter SET ip='$ip',visit_count='1',user_id='0',user_agent='$user_agent'") or die("ERROR inserting".mysql_error());
$result_count mysql_query("SELECT COUNT(*) FROM counter") or die("ERROR select count 2".mysql_error());
$count=mysql_result($result_count0);
echo 
$count;
}
?>
okay for some reason the visit_count aint incrementing. why and how do i fix it

btw ill give ya tp
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-25-2007, 11:45 AM Re: PHP counter.
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
I already thought this was happening.
You have entered $user_count = $user_count['visit_count']++;
'++' is somewhat a function and doesn't mean the same as +1.
You can place it in front of or behind a variable.
The difference is the following:

$a = 1;
$b = $a++;
// $a is now 2;
// $b is now 1;

$a = 1;
$b = ++$a;
// $a is now 2;
// $b is now 2;
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 10-25-2007, 02:15 PM Re: PHP counter.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
okay thanks my mistake obviously but i have always used $var++ to add one...
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-25-2007, 02:28 PM Re: PHP counter.
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Correct.
But it reads from left to right.
First it sets $b to $a and then it adds 1 to $a. (In my example)
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 10-25-2007, 02:57 PM Re: PHP counter.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
im confsued XD
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-25-2007, 03:34 PM Re: PHP counter.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Insensus View Post
Correct.
But it reads from left to right.
First it sets $b to $a and then it adds 1 to $a. (In my example)
I don't think that's true. Anytime an expression is evaluated (at least in any language I've ever used), it evaluates right to left. In your example $b is set to the value of $a++, meaning that $a++ is evaluated first and then $b gets that value.

And Dan...

I think this query might work better in your case:

Code:
//Your current query
"UPDATE counter SET visit_count='$user_count' WHERE ip='$ip'"

//This query should simplify your code
"UPDATE counter SET visit_count=visit_count+1 WHERE ip='$ip'"
Hope that helps
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-25-2007, 11:01 PM Re: PHP counter.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
i didnt realise that will work.

ill have to remember to test that in the erm later morning (its 4.01AM here)
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-26-2007, 03:06 AM Re: PHP counter.
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
It is true.
I just tested it on my server.

Code:
<?php
$a = 1;
$b = $a++;
echo "\$a: $a<br />\$b: $b";
?>
http://www.praediniuslustrum.nl/test5.php
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP counter.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.37355 seconds with 12 queries