Jump to content

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

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.