jmr3460 Posted January 30, 2010 Share Posted January 30, 2010 Hello All, I am trying to get my counter script working. The $count is inserting into DB and setting $SESSIONS correctly. What seems to be happening is even when $_SESSIONS are set it should only retrieve the existing count in DB without increasing the $count value. I am trying to count only one for each page for each session. I have three files I am working with. Can someone tell me what I am not seeing? This is count.php: <?php $table = 'counter'; $browser = $_SERVER['HTTP_USER_AGENT']; $date = date("M-d-Y G:i:s", time()+3600); $bots = array("/Robozilla/i","/SurveyBot/i","/W3C_Validator/i","/VoilaBot/i","/spider/i", "/Slurp/i","/Yandex/i","/LinkWalker/i","/Googlebot/i","/Exabot/i","/Java/i", "/msnbot/i","/Crawler/i","/Bot/i","/oozbot/i","/nutch/i","/Xenu/i","/Wget/i","/Jeeves/i"); foreach($bots as $bot){ if(preg_match($bot, $browser)){exit();} }//This ends browser check //This starts count check if(isset($_SESSIONS["$page"])){ //This if $_SESSIONS['count'] is set should check page_name for $_SESSIONS if($_SESSIONS["$page"] == $page){ mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); $sql = "SELECT * FROM $table WHERE page_name = '$page'"; $query = mysql_query($sql) or trigger_error(mysql_error()); $result = mysql_fetch_array($query); $count = $result['count']; } else { $_SESSIONS["$page"] = $page; mysql_connect($host, $user, $pass); mysql_select_db($database) or trigger_error(mysql_error()); $sql = "SELECT * FROM $table WHERE page_name = '$page'"; $get_count = mysql_query($sql) or trigger_error(mysql_error()); $result_count = mysql_fetch_array($get_count); $pre_count = $result_count['count']; $add_count = $pre_count + 1; $add_sql = "UPDATE $table SET count = '$add_count' WHERE page_name = '$page'"; $add_query = mysql_query($add_sql) or trigger_error(mysql_error()); $count = $add_count; echo "First else"; } } else{ //This should set $_SESSIONS['count'] if not set //and add one to DB $_SESSIONS["$page"] = $page; mysql_connect($host, $user, $pass); mysql_select_db($database) or trigger_error(mysql_error()); $sql = "SELECT * FROM $table WHERE page_name = '$page'"; $get_count = mysql_query($sql) or trigger_error(mysql_error()); $result_count = mysql_fetch_array($get_count); $pre_count = $result_count['count']; $add_count = $pre_count + 1; $add_sql = "UPDATE $table SET count = '$add_count' WHERE page_name = '$page'"; $add_query = mysql_query($add_sql) or trigger_error(mysql_error()); $count = $add_count; echo "Second else"; } ?> These files are what I want to insert on any page that I want to be counted: <?php session_start(); $page = 'count1'; include('in_db.php'); include('count.php'); echo "Number: ".$count."<br />".$browser; if(isset($_SESSIONS["$page"])){ echo "<br />".$_SESSIONS["$page"]; } ?> <?php session_start(); $page = 'count2'; include('in_db.php'); include('count.php'); echo "Number: ".$count."<br />".$browser; if(isset($_SESSIONS["$page"])){ echo "<br />".$_SESSIONS["$page"]; } ?> Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/190368-my-counter-is-counting-even-when-_sessions-isset/ Share on other sites More sharing options...
jmr3460 Posted January 30, 2010 Author Share Posted January 30, 2010 Doing some more checking I think that that the issue is happening where I am checking if my $_SESSIONS["$page"] is set. if(isset($_SESSIONS["$page"])){ //do something here} Is this the right way to check if sessions are set? Link to comment https://forums.phpfreaks.com/topic/190368-my-counter-is-counting-even-when-_sessions-isset/#findComment-1004314 Share on other sites More sharing options...
jl5501 Posted January 30, 2010 Share Posted January 30, 2010 What is $_SESSIONS?? Do you mean $_SESSION Link to comment https://forums.phpfreaks.com/topic/190368-my-counter-is-counting-even-when-_sessions-isset/#findComment-1004320 Share on other sites More sharing options...
jmr3460 Posted January 30, 2010 Author Share Posted January 30, 2010 THANK YOU Link to comment https://forums.phpfreaks.com/topic/190368-my-counter-is-counting-even-when-_sessions-isset/#findComment-1004327 Share on other sites More sharing options...
wildteen88 Posted January 30, 2010 Share Posted January 30, 2010 Also note that this code $sql = "SELECT * FROM $table WHERE page_name = '$page'"; $get_count = mysql_query($sql) or trigger_error(mysql_error()); $result_count = mysql_fetch_array($get_count); $pre_count = $result_count['count']; $add_count = $pre_count + 1; $add_sql = "UPDATE $table SET count = '$add_count' WHERE page_name = '$page'"; $add_query = mysql_query($add_sql) or trigger_error(mysql_error()); $count = $add_count; can be written as just two lines $add_sql = "UPDATE $table SET count = count+1 WHERE page_name = '$page'"; $add_query = mysql_query($add_sql) or trigger_error(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/190368-my-counter-is-counting-even-when-_sessions-isset/#findComment-1004345 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.