Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/21048-solvedproblem-with-click-count-script/
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]
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.