Jump to content


Photo

[Solved]Problem with click count script


  • Please log in to reply
5 replies to this topic

#1 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 17 September 2006 - 09:34 AM

Hello.

I am trying to use a clicks counter script on my site but I got a header error:

Notice: Undefined variable: id in C:\public_html\RPG\includes\clicks.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at C:\public_html\RPG\includes\clicks.php:3) in C:\public_html\RPG\includes\clicks.php on line 6



So i came here and read the sticky at the top of the forum, which says:

ive seen this error pop up time and time again, and the answer is ALWAYS the same.  please look here before you post it.  the problem is you are outputting to the browser (whitespace included) before sending a header.  this is unallowed.  remove output prior to the header, or use OUTPUT BUFFERING.


But i'm not sure how i could rearrange this as then it wouldn't run the query:
<?php include("dbconnect.php"); ?>
<?php 
// clickcount is the table, with 2 columns: id and clicks
 $sql="UPDATE site SET clicks=clicks+1 WHERE (id = '$id')";
 $result=mysql_query($sql,$dbcnx);
 $url = $_GET['url'];
 header("Location: http://$url");  /* Redirect browser to web site */
 exit; // just to be nice 
?> 

Any help/advice would be great.

Thanks.

Peter.

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 17 September 2006 - 09:38 AM

The solution to your problem is output control.
By using ob_start() all the output you send will be stored in a buffer, that means it don't actually get output yet. Then you can output something and run the header function, since it weren't really output. ob_end_flush() outputs what was buffered, ends the output buffering and clears the output buffer.

<?php
ob_start();
include("dbconnect.php");
// clickcount is the table, with 2 columns: id and clicks
$sql="UPDATE site SET clicks=clicks+1 WHERE (id = '$id')";
$result=mysql_query($sql,$dbcnx);
$url = $_GET['url'];
header("Location: http://$url");  /* Redirect browser to web site */
exit; // just to be nice 
ob_end_flush();
?>


#3 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 17 September 2006 - 09:52 AM

Thanks!!

One problem though, it doesn't seem to be adding 1 to the database.

So i tried using print sql after the query and i get this:

Notice: Undefined variable: id in C:\public_html\RPG\includes\clicks.php on line 5
UPDATE site SET clicks=clicks+1 WHERE (id = '')


So i think it's something to do with the id, the link being used is (for example):

/includes/clicks.php?url=www.google.com&id=10


Can anyone help?

Thanks.

Peter.

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 17 September 2006 - 10:04 AM

Change [code]$sql="UPDATE site SET clicks=clicks+1 WHERE (id = '$id')";[/code] to $sql="UPDATE site SET clicks=clicks+1 WHERE (id = '{$_GET['id']}')";


#5 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 17 September 2006 - 10:08 AM

Thanks!!! Works great now!

Thanks for your help!

Pete.

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 17 September 2006 - 12:47 PM

please also remember that these two php code statements are not a solution to get out of trouble if it is broke fix it if it not broke dont fix it ok.
ob_start();
ob_end_flash();

good luck.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users