* FAQ    * Search  * Register * Login 
Active topics
Unanswered topics

All times are UTC-06:00



Post new topic  Reply to topic  [ 13 posts ] 
Author Message
 Post subject: traffic counter
PostPosted: Sun Jul 09, 2006 4:11 pm 
Offline
DBB Fleet Admiral
DBB Fleet Admiral
User avatar

Joined: Wed Aug 07, 2002 2:01 am
Posts: 2365
Location: South of Ft. Worth Texas
Please forgive me for being a moron. I really DO know a little bit about computers, but I'm obviously lost when it comes to php, pearl, and cgi scripts.

My wife wants a traffic counter for her web page. nothing fancy, just increment the count each time someone hits the main page, and display the number, of course.

I found the following example on the web:

<?php
$file = \"count.txt\";
$open = fopen($file, \"r\");
$size = filesize($file);
$count = fread($open, $size);
$count1 = $count+1;
echo($count1);
fclose($open);

$open = fopen($file, \"w\");
fwrite($open, $count1);
fclose($open);
?>


seems simple enough, open the file, read the current number, increment, write the new number, close. All nice and simple.

BUT, I need to know how to access this script from an hmtl page, and how to return the new value so it can be displayed.

any help would be greatly appreciated. (or even just pointing me to the correct documentation, I'm starting from complete ignorance here)

Thanks!

<edit>
Ah! I see, I should be able to embed the php code directly into html. Ok, nice, but it only seems to be half working. for example, if I use:

<html>
<head>
<title>PHP Test</title>
</head>
<body>
This is before the php script
<br>
<?php echo '<p>Hello World</p>'; ?>
<br>
back to regular html
</body>
</html>


I get the following as a result:

This is before the php script
Hello World

'; ?>
back to regular html


Now obviously I'm running the php script, but why does it terminate BEFORE the end tag when it hits the second single quote?

I've tried embedding the above \"counter\" script, and while runs without any obvious error, it does NOT increment the counter in the file. <sigh>

---

<edit AGAIN>
UGH! only works embedded like that if you change the ext to php. ok, back to the drawing board. I just need to run a php script, have it update the counter, and return the number to a regular old html page.


Top
   
 Post subject:
PostPosted: Sun Jul 09, 2006 5:17 pm 
Offline
DBB Alumni
DBB Alumni
User avatar

Joined: Wed Mar 15, 2000 3:01 am
Posts: 8826
Location: Seattle
Uh... why can't you have the file extension as PHP? Or, maybe you could configure your webserver to parse all HTML files through the PHP parser.

Here's an example (save this as example.php):

Code:
<html>
<head>
<title>Counter Example</title>
</head>
<body>
<p>This is the contents of the page.</p>
<?php
$filename = 'count.txt';
$count = (int) file_get_contents($filename) + 1;
file_put_contents($filename, $count);
?>
<p>This page has been visited <?php echo $count; ?> times.</p>
</body>
</html>


Now, you must have a file called count.txt in the same directory as the webpage, and it MUST be world-readable and world-writable (under Linux this would be known as chmod 666).


Top
   
 Post subject:
PostPosted: Sun Jul 09, 2006 5:22 pm 
Offline
DBB Fleet Admiral
DBB Fleet Admiral
User avatar

Joined: Wed Aug 07, 2002 2:01 am
Posts: 2365
Location: South of Ft. Worth Texas
Thank you very much! I suppose I could make the home page php I guess.

Thanks, now I just have to figure out how to set the permissions at the host. :)

Thanks!


Top
   
 Post subject:
PostPosted: Sun Jul 09, 2006 5:27 pm 
Offline
DBB Master
DBB Master
User avatar

Joined: Sun Sep 05, 1999 2:01 am
Posts: 6377
Location: ☃☃☃
Your host may have a special directory that already has write permission outside of what's seeable by the Internet. In my case, it's a sibling directory of the directory that is shared to the Internet. For example, to implement your code for my host on the file www.mydomain.com/index.php I would need:
Code:
$file = \"../database/count.txt\";

And this database directory is accessible via my ftp account, etc.


Top
   
 Post subject:
PostPosted: Sun Jul 09, 2006 6:38 pm 
Offline
DBB Fleet Admiral
DBB Fleet Admiral
User avatar

Joined: Wed Aug 07, 2002 2:01 am
Posts: 2365
Location: South of Ft. Worth Texas
aha! by setting up a .htaccess file with the line:

AddType application/x-httpd-php php php3 html shtml

now all html pages process through php. fantastic!

Still haven't gotten the file access figured out yet. If I've got a writeable directory, I haven't found it yet. Perhaps read write access can be set through .htaccess? More research...

Meanwhile, a question. When testing your code DCrazy, the function: file_put_contents($filename, $count);

Fatal error: Call to undefined function: file_put_contents()

Which seems odd since I can find the function documented. Its not a show stopper, I can always just call open, write, close, but doing it all in one function is kinda cool. :)

Could the error be because the file didn't open correctly? the error message seems all wrong for that.


Top
   
 Post subject:
PostPosted: Sun Jul 09, 2006 7:39 pm 
Offline
DBB Alumni
DBB Alumni
User avatar

Joined: Wed Mar 15, 2000 3:01 am
Posts: 8826
Location: Seattle
You need at least PHP 4.3.0 for file_put_contents(). If you can't get your host to upgrade, then open/write/close is the only alternative. Sorry :(.

Write access needs to be given on a filesystem basis. How do you access your host? If you have shell (SSH) access, you can find the permissions on a file by doing a 'stat filename'. Linux permissions are given in three categories: owner, group, and everyone. These are each represented by a digit in a three-digit number. Read is 4, Write is 2, and Execute is 1, meaning that, for example, if the owner of a file is allowed to do all three things, that permission is expressed by 4 + 2 + 1 = 7. A file that has a permission of 666 means that the owner can read and write (4 + 2), the group that owns the file can read and write, and everyone else (e.g. the webserver) can read and write that file. For directories, the execute flag changes meaning; it means \"can open this directory\". This site does a far better job of explaining.

If you don't have SSH access to your webserver, you might be able to change the file permissions using your FTP client. I know IE's built-in client can do it (right click, Properties).


Top
   
 Post subject:
PostPosted: Sun Jul 09, 2006 9:16 pm 
Offline
DBB Fleet Admiral
DBB Fleet Admiral
User avatar

Joined: Wed Aug 07, 2002 2:01 am
Posts: 2365
Location: South of Ft. Worth Texas
DCrazy wrote:
If you don't have SSH access to your webserver, you might be able to change the file permissions using your FTP client. I know IE's built-in client can do it (right click, Properties).

I'll try IE, cause I certainly can't seem to change the properties using cuteftp.

Thank you VERY much for the help!


Top
   
 Post subject:
PostPosted: Sun Jul 09, 2006 9:52 pm 
Offline
DBB Alumni
DBB Alumni
User avatar

Joined: Wed Mar 15, 2000 3:01 am
Posts: 8826
Location: Seattle
No problem at all. If you have future coding questions, you might wanna stick them in the Coders' Corner. :P


Top
   
 Post subject:
PostPosted: Sun Jul 09, 2006 9:59 pm 
Offline
DBB Database Master
DBB Database Master
User avatar

Joined: Sun Nov 29, 1998 3:01 am
Posts: 15146
ICQ: 46028958
Website: http://krom.d3k.org
AOL: dutenhnj
Location: Camping the energy center. BTW, did you know you can have up to 100 characters in this location box?
/me slaps fist into palm. Ahh! Thats where this goes, thanks for reminding me DC.


Top
   
 Post subject:
PostPosted: Sun Jul 09, 2006 11:51 pm 
Offline
DBB DemiGod
DBB DemiGod
User avatar

Joined: Sat Oct 24, 1998 2:01 am
Posts: 6415
Location: Calgary Alberta Canada
did you check to see if your provider already has a premade counter availible to their customers?

just a thought


Top
   
 Post subject:
PostPosted: Mon Jul 10, 2006 5:52 am 
Offline
DBB Fleet Admiral
DBB Fleet Admiral
User avatar

Joined: Wed Aug 07, 2002 2:01 am
Posts: 2365
Location: South of Ft. Worth Texas
DCrazy wrote:
If you have future coding questions, you might wanna stick them in the Coders' Corner.

oops. I thought coding corner was for D3 specific coding issues. sorry!

And darnit, there went another chance to be nazi moderated. I never have any luck! :)

fliptw wrote:
did you check to see if your provider already has a premade counter availible to their customers?

Yeah, it's clear that we didn't buy top end hosting. The response was "we don't provide any scripts, but for $30 we'd be happy to write any you would like"

Uhuh. Of course, I've spent a LOT more than $30 worth of time working on this, but I LIKE learning things, and it's the principle of the matter. :)


Top
   
 Post subject:
PostPosted: Mon Jul 10, 2006 5:17 pm 
Offline
DBB Fleet Admiral
DBB Fleet Admiral
User avatar

Joined: Wed Aug 07, 2002 2:01 am
Posts: 2365
Location: South of Ft. Worth Texas
Just an update. I switched to using filezilla as my ftp client. NICE program! I like it much better than my old copy of cuteftp. I could reset my file permissions to the ominous 666 with a simple right click. :)

I also figured out how to make the server process any html page as php. You do this by creating a file named .htaccess (that's right, no name, only an extension). That file should contain the following line (It may have others if you've already set other stuff up, I hadn't)

AddType application/x-httpd-php php php3 html htm shtml

Drop this file into your root html directory. And, tada! every .htm or .html page goes through the php interpreter. shiny!

So, now I've got it all working, thank you VERY much for your help folks. Php is interesting, I'll have to explore it further.


Top
   
 Post subject:
PostPosted: Mon Jul 10, 2006 9:05 pm 
Offline
DBB Alumni
DBB Alumni
User avatar

Joined: Wed Mar 15, 2000 3:01 am
Posts: 8826
Location: Seattle
Well, the PHP documentation isn't bad, but it's really only useful as a reference. It's very hard to learn PHP from the manual unless you are familiar with programming. If the concepts are programming are foreign to you, take a look into online tutorials or even a book (I'd avoid O'Reilly if I were you; their PHP book, last time I checked, was sorely out of date and chock full of errors).

The .htaccess file is great because, assuming the server admin has given you authority to do so, you can override many things using .htaccess. The most common application of .htaccess files is for HTTP authentication. Check out this page from the Apache reference for information on what exactly .htaccess files do and how you can do some neat tricks with them. Keep in mind that the version I linked was written for Apache 2.2; if you try something and it doesn't work, first make sure the appropriate AllowOverrides permissions are in effect if you can, and then make sure that you're not trying to do something that your version of Apache can't do.

By the way, this will come in handy if you ever need to debug a problem. Create a new file called \"phpinfo.php\" and put the following code in it:

Code:
<?php phpinfo(); ?>


Upload it, then visit it. PHP will produce a page that gives a lot of detailed information about its configuration. Then figure out how to use .htaccess files to require authentication for that page. :)


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 13 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Descent'rs have piloted these pages
 
The layout and contents contained within this site are © DescentBB.net 1997-2006.
Descent, Descent II are © Parallax Software Corporation.
Descent III is Outrage Entertainment.
Descent is a Trademark of Interplay Productions.

Miner Wars™ is trademark of Keen Software House s. r. o.
.


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group