[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need help fixing some code...

Posted on 2004-11-18
17
Medium Priority
?
1,030 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:erikcook
17 Comments
 
LVL 1

Expert Comment

by:Codeit1978
ID: 12614654
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
 

Author Comment

by:erikcook
ID: 12614771
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12614888
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:nicholassolutions
ID: 12614891
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12614897
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
 
LVL 27

Accepted Solution

by:
Diablo84 earned 2000 total points
ID: 12615046
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
 

Author Comment

by:erikcook
ID: 12615186
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12615231
\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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12615258
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12615274
Forgot to remove  \r\n from each string, that would not be needed with the above method.
0
 

Author Comment

by:erikcook
ID: 12615414
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12615492
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
 

Author Comment

by:erikcook
ID: 12615683
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
 

Author Comment

by:erikcook
ID: 12615738
correction..

$time =$today['time']
$content .=" - Time: $time";
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12615746
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12615789
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
 

Author Comment

by:erikcook
ID: 12615822
Good Stuff, Thanks again!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question