Jump to content

Recommended Posts

I just started php a couple of days ago. Probably would of started sooner if I could install php on my localhost.

 

Anyway I made this code:

<?php
$ip = isset($_SERVER['REMOTE_ADDR'];

$file = fopen("counter.txt", "r");
$bytes = 256;
$counts = fread($file, $bytes);
fclose ($file);

$file2 = fopen ("ips.txt", "r");
$fileip = fread($file2, $bytes);

if( $ip == $fileip)
{
	echo "You have already been counted\n";
	echo "Total people counted: " . $counts . "\n";
	echo "See currents results here: http://zezombia.themavesite.com/browser-count/browsers.txt";
	return false;
}

fclose ($file2);

$file3 = fopen ("ips.txt", "w+");
fwrite ($file3, $fileip . $ip . "\n");
fclose ($file3);

$file4 = fopen ("counter.txt", "w+");
fwrite ($file4, $counts + 1);
fclose ($file4);

$file5 = fopen ("browsers.txt", "w+");
$browsers = fread($file5, $bytes);
fwrite ($file5, $browsers . $_SERVER['HTTP_USER_AGENT'] . "\n");
fclose ($file5);

echo "You have been counted!\n";
echo "Total people counted: " . $counts . "\n";
echo "See currents results here: http://zezombia.themavesite.com/browser-count/browsers.txt";
?>

 

What's wrong:

 

1) When the page loads, it dosn't say:

 

message 1

message 2

message 3

 

It says:

 

message 1 message 2 message 3

 

2) When it saves what browser they were in, it goes over the last saved one instead of putting it on a new line.

 

3) Because the IP file holds more then one IP, it checks everything. That gives no help at all from people who want to refresh the page a ton. How do I make it check every ip on the list (one per line)?

 

Also could you help me with it just saying Internet Explorer or Firefox instead of:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1

Link to comment
https://forums.phpfreaks.com/topic/121187-something-wrong-with-code/
Share on other sites

1) instead of using /n try using

<br />

 

2) try changing your fopen mode from w+ to a+

 

3) if you are going to record IP addresses, and then you are going to check against that record, you should be saving them as csv or using some delimiter. When you fread, use strstr to check for the IP match.

 

This is really not the easiest or best way to do what you are trying to accomplish. Using mysql would be easier, but I realize you are just learning so at least check it out.

its \n not /n first off and the people are right

 

in a browser the data is rendered as xhtml (or html) and the \n character is treated as a new line character (carriage return) in the source code meaning that the actual code being rendered has a carriage return added.

 

To get a carriage return into the outputted rendering the <br /> is needed as stated.

i can't see where it ouputs message 1 ... but try adding this to the top

echo "<pre>";

for the browser info have a look at this

http://us3.php.net/function.get-browser

also read the note at the bottom you have to make sure you specify a browscap file in your php.ini the php page has a link to a site you can download one from

but this looks like a really big job for files you could do this in a lot less code and a lot less hassle with a database have you looked at mysql (it is free)

 

Scott.

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.