LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2007, 08:21 PM   #1
6millionbucks
LQ Newbie
 
Registered: Jul 2007
Posts: 10

Rep: Reputation: 0
exporting dymanic ip address to a file using php


Hello All, this is my first post EVER so I apologies in advance for any newbie pet peeves. I recently installed postfix/spamass./clamav/amavisd/mysqld/php on a dedicated server and after a couple sleepless nights have my mail server running nicely. I have one problem left to solve.

I found a php file a few years ago that will display my current dynamic ip address

<?
if(getenv("HTTP_CLIENT_IP")) {
$ipad = getenv("HTTP_CLIENT_IP");
} elseif(getenv("HTTP_X_FORWARDED_FOR")) {
$ipad = getenv("HTTP_X_FORWARDED_FOR");
} else {
$ipad = getenv("REMOTE_ADDR");
}
$ipad = substr($ipad, 0,14);
echo "$ipad 0";
?>


I changed the last line from

echo $ipad;
to
echo "$ipad 0";

in order to include the zero after the IP address. What I would like this script to do now is export the "$ipad 0" to a file, lets say /etc/postfix/ipfile

I tried different variances of

echo "$ipad 0" >/etc/postfix/ipfile;

but keep getting syntax errors. Your help is greatly appreciated.
 
Old 07-24-2007, 01:53 AM   #2
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
Quote:
I changed the last line from

echo $ipad;
to
echo "$ipad 0";

in order to include the zero after the IP address.
change it to:

Code:
echo $ipad . " 0";

Quote:
echo "$ipad 0" >/etc/postfix/ipfile;
well, php is not bash
you'll need s. th. like the following:
Code:
$filename = 'ipfile';
$ipad = $ipad . " 0";
    if (!$handle = fopen($filename, "r")) { 
         print "can't open file";
         exit;
    }
    if (!fwrite($handle, $ipad)) {
        print "can't write to file";
        exit;
    }    
print "done, wrote $ipad";

    fclose($handle);

  else {
    print "file is not writable";
}

Last edited by baikonur; 07-24-2007 at 02:05 AM.
 
Old 07-24-2007, 02:12 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
A few quick comments...
echo "$ipad 0"; is correct.

If you are using fopen() then you will want the mode to be w+ (read and write), not just read.

However a quick way would be to uses file_get_contents() and file_put_contents(). Check them out on your help file...
 
Old 07-24-2007, 04:11 AM   #4
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
Quote:
A few quick comments...
echo "$ipad 0"; is correct.

If you are using fopen() then you will want the mode to be w+ (read and write), not just read.

However a quick way would be to uses file_get_contents() and file_put_contents(). Check them out on your help file...
oops, my mistake on the fopen mode.
i prefer the concatenation with "." to putting the variables directly inside the string, but you're right, it is correct.
 
Old 07-24-2007, 04:40 PM   #5
6millionbucks
LQ Newbie
 
Registered: Jul 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks for the replies, but I don't follow. I know very little about programming. This is my first attempt at it.

Maybe I should clarify what I'm trying to achieve.

The php code I mentioned earlier is a web page called ip.php which is accessed by the following URL:

http://mydomain.com/ip.php

The output (which is my current home IP address) is then displayed in the browser window as:

xxx.xxx.xxx.xxx 0

I would like this ip string to also be written to a file in /etc/postfix/ipfile when the webpage is called.

That said, I dont know how or where in my ip.php to put the code you provided.

Could you please clarify how the complete code should look in the ip.php to produce the results I'm looking for. Thanks a million.
 
Old 07-24-2007, 11:48 PM   #6
6millionbucks
LQ Newbie
 
Registered: Jul 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Exporting Dynamic IP to a file - SOLVED

My problem has been solved. Here is the final php page that will show your clients IP address while at the same time write the output to a file (ie /tmp/ip.txt).

Thanks to all that replied.

<html>
<head>
<title>Get IP</title>
<style type="text/css">
body
{
background-color: white;
font-family: "verdana";
}
P
{
font-size: 10pt;
}
</style>
</head><body>

<table width="90%" border="1" align="center" cellpadding="3" cellspacing="1">
<tr><td height="60" align="center"><p>

<div align="center">Your IP address is:


<?
if(getenv("HTTP_CLIENT_IP")) {
$ipad = getenv("HTTP_CLIENT_IP");
} elseif(getenv("HTTP_X_FORWARDED_FOR")) {
$ipad = getenv("HTTP_X_FORWARDED_FOR");
} else {
$ipad = getenv("REMOTE_ADDR");
}

$ipad = substr($ipad, 0,14);
echo "$ipad";

$filename = '/tmp/ip.txt';
$ipad = $ipad . " 0";

if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $ipad) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo " Success, wrote ($ipad) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>


</div>
</font></p>
</td></tr></table></body></html>


Now to get the ip address into postfix so that I can relay from home or anywhere else I may be. This is actually fun .

Last edited by 6millionbucks; 07-24-2007 at 11:49 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get linux machine IP address value thru PHP? green_njk Linux - Software 5 12-25-2005 04:06 AM
[SOLVED] (automatic) exporting content of file druuna Programming 2 05-24-2005 03:43 PM
Getting a client's MAC address from PHP ? michaelsanford Programming 1 05-13-2005 02:10 PM
exporting Evolution address book phil81 Linux - Software 1 06-12-2004 02:03 PM
Monitoring a Specific Port and Exporting to a log file chrisfirestar Linux - General 0 10-27-2003 04:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration