jmr3460 Posted June 16, 2009 Share Posted June 16, 2009 Why do I have this notice and why does my cookie not set? Undefined variable count in URL line 111 <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); include("db_in.php"); $table = "home"; $ip = getenv('REMOTE_ADDR'); $date = date("M-d-Y h:i:s", time()+3600); $browser = $_SERVER['HTTP_USER_AGENT']; $result = "false"; $bots = array("Robozilla", "SurveyBot", "W3C_Validator", "VoilaBot", "spider", "Slurp", "Yandex", "LinkWalker", "Googlebot", "Exabot", "Java", "msnbot", "Crowler", "GT::WWW"); foreach ($bots as $bot){ $botlist = "/$bot/i";} if (preg_match($botlist, $browser)){$result = "true";} else if ($result == "true"){ if (!isset($_COOKIE['clean'])) { setcookie("clean","cleantime",time()+7200,"/"); mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); mysql_query("INSERT INTO $table values('id','$ip','$date','$browser')") or die(mysql_error()); $data = mysql_query("SELECT id FROM $table ORDER BY id DESC LIMIT 1;"); $info = mysql_fetch_array($data); //Does this not define $count $count = $info['id']; } else { mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); $data = mysql_query("SELECT id FROM $table ORDER BY id DESC LIMIT 1;"); $info = mysql_fetch_array($data); $count = $info['id']; }} ?> //Html Document Starts //Why is this $count undefined <?php echo "No: " . $count; ?> <!-- end #footer --></div> <!-- end #mainContent --></div> <!-- end #container --></div> Link to comment https://forums.phpfreaks.com/topic/162324-solved-undefined-variable/ Share on other sites More sharing options...
haku Posted June 16, 2009 Share Posted June 16, 2009 You are a lot more likely to get an answer if you aren't so vague. I couldn't be bothered to try to look through your code to decipher exactly what your problem is, since you couldn't be bothered to explain it, but throwing out a stab in the dark from your comments, cookies are not available until after a page refresh, so if you are trying to pull out a cookies value on the same page you set it, it's not going to work. Link to comment https://forums.phpfreaks.com/topic/162324-solved-undefined-variable/#findComment-856762 Share on other sites More sharing options...
jmr3460 Posted June 16, 2009 Author Share Posted June 16, 2009 OK I have a counter that works except it also counts any hit. I added this bit to try and disregard search engines. $result = "false"; $bots = array("Robozilla", "SurveyBot", "W3C_Validator", "VoilaBot", "spider", "Slurp", "Yandex", "LinkWalker", "Googlebot", "Exabot", "Java", "msnbot", "Crowler", "GT::WWW"); foreach ($bots as $bot){ $botlist = "/$bot/i";} if (preg_match($botlist, $browser)){$result = "true";} else if ($result == "true"){ I read about preg_match today and thought this might work. (I got some of the code from a previous posted solution.) The cookie sets until I insert this bit of code. Also when I inserted this code a variable that echoed before is now undefined at the bottom of the document. Link to comment https://forums.phpfreaks.com/topic/162324-solved-undefined-variable/#findComment-856771 Share on other sites More sharing options...
jmr3460 Posted June 16, 2009 Author Share Posted June 16, 2009 I was trying to replace this bit of code. I haven't gotten any search engine counts in over an hour. This is my counter: <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); include("db_in.php"); $table = "home"; $ip = getenv('REMOTE_ADDR'); $date = date("M-d-Y h:i:s", time()+3600); $browser = $_SERVER['HTTP_USER_AGENT']; //Was trying to shorten starts here if (preg_match("/Robozilla/i",$browser)){ exit();} else if (preg_match("/SurveyBot/i",$browser)){ exit();} else if (preg_match("/W3C_Validator/i",$browser)){ exit();} else if (preg_match("/VoilaBot/i",$browser)){ exit();} else if (preg_match("/spider/i",$browser)){ exit();} else if (preg_match("/Slurp/i",$browser)){ exit();} else if (preg_match("/Yandex/i",$browser)){ exit();} else if (preg_match("/LinkWalker/i",$browser)){ exit();} else if (preg_match("/Googlebot/i",$browser)){ exit();} else if (preg_match("/Exabot/i",$browser)){ exit();} else if (preg_match("/Java/i",$browser)){ exit();} else if (preg_match("/msnbot/i",$browser)){ exit();} else if (preg_match("/Crawler/i",$browser)){ exit();} else if(!isset($_COOKIE['clean'])) { //Ends here setcookie("clean","cleantime",time()+7200,"/"); mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); mysql_query("INSERT INTO $table values('id','$ip','$date','$browser')") or die(mysql_error()); $data = mysql_query("SELECT id FROM $table ORDER BY id DESC LIMIT 1;"); $info = mysql_fetch_array($data); $count = $info['id']; } else { mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); $data = mysql_query("SELECT id FROM $table ORDER BY id DESC LIMIT 1;"); $info = mysql_fetch_array($data); $count = $info['id']; } ?> //start html <?php echo $count; ?> //end html I will see how many are their tomorrow. Link to comment https://forums.phpfreaks.com/topic/162324-solved-undefined-variable/#findComment-856813 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.