Need help fixing some code...

Im looking for help as to why I am getting these errors on my .php.

This is the code I am using in my php.

<?php
$today = getdate();
$mon = $today['month'];
$dayn = $today['mday'];
$year = $today['year'];
$url = $_SERVER['REQUEST_URI'];

if ($_SERVER["HTTP_X_FORWARDED_FOR"])
{
       if ($_SERVER["HTTP_CLIENT_IP"])
       {
                 $proxy = $_SERVER["HTTP_CLIENT_IP"];
       }
       else
       {
                  $proxy = $_SERVER["REMOTE_ADDR"];
       }
       $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
       if ($_SERVER["HTTP_CLIENT_IP"]) {
                  $ip = $_SERVER["HTTP_CLIENT_IP"];
       }
       else
       {
                  $ip = $_SERVER["REMOTE_ADDR"];
       }
}

$filename = "ip.txt";

if (is_writable($filename)) {

   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }

if(fwrite($handle,"Ip Address: $ip")==FALSE)
     echo "Could not write into the file 1<BR>";

if(isset($proxy))
      if(fwrite($handle," - Proxy Address: $proxy")==FALSE)
            echo "Could not write into the file 2<BR>";

      if(fwrite($handle," - Date: $month/$dayn/$year")==FALSE)
            echo "Could not write into the file 3<BR>";

      if(fwrite($handle," - URL: $url")==FALSE)
            echo "Could not write into the file 4<BR>";

if(fwrite($handle,"\r\n")==FALSE)
     echo "Could not write into the file 5<BR>";
   
   echo "Success, wrote the ip address to file ($filename)";
   
   fclose($handle);
                   
} else {
   echo "The file $filename is not writable";
}
?>



****************************************
THESE ARE THE ERRORS THAT DISPLAY ON THE PAGE.


Notice: Undefined index: REQUEST_URI in C:\Web Sites\Active WebSite\homepage.php on line 64

Notice: Undefined index: HTTP_X_FORWARDED_FOR in C:\Web Sites\Active WebSite\homepage.php on line 66

Notice: Undefined index: HTTP_CLIENT_IP in C:\Web Sites\Active WebSite\homepage.php on line 80

Notice: Undefined variable: month in C:\Web Sites\Active WebSite\homepage.php on line 105
Success, wrote the ip address to file (ip.txt)



**********************************

The script does what it was meant to do, the file is created and the IP and other information is logged, but why am I getting these errors?
erikcookAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Codeit1978Commented:
check this link out:
http://www.phpdig.net/forum/archive/index.php/t-544.html

It refers to the problem with Undefined index: REQUEST_URI
0
erikcookAuthor Commented:
So am I correct to assume that I may not have PHP setup correctly or am lacking some kind of support for the Indexes that are used in this script?
0
Diablo84Commented:
There are a few errors in the code, i havent tested this yet but try:

<?php
$today = getdate();
$month = $today['month'];
$dayn = $today['mday'];
$year = $today['year'];
$url = "http://yourdomain.com".$_SERVER['PHP_SELF'];

if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
 $proxy = (isset($_SERVER["HTTP_CLIENT_IP"])) ? $_SERVER["HTTP_CLIENT_IP"] : $_SERVER["REMOTE_ADDR"];
 $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else {
 $ip = (isset($_SERVER["HTTP_CLIENT_IP"])) ? $_SERVER["HTTP_CLIENT_IP"] : $_SERVER["REMOTE_ADDR"];
}

$filename = "ip.txt";

if (is_writable($filename)) {

 if (!$handle = fopen($filename, 'a')) {
  echo "Cannot open file ($filename)";
  exit;
 }
 
 $content = "Ip Address: $ip \r\n";
 
 if (isset($proxy)) {
 $content .= "
 - Proxy Address: $proxy \r\n
 - Date: $month/$dayn/$year \r\n
 - URL: $url \r\n";
 }
 
 echo (fwrite($handle,$content) === false)) ? "Could not write into the file<BR>" : "Success, wrote the ip address to file ($filename)";
 
 fclose($handle);
                 
}
else {
 echo "The file $filename is not writable";
}
?>

replacing "http://yourdomain.com" with your domain, that will cover the request uri problem
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

nicholassolutionsCommented:
The problem is that if your webserver does not create all the entries you need in $_SERVER, they are not available to PHP. Try using phpinfo() to see what variables your server has created. You'll probably have to find a workaround for each error you are getting.

Here is a fairly good one:

if ($_SERVER["HTTP_X_FORWARDED_FOR"]) {
$ipadres = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
      if ($_SERVER["HTTP_CLIENT_IP"]) {
      $ipadres = $_SERVER["HTTP_CLIENT_IP"];
      } else {
      $ipadres = $_SERVER["REMOTE_ADDR"];
      }
}


The last error about $month is because you should have used $mon, which you define at the top of your script (not $month, which is undefined)

Cheers,
Matt
0
Diablo84Commented:
sorry error on this line:

echo (fwrite($handle,$content) === false)) ? "Could not write into the file<BR>" : "Success, wrote the ip address to file ($filename)";

should be:

echo (fwrite($handle,$content) === false) ? "Could not write into the file<BR>" : "Success, wrote the ip address to file ($filename)";
0
Diablo84Commented:
Just looked at your original code again, seems you want to write everything and the proxy if the proxy is set, originally i had it as the date and url are only added if the proxy is set. Your incorrect use of the if statement threw me off (you can only use it without { brackets } if the is only one event happening, not for multi line events).

The following might be better suited:

<?php
$today = getdate();
$month = $today['month'];
$dayn = $today['mday'];
$year = $today['year'];
$url = "http://yourdomain.com".$_SERVER['PHP_SELF'];

if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
 $proxy = (isset($_SERVER["HTTP_CLIENT_IP"])) ? $_SERVER["HTTP_CLIENT_IP"] : $_SERVER["REMOTE_ADDR"];
 $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else {
 $ip = (isset($_SERVER["HTTP_CLIENT_IP"])) ? $_SERVER["HTTP_CLIENT_IP"] : $_SERVER["REMOTE_ADDR"];
}

$filename = "ip.txt";

if (is_writable($filename)) {

 if (!$handle = fopen($filename, 'a')) {
  echo "Cannot open file ($filename)";
  exit;
 }
 
 $content = "Ip Address: $ip \r\n";
 if (isset($proxy)) $content .= "- Proxy Address: $proxy \r\n";
 $content .= "- Date: $month/$dayn/$year \r\n";
 $content .= "- URL: $url \r\n";
 
 echo (fwrite($handle,$content) === false) ? "Could not write into the file<BR>" : "Success, wrote the ip address to file ($filename)";
 
 fclose($handle);
                 
}
else {
 echo "The file $filename is not writable";
}
?>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
erikcookAuthor Commented:
Awesome, that helps alot. Another question that pops up is, am I using my \r\n incorreclty?
I need the script to output each hit to the script on a single line.

This is how it outputs now...

Ip Address: 127.0.0.1
- Date: November/18/2004
- URL: http://yourdomain.com/test.php
 

I need:
Ip Address: 127.0.0.1 - Date: November/18/2004 - URL: http://yourdomain.com/test.php

Also, my end goal is to be able to use another script to import this data from the text file in a 3 collum table

IP ADDRESS     DATE                                  REFERRER
127.0.0.1         November/18/2004              http://yourdomain.com/test.php

How can I extract that without the other information in the text file?
 
0
Diablo84Commented:
\r is carriage return and \n is new line

windows uses \r\n for a new line

unix/lines just uses \n

for what you are doing i would save them in this format

ip|date|referer

then when you read from the file you can use

$contents= explode("|",$line_from_file);

you then have an array with $contents

print_r($contents);

or access each item using

echo $contents[0] //ip

and so on where each index is numeric, starting at 0 and incrementing by one for each item
0
Diablo84Commented:
unix/linux*

eg. instead of:

 $content = "Ip Address: $ip \r\n";
 if (isset($proxy)) $content .= "- Proxy Address: $proxy \r\n";
 $content .= "- Date: $month/$dayn/$year \r\n";
 $content .= "- URL: $url \r\n";
 
 echo (fwrite($handle,$content) === false) ? "Could not write into the file<BR>" : "Success, wrote the ip address to file ($filename)";
 
 fclose($handle);

use:

 $content[] = "Ip Address: $ip \r\n";
 if (isset($proxy)) $content[] = "- Proxy Address: $proxy \r\n";
 $content[] = "- Date: $month/$dayn/$year \r\n";
 $content[] = "- URL: $url \r\n";
 
 echo (fwrite($handle,implode("|",$content) === false) ? "Could not write into the file<BR>" : "Success, wrote the ip address to file ($filename)";
 
 fclose($handle);

which will store the data in the format i mentioned, ip|proxy if set|date|page etc

so you can later use the info using explode
0
Diablo84Commented:
Forgot to remove  \r\n from each string, that would not be needed with the above method.
0
erikcookAuthor Commented:
What syntax change would I need to make in windows for..
echo (fwrite($handle,implode("|",$content) === false) ? "Could not write into the file<BR>" : "Success, wrote the ip address to file ($filename)";
0
Diablo84Commented:
You should just need to append \r\n

Basically what happens is you open the file placing the file pointer at the end of that file, build an array of values to add (your ip, your date etc), you then join the array items together using | and then write them to the file, to account for the new line:

 echo (fwrite($handle,implode("|",$content)."\r\n" === false) ? "Could not write into the file<BR>" : "Success, wrote the ip address to file ($filename)";

or alternatively just add another aray item after the url one

$content[] = "\r\n";

When it comes to displaying the data you can read the file (perhaps using file_get_contents) explode by \r\n then loop through that array and for each item explode by | so you can retirieve the induvidual items.


0
erikcookAuthor Commented:
Ok I follow now, thanks for the help.

One other thing I want to verify before I close the thread..

To add local time I would just add the variable

$month = $today['time'];

and another array item

$content .= " - Time: $time";

Correct?
0
erikcookAuthor Commented:
correction..

$time =$today['time']
$content .=" - Time: $time";
0
Diablo84Commented:
you need

$content[] = "value";

for the array

.= appends a string. eg:

$var = "this is";
$var .= " a string";
echo $var ; //outputs this is a string

$var[] = "this is an array item";
$var[] = "this is another array item";

echo $var[0]; //outputs this is an array item
echo $var[1]; //outputs this is another array item
0
Diablo84Commented:
However there isnt a time index for getdate so you would either have to use

$time = $today['hours'].":".$today['minutes'].":".$today['seconds'];

or

$time = date("H:i:s");
0
erikcookAuthor Commented:
Good Stuff, Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.