UGN Security
Posted By: Shinobi php help (I think) - 10/20/05 12:00 AM
What up bitches, I never ask for help, but on this I am truly stumped..... I'm trying to make a public web stats section on my webpage. To display:

Today's Hits:
Today's Unique Hits:
Total Unique Hits:

I can't seem to it it to work how I want it to. The only condition is that no mysql database. I do not have mysql installed and setup and I really do not want to do it cause... Well I'm lazy. But if anyone has any ideas. Please Help would be greatly appreciated
Posted By: Gremelin Re: php help (I think) - 10/20/05 12:34 AM
have perl? have access logs? Use AWStats
Posted By: Shinobi Re: php help (I think) - 10/20/05 12:51 AM
I do have a reports page that show me all of this, and perl is active. However the reports page is...[censored] up is the best way I can describe it
Posted By: Gremelin Re: php help (I think) - 10/20/05 01:22 AM
reports page? access logs are generated by the apache webserver; usually /var/logs or some [censored]
Posted By: §intå× Re: php help (I think) - 10/20/05 04:02 AM
lol OMG are you using PHP 5.X? if so you have SQL lite. If not use a flat file system like UBB works off of. It isn't as fast as a modern db, but I seriously doubt you will generate the traffic to truely make a difference.

Like I say Ubb uses a flat file system, PHP is highly able to do this as well.

There are actually some advantages of using a flat file system also. Check it out

http://www.dummies.com/WileyCDA/DummiesArticle/id-2402.html


Here are some tuts on flat file systems
http://www.tutorialized.com/tutorial/Flat-file-Shoutbox/9119
some file manipulation tuts http://www.tutorialized.com/tutorials/PHP/File-Manipulation/1
http://www.tutorialized.com/tutorial/Data-Access-Benchmarks/5593
http://www.tutorialized.com/tutorial/File-existance-checking/3898
http://www.tutorialized.com/tutorial/File-locking/3905
http://www.tutorialized.com/tutorial/File-Creator-Editor/3012
http://www.tutorialized.com/tutorial/Last-Modified/3016


Those should get you well on your way to creating a flat file system to count hits and such.


The way I see it...


1st file: total_hits.inc(besure to make inc file viewable only by php and might as well place them below the viewable directory)

file lay-out
Code
$hits //$hits is replaced by a running tally of hits
$last_hit // timestamp of last logged hit
$last_ip //ip address of last hit
second file(one will be created every month)
file name: Month_year.inc
$date_timestamp
$hits_count
$ip_address[1 thru whatever] //this is an array of every IP from that day
$browser_per_ip //what browser each ip is using
$tally_unique_ip's
//next day but same month is added below seperated by 2 carriage returns
$date_timestamp
$hits_count
$ip_address[1 thru whatever] //this is an array of every IP from that day
$browser_per_ip //what browser each ip is using
$tally_unique_ip's



Little math and you could have one hell of a stats page.
Posted By: Shinobi Re: php help (I think) - 10/20/05 04:11 AM
Well the thing is I don't want a whole page dedicated to stats, its just a little portion of the main page. So all I need is something that counts hits, unique hits and total unique hits, and generates a normal looking number. To be displayed on the index page.
Posted By: Gremelin Re: php help (I think) - 10/20/05 04:15 AM
we're now running PHP4.3.9 heh; Apache 2.0; but we can upgrade to whatever since it's our vps wink
Posted By: §intå× Re: php help (I think) - 10/20/05 06:25 AM
who said you need a whole page? I think you are miss understanding me.


Code
Figure 1 - Flat file data storage system

               ------Flat file1.inc
              /
index.php-----
              \
               ------Flat File2.inc

######################################################

Figure 2 - MySQL data storage system

               ------mySql Table 1
              /
index.php-----
              \
               ------mySql Table 2
Think of the flat files as database tables you use to pull data from to populate a/any page. Also you add a bit of code to every page to ensure the flat files are updated with every hit.


Basically what I am saying is you can use plain old text files as a database. You can read and write to them just as you can a database.
Posted By: Gremelin Re: php help (I think) - 10/20/05 06:59 AM
we use flat files for storing our link statistics; mainly cas i'm too lazy to get on dem knees...
Posted By: §intå× Re: php help (I think) - 10/20/05 07:04 AM
They are easy to use. A little more work than MySQL to manipulat data, but a good solid alternative if you are missing a SQL db.
Posted By: Shinobi Re: php help (I think) - 10/20/05 03:07 PM
I gotcha now, thanks a lot sintax
Posted By: §intå× Re: php help (I think) - 10/20/05 04:35 PM
Anytime man, any time. Let us know how it works out. I have never created a flat file system myself, but If you need help I am willing learn and lend a hand.
Posted By: Shinobi Re: php help (I think) - 10/20/05 07:31 PM
Yes yes, it should be a learning experience for me as well. I'm actully getting started on it right....
....
.....
......
.......

NOW

hehe I'll keep you updated as to how it went.
Posted By: Shinobi Re: php help (I think) - 10/20/05 08:30 PM
these damn tutorials are like pulling teeth...
Posted By: Shinobi Re: php help (I think) - 10/20/05 09:35 PM
ok finally got it if anyone is intersted I'll put the code here...

totalhits.php
Code
 <?php
$myfile = "totalhits.txt";
//Assigns file name to the variable we'll use to handle it
if(file_exists($myfile))//if the file exists
    {                    // runs counter script
    $var = fopen( $myfile,'r+');
    //opens in read and write mode for file
    $visits = fread($var,filesize($myfile));
    //puts the content of the file for its whole length
    rewind( $var );
    //resets the file position indicator to the beginning
    $visits++; //increments the actual number of vists by 1
    fwrite($var, $visits);
    //writes on the variable the actual (incremented) value
    fclose($var);//closes our file reference
    }
else
    {
        print "File $myfile doesn't exist...";
        Die();
//if the file doesn't exist prompts a warning and kills the script        
    }
$message = sprintf("Total Hits: %s",$visits);
//saves our visits message in a variable ($message) that will be used as output
print $message;
?> 
Code in index.php
Code
<?php include 'totalhits.php'; ?>
only question I have is permissions... The txtfile has to be 777, thats the only way it will work... is that correct? Wait... I can use .inc......
Posted By: Shinobi Re: php help (I think) - 10/20/05 09:36 PM
sorry thinking while posting...
Posted By: §intå× Re: php help (I think) - 10/20/05 09:47 PM
you should not have to use 777 for it to work. 666 should be a viable secure chmod. you can use any file extension you want for the tex/flat files. inc is common. I have used .fun for functions, .pwd for MySQL login info, etc etc etc

You have to modify your .htaccess file however to may these files treated as PHP files. Once you do that you should beable to store sensative data in them.
Posted By: Shinobi Re: php help (I think) - 10/20/05 10:03 PM
Well I changed them to .inc, and moved them out of the main directory to a hidden on, I'm now changing the chmod to 666 to see if it works. TY for all the help sintax, it was not as hard as I had first thought

laugh
Posted By: §intå× Re: php help (I think) - 10/21/05 01:23 AM
you do not need a hidden directory, it would just be more secure to store them below the pulic accesable directory.


ie...

/home/your_dir/public_html

or

/vars/your_dir/htdocs

or

/vars/your_dir/www


each of the "your_dir" in the examples above will not be reachable by a web browser. FTP yes, but not a web browser. This keep people from harvesting your IP list
© UGN Security Forum