Jump to content

[SOLVED] having trouble with the sum of two variables. Please help!


eon201

Recommended Posts

Hey,

 

First off. My name is eon201 and I am a new/noob member!

 

So my problem is this.

 

Im creating a small stats counter (god knows why after all the trouble it ahs caused me!) for my site, and getting all of the relevant data from the server log files. Getting the data isnt a problem, and slicing it up is fine but.. Ive created a rule that is trying to emulate a google analytics feature (the feature will count a visitor twice if they return after a certain amount of minutes even if its the same ip), but the code doesnt work. There are loads of different ip's in the log so I just cant understand why the code is interpreting just one ip.

 

Here's the code.

			$bits = explode(" ", $value);
			// CLIENT IP
			// $bits[9]
			$log_clientIP[] = $bits[9];
			// CLIENT REQUEST
			// $bits[5]
			$log_request[] = $bits[5];

						// Now sort out time range return
						$hour = substr($value, 11, 2);
						$hour = $hour *60;
						$minute = substr($value, 14, 2);
						$minutes = $minute + $hour; // These are the minutes for this line
						echo "$minutes - The time (as minutes)<br/>"; //echo the minutes for this day.
						echo "$bits[1] The time<br/>";

						$uniqueip[] = array_unique($log_clientIP);

					if ($log_clientIP = $uniqueip){	
						echo "This is a unique ip<br/><br/>";
						// Then count this as a new visitor and an addittion to log_request

						}
					elseif ($log_clientIP != $uniqueip){
						$mylogtime = $minutes[$temp -1] - $minutes;
						echo "$mylogtime = The sum of this time loop - the last time loop<br/><br/><br/>";

							if ($mylogtime >=7){
							// Then count this as a (returning but new) visitor and an addittion to log_request
							}
							else{}

 

Heres (a slice of) the output.

 

1410 - The time (as minutes)
23:30:27 The time
This is a unique ip

1410 - The time (as minutes)
23:30:25 The time
This is a unique ip

1383 - The time (as minutes)
23:03:39 The time
This is a unique ip

1368 - The time (as minutes)
22:48:45 The time
This is a unique ip

1367 - The time (as minutes)
22:47:05 The time
This is a unique ip

1329 - The time (as minutes)
22:09:07 The time
This is a unique ip

1328 - The time (as minutes)
22:08:43 The time
This is a unique ip

1322 - The time (as minutes)
22:02:35 The time
This is a unique ip

1315 - The time (as minutes)
21:55:58 The time
This is a unique ip

1311 - The time (as minutes)
21:51:24 The time
This is a unique ip

1308 - The time (as minutes)
21:48:50 The time
This is a unique ip

1277 - The time (as minutes)
21:17:16 The time
This is a unique ip

1265 - The time (as minutes)
21:05:07 The time
This is a unique ip

1251 - The time (as minutes)
20:51:34 The time
This is a unique ip

1250 - The time (as minutes)
20:50:20 The time
This is a unique ip

 

As you can see it doesnt seem to get to my elseif statement. Why?? Can someone please help. Ive been sat for ten hours today trying to solve this mother!

 

ps. the code is in a foreach loop statement. But I thought it was unessecary to include it.

Link to comment
Share on other sites

Ok ive done that.

 

It now outputs..

 

1410 - The time (as minutes)
23:30:27 The time
-1410 = The sum of this time loop - the last time loop


1410 - The time (as minutes)
23:30:25 The time
-1410 = The sum of this time loop - the last time loop


1383 - The time (as minutes)
23:03:39 The time
-1383 = The sum of this time loop - the last time loop


1368 - The time (as minutes)
22:48:45 The time
-1368 = The sum of this time loop - the last time loop


1367 - The time (as minutes)
22:47:05 The time
-1367 = The sum of this time loop - the last time loop


1329 - The time (as minutes)
22:09:07 The time
-1329 = The sum of this time loop - the last time loop


1328 - The time (as minutes)
22:08:43 The time
-1328 = The sum of this time loop - the last time loop

 

Which means its not doing this sum now

$minutes[$temp -1] - $minutes

but this instead??

$minutes -$minutes - $minutes

 

Very strange. ???

Link to comment
Share on other sites

You're trying to use $minutes as both a scalar and an array at the same time here:

<?php
$mylogtime = $minutes[$temp -1] - $minutes;
?>

 

That is not a good practice.

 

Where are you storing the previous contents of "$minutes"?

 

Also, the code

<?php
elseif ($log_clientIP != $uniqueip){
?>

can just be written as

<?php
else {
?>

 

Ken

Link to comment
Share on other sites

Ok. Thanks kenrbnsn.

 

Yer I thought I could use the else statement, just been trying soooo many different ways to get this to work!

 

The $minutes comes straight from the log file which looks like this for the first bunch of characters...

2007-11-11 00:00:38 W3SVC10019

I used

substr

function to grab the hours and minutes. I then multiplied the hours by 60 to turn them into minutes then added both the $hours and $minutes variables to get the overall amount of minutes (I hope that makes sense!).

 

The previous contents of $minutes ($minutes[$temp -1]) I thought was created by the

foreach

statement that this is all nested in as it looped through??

 

Am I massivly wrong here? ???

 

ps. Just to stop confusion here's the whole script.

<?php set_time_limit(30);
// Run this script every 15 minutes!
// Do workings for time to be parsed

// What time was the script last run?
$timefile = 'time.txt';
$timeopen = fopen($timefile, "r");						
$thentiming = fread($timeopen, filesize($timefile));
fclose($timeopen);

// whats the time Mr Wolf?
$nowtiming = date('Gis');

//whats the time difference
$timedifference = $nowtiming - $thentiming;

// Now update last run time in file
$timefile2 = 'time.txt';
$timeopen2 = fopen($timefile, "w");						
fwrite($timeopen2, $nowtiming);
fclose($timeopen2);

$filename = 'logs/'. 'ex'. date('ymd'). '.log'; // Filename Checker
		// D:\home\Planet Cruise Sites\planetcruise.co.uk\logs\W3SVC10019\
if (file_exists($filename)) // Does the corresponding filename exist - if yes then
{
	$fp = fopen($filename, "r"); //Open the server log
        $content = fread($fp, filesize($filename));	 // Read the server log	
	$content = explode("\n", $content); // explode into array	
	$content  = array_reverse($content ); // reverse the array

	foreach($content as $temp=>$value)	{

		$findme   = 'Googlebot';
		$googlebot = strpos($value, $findme);

		$findme1   = 'picsearch.com/bot';
		$picsearch = strpos($value, $findme1);

		$findme2   = '/sys-includes';
		$sysincludes = strpos($value, $findme2);

		$findme3   = '/offers/Scat';
		$offersscat = strpos($value, $findme3);

		$findme4   = '/revelex';
		$revelex = strpos($value, $findme4);

		$findme5   = '.gif';
		$gif = strpos($value, $findme5);

		$findme6   = '.jpg';
		$jpg = strpos($value, $findme6);

		$findme7   = '.txt';
		$txt = strpos($value, $findme7);

		$findme8   = '.swf';
		$swf = strpos($value, $findme8);

		$findme9   = '.dll';
		$dll = strpos($value, $findme9);

		$findme10   = '.asp';
		$asp = strpos($value, $findme10);

		$findme11   = '.png';
		$png = strpos($value, $findme11);

		$findme12   = '.css';
		$css = strpos($value, $findme12);

		$findme13   = '.ico';
		$ico = strpos($value, $findme13);

		$findme14   = '.js';
		$js = strpos($value, $findme14);

		$findme15   = '- 200';
		$error = strpos($value, $findme15);

		$findme16   = 'msnbot';
		$msnbot = strpos($value, $findme16);

		$findme17   = 'gigablast.com/spider.html';
		$gigaspider = strpos($value, $findme17);

		$findme18   = 'InternetSeer.com';
		$internetSeer = strpos($value, $findme18);

		$findme19   = '+Slurp';
		$yahoo = strpos($value, $findme19);

		$findme20   = '....../1.0+';
		$msnsneakbot = strpos($value, $findme20);

		$firstcharacter = substr($value, 0, 1); //strip lines beginning with #/null
		$weberror = substr($value, -18, 3); //strip lines beginning with 400/401/403/404/503


	if($googlebot === false && $picsearch === false && $sysincludes === false && $offersscat === false && $revelex === false && $firstcharacter != "#" && $firstcharacter != "" && $weberror != "400" && $weberror != "401" && $weberror != "403" && $weberror != "404" && $weberror != "503" && $gif === false && $jpg === false && $txt === false && $swf === false && $dll === false && $asp === false && $png === false && $css === false && $ico === false && $js === false && $error == true && $msnbot === false && $gigaspider === false && $internetSeer === false && $yahoo === false && $msnsneakbot === false)
			{
			//echo "$value<br/><br/>";
			$bits = explode(" ", $value);
			// CLIENT IP
			// $bits[9]
			$log_clientIP[] = $bits[9];
			// CLIENT REQUEST
			// $bits[5]
			$log_request[] = $bits[5];

						// Now sort out time range return
						$hour = substr($value, 11, 2);
						$hour = $hour *60;
						$minute = substr($value, 14, 2);
						$minutes = $minute + $hour; // These are the minutes for this line
						echo "$minutes - The time (as minutes)<br/>"; //echo the minutes for this day.
						echo "$bits[1] The time<br/>";

						$uniqueip[] = array_unique($log_clientIP);

					if ($log_clientIP == $uniqueip){	
						echo "This is a unique ip<br/><br/>";
						// Then count this as a new visitor and an addittion to log_request

						}
					elseif ($log_clientIP != $uniqueip){
						$mylogtime = $minutes[$temp -1] - $minutes;
						echo "$mylogtime = The sum of this time loop - the last time loop<br/><br/><br/>";

							if ($mylogtime >=7){
							// Then count this as a (returning but new) visitor and an addittion to log_request
							}
							else{}

					}// end of elseif statement




			} // end of if stripper statement

			else{}

		} //end of foreach $value command


fclose($fp);			
} //end of original if file exists statement

else {
echo 'This file does not exist!';
	}
?>

 

Link to comment
Share on other sites

I didn't fix anything FYI. I don't totally understand what you're trying to do, but I figured this might help anyone who does - Much prettier code .. :)

 

<?php
set_time_limit (30);

// Run this script every 15 minutes!
// Do workings for time to be parsed

// What time was the script last run?
$timefile       = 'time.txt';
$timeopen       = fopen($timefile, "r");

$thentiming     = fread($timeopen, filesize($timefile));
fclose ($timeopen);

// whats the time Mr Wolf?
$nowtiming      = date('Gis');

//whats the time difference
$timedifference = $nowtiming - $thentiming;

// Now update last run time in file
$timefile2      = 'time.txt';
$timeopen2      = fopen($timefile, "w");

fwrite($timeopen2, $nowtiming);
fclose ($timeopen2);

$filename = 'logs/' . 'ex' . date('ymd') . '.log'; // Filename Checker


// D:\home\Planet Cruise Sites\planetcruise.co.uk\logs\W3SVC10019\

if (file_exists($filename))           // Does the corresponding filename exist - if yes then
{
    $fp      = fopen($filename, "r"); //Open the server log
    $content = fread($fp, filesize($filename));


    // Read the server log

    $content = explode("\n", $content); // explode into array

    $content = array_reverse($content); // reverse the array

    foreach ($content as $temp => $value) {
        $findme         = 'Googlebot';

        $googlebot      = strpos($value, $findme);

        $findme1        = 'picsearch.com/bot';

        $picsearch      = strpos($value, $findme1);

        $findme2        = '/sys-includes';

        $sysincludes    = strpos($value, $findme2);

        $findme3        = '/offers/Scat';

        $offersscat     = strpos($value, $findme3);

        $findme4        = '/revelex';

        $revelex        = strpos($value, $findme4);

        $findme5        = '.gif';

        $gif            = strpos($value, $findme5);

        $findme6        = '.jpg';

        $jpg            = strpos($value, $findme6);

        $findme7        = '.txt';

        $txt            = strpos($value, $findme7);

        $findme8        = '.swf';

        $swf            = strpos($value, $findme8);

        $findme9        = '.dll';

        $dll            = strpos($value, $findme9);

        $findme10       = '.asp';

        $asp            = strpos($value, $findme10);

        $findme11       = '.png';

        $png            = strpos($value, $findme11);

        $findme12       = '.css';

        $css            = strpos($value, $findme12);

        $findme13       = '.ico';

        $ico            = strpos($value, $findme13);

        $findme14       = '.js';

        $js             = strpos($value, $findme14);

        $findme15       = '- 200';

        $error          = strpos($value, $findme15);

        $findme16       = 'msnbot';

        $msnbot         = strpos($value, $findme16);

        $findme17       = 'gigablast.com/spider.html';

        $gigaspider     = strpos($value, $findme17);

        $findme18       = 'InternetSeer.com';

        $internetSeer   = strpos($value, $findme18);

        $findme19       = '+Slurp';

        $yahoo          = strpos($value, $findme19);

        $findme20       = '....../1.0+';

        $msnsneakbot    = strpos($value, $findme20);

        $firstcharacter = substr($value, 0, 1);   //strip lines beginning with #/null

        $weberror       = substr($value, -18, 3); //strip lines beginning with 400/401/403/404/503

        if ($googlebot === false && $picsearch === false && $sysincludes === false && $offersscat === false
            && $revelex === false && $firstcharacter != "#" && $firstcharacter != "" && $weberror != "400"
            && $weberror != "401" && $weberror != "403" && $weberror != "404" && $weberror != "503"
            && $gif === false && $jpg === false && $txt === false && $swf === false && $dll === false
            && $asp === false && $png === false && $css === false && $ico === false && $js === false
            && $error == true && $msnbot === false && $gigaspider === false && $internetSeer === false
            && $yahoo === false && $msnsneakbot === false) {


            //echo "$value<br/><br/>";

            $bits           = explode(" ", $value);


            // CLIENT IP


            // $bits[9]

            $log_clientIP[] = $bits[9];


            // CLIENT REQUEST


            // $bits[5]

            $log_request[]  = $bits[5];


            // Now sort out time range return

            $hour           = substr($value, 11, 2);

            $hour           = $hour * 60;

            $minute         = substr($value, 14, 2);

            $minutes        = $minute + $hour;            // These are the minutes for this line

            echo "$minutes - The time (as minutes)<br/>"; //echo the minutes for this day.

            echo "$bits[1] The time<br/>";

            $uniqueip[]     = array_unique($log_clientIP);

            if ($log_clientIP == $uniqueip) {
                echo "This is a unique ip<br/><br/>";


            // Then count this as a new visitor and an addittion to log_request

            }  elseif ($log_clientIP != $uniqueip) {
                $mylogtime = $minutes[$temp - 1] - $minutes;

                echo "$mylogtime = The sum of this time loop - the last time loop<br/><br/><br/>";

                if ($mylogtime >= 7) {


                // Then count this as a (returning but new) visitor and an addittion to log_request

                }
                else { }
            } // end of elseif statement
        }     // end of if stripper statement
        else { }
    }         //end of foreach $value command

    fclose ($fp);
}             //end of original if file exists statement
else {
    echo 'This file does not exist!';
}
?>

Link to comment
Share on other sites

I havnt really explained what I am trying to do well enough here.

 

Let me explain step by step what im trying to do!

 

Search for a specific file name. (We are working with a server log file)

Does it exist? Yes.Continue.    No.Error

Open the file

Read the file

Explode it into seperate lines

Reverse it (therefore getting the most recent data at the top)

Create a loop

state all the bad stuff that i want the code to find and if it does ignore the line of data

Get variables for the client ip's and client url requested

Get the time for hours and minutes and convert to just minutes from log

 

if the ip is unique; then output the url and ip (which i will later send to mysql)

if the ip is not unique but the time difference between the last visit to this page from the same ip was moe than 7 minutes ago; then output the url and ip (which i will later send to mysql).

 

And thats about it!

 

I know its weird. And it fries my brain. Especially when its my first project.

 

I hope this makes things more clear!

 

Thankyou very very much for the help so far!

 

 

Link to comment
Share on other sites

Ok. so I have managed to get this far now...

My script is now in this state..

<?php set_time_limit(30);
// Run this script every 15 minutes!

$filename = 'logs/'. 'ex'. date('ymd'). '.log'; // Filename Checker
		// D:\home\Planet Cruise Sites\planetcruise.co.uk\logs\W3SVC10019\
if (file_exists($filename)) // Does the corresponding filename exist - if yes then
{
	$fp = fopen($filename, "r"); //Open the server log
        $content = fread($fp, filesize($filename));	 // Read the server log	
	$content = explode("\n", $content); // explode into array	
	$content  = array_reverse($content ); // reverse the array

	foreach($content as $temp=>$value)	{

		$findme   = 'Googlebot';
		$googlebot = strpos($value, $findme);

		$findme1   = 'picsearch.com/bot';
		$picsearch = strpos($value, $findme1);

		$findme2   = '/sys-includes';
		$sysincludes = strpos($value, $findme2);

		$findme3   = '/offers/Scat';
		$offersscat = strpos($value, $findme3);

		$findme4   = '/revelex';
		$revelex = strpos($value, $findme4);

		$findme5   = '.gif';
		$gif = strpos($value, $findme5);

		$findme6   = '.jpg';
		$jpg = strpos($value, $findme6);

		$findme7   = '.txt';
		$txt = strpos($value, $findme7);

		$findme8   = '.swf';
		$swf = strpos($value, $findme8);

		$findme9   = '.dll';
		$dll = strpos($value, $findme9);

		$findme10   = '.asp';
		$asp = strpos($value, $findme10);

		$findme11   = '.png';
		$png = strpos($value, $findme11);

		$findme12   = '.css';
		$css = strpos($value, $findme12);

		$findme13   = '.ico';
		$ico = strpos($value, $findme13);

		$findme14   = '.js';
		$js = strpos($value, $findme14);

		$findme15   = '- 200';
		$error = strpos($value, $findme15);

		$findme16   = 'msnbot';
		$msnbot = strpos($value, $findme16);

		$findme17   = 'gigablast.com/spider.html';
		$gigaspider = strpos($value, $findme17);

		$findme18   = 'InternetSeer.com';
		$internetSeer = strpos($value, $findme18);

		$findme19   = '+Slurp';
		$yahoo = strpos($value, $findme19);

		$findme20   = '....../1.0+';
		$msnsneakbot = strpos($value, $findme20);

		$firstcharacter = substr($value, 0, 1); //strip lines beginning with #/null
		$weberror = substr($value, -18, 3); //strip lines beginning with 400/401/403/404/503


	if($googlebot === false && $picsearch === false && $sysincludes === false && $offersscat === false && $revelex === false && $firstcharacter != "#" && $firstcharacter != "" && $weberror != "400" && $weberror != "401" && $weberror != "403" && $weberror != "404" && $weberror != "503" && $gif === false && $jpg === false && $txt === false && $swf === false && $dll === false && $asp === false && $png === false && $css === false && $ico === false && $js === false && $error == true && $msnbot === false && $gigaspider === false && $internetSeer === false && $yahoo === false && $msnsneakbot === false)
			{
			//echo "$value<br/><br/>";
			$bits = explode(" ", $value);
			// CLIENT IP
			// $bits[9]
			$log_clientIP[] = $bits[9];
			// CLIENT REQUEST
			// $bits[5]
			$log_request[] = $bits[5];

						// Now sort out time range return
						$hour = substr($value, 11, 2);
						$hour = $hour *60;
						$minute = substr($value, 14, 2);
						$minutes = $minute + $hour; // These are the minutes for this line
						echo "$minutes - The time (as minutes)<br/>"; //echo the minutes for this day.
						echo "$bits[1] The time<br/>";

						$uniqueip[] = array_unique($log_clientIP);

					if ($log_clientIP == $uniqueip){	
						echo "This is a unique ip<br/><br/>";
						// Then count this as a new visitor and an addittion to log_request

						}
					elseif ($log_clientIP != $uniqueip){
						$previouskey[] = $temp[-1];
						$lastminutes = $previouskey[1];

						$mylogtime = $lastminutes - $minutes;
						echo "$mylogtime = The sum of this time loop - the last time loop<br/><br/><br/>";

							if ($mylogtime >=7){
							// Then count this as a (returning but new) visitor and an addittion to log_request
							}
							else{}

					}// end of elseif statement




			} // end of if stripper statement

			else{}

		} //end of foreach $value command


fclose($fp);			
} //end of original if file exists statement

else {
echo 'This file does not exist!';
	}
?>

 

but the output is this...

1410 - The time (as minutes)
23:30:27 The time

Notice: Undefined offset: 1 in D:\home\Default\planetcruise-svr.co.uk\htdocs\pcm\logreader3.php on line 145
-1410 = The sum of this time loop - the last time loop


1410 - The time (as minutes)
23:30:25 The time
-1410 = The sum of this time loop - the last time loop


1383 - The time (as minutes)
23:03:39 The time
-1383 = The sum of this time loop - the last time loop


1368 - The time (as minutes)
22:48:45 The time
-1368 = The sum of this time loop - the last time loop


1367 - The time (as minutes)
22:47:05 The time
-1367 = The sum of this time loop - the last time loop


1329 - The time (as minutes)
22:09:07 The time
-1329 = The sum of this time loop - the last time loop


1328 - The time (as minutes)
22:08:43 The time
-1328 = The sum of this time loop - the last time loop


1322 - The time (as minutes)
22:02:35 The time
-1322 = The sum of this time loop - the last time loop


1315 - The time (as minutes)
21:55:58 The time
-1315 = The sum of this time loop - the last time loop


1311 - The time (as minutes)
21:51:24 The time
-1311 = The sum of this time loop - the last time loop


1308 - The time (as minutes)
21:48:50 The time
-1308 = The sum of this time loop - the last time loop


1277 - The time (as minutes)
21:17:16 The time
-1277 = The sum of this time loop - the last time loop


1265 - The time (as minutes)
21:05:07 The time
-1265 = The sum of this time loop - the last time loop


1251 - The time (as minutes)
20:51:34 The time
-1251 = The sum of this time loop - the last time loop


1250 - The time (as minutes)
20:50:20 The time
-1250 = The sum of this time loop - the last time loop


1234 - The time (as minutes)
20:34:38 The time
-1234 = The sum of this time loop - the last time loop


1188 - The time (as minutes)
19:48:59 The time
-1188 = The sum of this time loop - the last time loop


1187 - The time (as minutes)
19:47:52 The time
-1187 = The sum of this time loop - the last time loop


1183 - The time (as minutes)
19:43:19 The time
-1183 = The sum of this time loop - the last time loop


1182 - The time (as minutes)
19:42:51 The time
-1182 = The sum of this time loop - the last time loop

 

I belive the problem with the time is within this part of the code..

						$uniqueip[] = array_unique($log_clientIP);

					if ($log_clientIP == $uniqueip){	
						echo "This is a unique ip<br/><br/>";
						// Then count this as a new visitor and an addittion to log_request

						}
					elseif ($log_clientIP != $uniqueip){
						$previouskey[] = $temp[-1];
						$lastminutes = $previouskey[1];

						$mylogtime = $lastminutes - $minutes;
						echo "$mylogtime = The sum of this time loop - the last time loop<br/><br/><br/>";

							if ($mylogtime >=7){
							// Then count this as a (returning but new) visitor and an addittion to log_request
							}
							else{}

					}// end of elseif statement

 

Can anybody help me please?? Im getting very desperate! ???

Link to comment
Share on other sites

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.