wwfc_barmy_army Posted September 17, 2006 Share Posted September 17, 2006 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 3Warning: 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. Quote Link to comment https://forums.phpfreaks.com/topic/21048-solvedproblem-with-click-count-script/ Share on other sites More sharing options...
Daniel0 Posted September 17, 2006 Share Posted September 17, 2006 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]<?phpob_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] Quote Link to comment https://forums.phpfreaks.com/topic/21048-solvedproblem-with-click-count-script/#findComment-93425 Share on other sites More sharing options...
wwfc_barmy_army Posted September 17, 2006 Author Share Posted September 17, 2006 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 5UPDATE 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. Quote Link to comment https://forums.phpfreaks.com/topic/21048-solvedproblem-with-click-count-script/#findComment-93428 Share on other sites More sharing options...
Daniel0 Posted September 17, 2006 Share Posted September 17, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/21048-solvedproblem-with-click-count-script/#findComment-93435 Share on other sites More sharing options...
wwfc_barmy_army Posted September 17, 2006 Author Share Posted September 17, 2006 Thanks!!! Works great now!Thanks for your help!Pete. Quote Link to comment https://forums.phpfreaks.com/topic/21048-solvedproblem-with-click-count-script/#findComment-93438 Share on other sites More sharing options...
redarrow Posted September 17, 2006 Share Posted September 17, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/21048-solvedproblem-with-click-count-script/#findComment-93457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.