Hi,

I got two files a php file to process and record data to a text file

Now when a visitor visits a page on my website.... it records some information about them and it is sent to process.php and then process.php then records the data in a .txt file.

That works great. I simply use a php include function on pages i want tracked and it tracks the information and records it.

Problem i got is some of my webpages are in folders, i have realised that from testing that i cannot use full url to the text file for it to be written to. It seems my only way to get around it is by having multiple instances of the text file in folders. This is hard work thou as it means when i want to view the information i have to view several txt files not just one.

To make it more clear this is what i got by using http:// to write to the file:

Warning: fopen(http://www.my-dowmain-name.com/visitordata.txt) [function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections in /home/blahblah/public_html/visitorrecorder.php on line 37

So my questions is, is there a way i can get around this? Having multiple files to have to read through is not what i want, i want it store it all in one file.

I am very new to php and know i could setup a MySQL database but i rather use a txt file for storing the data.

Thank you,
genieuk

Recommended Answers

All 8 Replies

<?php //stats.php
$file = $_SERVER['DOCUMENT_ROOT'].'visitordata.txt'; // define the text file.
// Get user stats.
$getdate = date( 'd-m-Y, H:i:s' );// Get the date.
$user_ip = $_SERVER['REMOTE_ADDR'];// Get the users IP.
$user_browser = $_SERVER['HTTP_USER_AGENT'];// Get the browser type. one of many things you can get for the stats
$page= $_SERVER['PHP_SELF']; //Get the current page
$referer = getenv("HTTP_REFERER"); // Get the refering page.
// Look for the text file and open it for writting.
$fp = fopen($file, "a+");//open the text file for writing.
fputs ($fp, "$user_ip,$getdate, $page, $referer\n");// Write the user stats into the text file.
fclose($fp);// Close the text file.
?>

a draft version it works but if it isnt the data you want its easy to recode <?php include($_SERVER['DOCUMENT_ROOT'].'stats.php'); ?> in each file, no hasslse with .. . ../../../..

line 1, is the one with path details
you can $_SERVER['DOCUMENT_ROOT'].'/folder/folder2/visitordata.txt'; if you want to keep the data file out of the root

Hi,

Can you tell me will this always work ? the

$_SERVER['DOCUMENT_ROOT']

I mean what does it mean and do? if you could explain for me please.

Also i am rite in thinking that this is a possible secuirty threat or something and if someone is behind a proxy i think it called then it would not work ? am i also rite that some hosts don't allow it or something?

Sorry for not being much help but i am sure i read bad things about

$_SERVER['DOCUMENT_ROOT']

Thank you,
genieuk

Hello hello i have an adviceto u.. y dontu store your data in a table..
u can have havethe fields as follows:

Name
Date
and your other fields..

If u r to keep certain information in a text file then its not that suitable..

er yes the solution given s right

Hello hello i have an adviceto u.. y dontu store your data in a table..
u can have havethe fields as follows:

Name
Date
and your other fields..

If u r to keep certain information in a text file then its not that suitable..

er yes the solution given s right

mmm, i am not storing it in a table if that what you mean, it will appear in the txt file something like:

Date: Date would be here
IP: IP info would be here if not it records it as No Record
Host: Host info would be here if not it records it as No Record
Browser: Browser info would be here if not it records it as No Record
URL: URL will appear here
Referrer: if there is on referrer info would appear here

I just not sure if that

$_SERVER['DOCUMENT_ROOT']

will cause problems on servers. I mean does it only work on certain servers? does the host i am using would need safe_mode or what ever they call it on/off ? would it be reuired for globals to be turned on/off?

Reason why i will be using on different hosts so need to know if it will work with any server or not etc

Thank you,
genieuk

Hi,

Sadly it does not work on host server. Yes server does support php etc...

I get 2 errors all relating to the $_SERVER function, basically it a reliable solution.

Warning: fopen() [function.fopen]: open_basedir restriction in effect. File(/usr/local/apache/htdocsvisitordata.txt) is not within the allowed path(s): (/home/:/usr/lib/php:/tmp) in /home/a145ygdt/public_html/visitorrecorder.php on line 39

Not looking in rite location ^^

Warning: fopen(/usr/local/apache/htdocsvisitordata.txt) [function.fopen]: failed to open stream: Operation not permitted in /home/a145ygdt/public_html/visitorrecorder.php on line 39

Looking in rite location but server must not allow me to open it, think this is a restriction on host server. ^^ txt file is chomoded to 777 so not a permission problem.

Doh!

genieuk

The PHP Manual says:
"$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI 1.1 specification, so you should be able to expect those. "

Bold by me

Because there is no guarantee that $_SERVER will yield, you can set your own variable $root_path at the top of every php page (not includes).

In files at root level:
$root_path = './';

In files one folder deep
$root_path = '../';

In files two folders deep
$root_path = '../../';

etc.

Then use this variable as a prefix for all paths that need to be defined relative to root:

<?php //stats.php
$file = $root_path . 'visitordata.txt'; // define the text file.
$fp = fopen($file, "a+");//open the text file for writing.
$whatever = 'whatever';
fputs ($fp, "$whatever\n");// Write the user stats into the text file.
fclose($fp);// Close the text file.
?>

Thus, the above code can be indude()d in any file, with $root_path (coded directly on the page) providing the necessary upward direction to root.

This is a commonly used technique, all through phpBB 2.x for example.

Hope this helps

Airshow

Have you looked into adding a php.ini files to get around what your host isn't allowing you to do. This solved my globals problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.