Jump to content

Archived

This topic is now archived and is closed to further replies.

wwfc_barmy_army

[Solved]Problem with click count script

Recommended Posts

Hello.

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

[quote]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[/quote]


So i came here and read the sticky at the top of the forum, which says:
[quote]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.[/quote]

But i'm not sure how i could rearrange this as then it wouldn't run the query:
[CODE]<?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
?>  [/code]

Any help/advice would be great.

Thanks.

Peter.

Share this post


Link to post
Share on other sites
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.

[code]<?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();
?>[/code]

Share this post


Link to post
Share on other sites
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:

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

So i think it's something to do with the id, the link being used is (for example):
[quote]/includes/clicks.php?url=www.google.com&id=10[/quote]

Can anyone help?

Thanks.

Peter.

Share this post


Link to post
Share on other sites
[code]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']}')";[/code]

Share this post


Link to post
Share on other sites
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.
[code]
ob_start();
ob_end_flash();
[/code]

good luck.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.