Page 1 of 1

Help me please to debug a simple page

Posted: Sun Dec 19, 2010 12:34 pm
by triplebit
Hi all,
I'm new to PHP so please forgive me if my question is weird a little...
I got a code from a friend that worked once for him but fails for me.
The code should get the IP address from the PC that runs it,
than, open a file in the web site, name it video.txt and write down this data,
than close that file.
The PHP version is 5.2.14
Any help is very appreciated
Regards

Kishon

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<title></title>

<?php 
$addr = $_SERVER['REMOTE_ADDR'];


// set file to write
$file = 'video.txt'; 
// open file 
$fh = fopen($file, 'w') or die('Could not open file!'); 
// write to file 
fwrite($fh, $addr) or die('Could not write to file'); 
// close file 
fclose($fh); 
?> 

<script language="javascript" >
<!--


var str = "<?php echo($addr) ;?>";

function InvokeMethod (str)
{
    myDiv.innerHTML = "<font size=8>" + str + "</font>";
}
 
function Button1_onclick() {
    window.external.functionInCSharp(str);
}

function Button2_onclick() {
    window.external.AMessageFromHome = "Try try try";
}

function Button3_onclick() {
    alert (window.external.AMessageFromHome);
}

-->
</script>
</BODY>
</head>
	<body>
        <p>myExtentionPage is loaded !</p>
        <br />
        <br />
        
        <input id="Button1" type="button" value="call CSharp function functionInCSharp" onclick="return Button1_onclick()" /><br />
        
        <br />
        <input id="Button3" type="button" value="Show me a message from home !" onclick="return Button3_onclick()" /><br />
        <br />
        <input id="Button2" type="button" value="Try to change something !" onclick="return Button2_onclick()" /><br />
        <br />
        <br />
        <div id="myDiv" style="font-size: 100%; vertical-align: middle; width: 100%; direction: ltr;
            font-family: Fantasy, Sans-Serif, Serif; height: 200px; text-align: center">
        </div>
	</body>
</html>

Re: Help me please to debug a simple page

Posted: Sun Dec 19, 2010 6:53 pm
by requinix
That looks like thoroughly pointless code.

Make sure that whatever directory the PHP file is in has write permissions for everybody. How you change that depends on what kind of software you're using: examples are CPanel or some FTP program.

Re: Help me please to debug a simple page

Posted: Sun Dec 19, 2010 8:03 pm
by califdon
98% of that code bears no relationship to what you said you want to do. Maybe your friend was just too lazy to show you the 5 lines that mean anything for this purpose.

Code: Select all

$addr = $_SERVER['REMOTE_ADDR'];
gets the IP address of the requesting client browser (NOT "the PC that runs it" because PHP never runs in the client, it is strictly server code) and assigns it to the variable $addr. The lines

Code: Select all

$file = 'video.txt';
$fh = fopen($file, 'w') or die('Could not open file!');
fwrite($fh, $addr) or die('Could not write to file');
fclose($fh); 
write that IP address value to a file in the same directory as the script, named video.txt. If the file doesn't exist, it will create a new file. If a file of that name already exists, it will discard whatever might be in it and replace that with your data. If that's what you want to do, that's all you need. Absolutely no other code in your script, other than the opening <?php and the closing ?>.

Or you can send the IP address back to the browser, to check whether it's working, by replacing those 4 lines of file related code with:

Code: Select all

echo "Your IP address is: $addr";
What do you want to do?