Jump to content

My counter is counting even when $_SESSIONS isset


jmr3460

Recommended Posts

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.

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?

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());

Archived

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

×
×
  • 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.