Jump to content

fwrite and line endings - big headache


arew264

Recommended Posts

I have a php script that should be writing a file that modifies the registry for the purpose of getting starcraft to work over the internet (nasty NAT lag bug that starcraft can't fix). Anyway, I have a script set up to write a registry file to set settings that need to be set, but fwrite is not putting any line endings in that I can see. This file will be generated on a linux server for windows clients. Here is the code I have so far, the \n and \n\r's are my futile attempts to get this to work.
[code]
session_start();
$fn = 'downloads/'.session_id().'.reg';
$fh = fopen($fn, 'wb') or die("can't open file");
$ip = $REMOTE_ADDR;
$ipb = explode(".",$ip);
$ipb = explode(".",$ip);
$ipb = explode(".",$ip);
fwrite($fh, 'REGEDIT4\n');
fwrite($fh, '');
fwrite($fh, '[HKEY_CURRENT_USER\Software\Battle.net\Configuration]\r\n');
fwrite($fh, '"Battle.net Gateways"=hex(7):31,30,30,31,00,30,31,00,75,73,77,65,73,74,2e,62,61,74,74,6c,65,2e,6e,65,74,00,38,00,55,2e,53,2e,20,57,65,73,74,00,75,73,65,61,73,74,2e,62,61,74,74,6c,65,2e,6e,65,74,00,36,00,55,2e,53,2e,20,45,61,73,74,00,61,73,69,61,2e,62,61,74,74,6c,65,2e,6e,65,74,00,2d,39,00,41,73,69,61,00,65,75,72,6f,70,65,2e,62,61,74,74,6c,65,2e,6e,65,74,00,2d,31,00,45,75,72,6f,70,65,00,66,77,67,67,2e,73,65,72,76,65,67,61,6d,65,2e,6f,72,67,00,37,00,55,2e,53,2e,20,54,65,78,61,73,00,00\n');
fwrite($fh, '"Game Data Port"=dword:000046e9\n');
fwrite($fh, '');
fwrite($fh, '[HKEY_CURRENT_USER\Software\Blizzard Entertainment\Warcraft III]');
fwrite($fh, '"Battle.net Gateways"=hex(7):31,30,30,31,00,30,30,00,75,73,77,65,73,74,2e,62,61,74,74,6c,65,2e,6e,65,74,00,38,00,4c,6f,72,64,61,65,72,6f,6e,28,55,2e,53,2e,20,57,65,73,74,29,00,75,73,65,61,73,74,2e,62,61,74,74,6c,65,2e,6e,65,74,00,36,00,41,7a,65,72,6f,74,68,28,55,2e,53,2e,20,45,61,73,74,29,00,61,73,69,61,2e,62,61,74,74,6c,65,2e,6e,65,74,00,2d,39,00,4b,61,6c,69,6d,64,6f,72,28,41,73,69,61,29,00,65,75,72,6f,70,65,2e,62,61,74,74,6c,65,2e,6e,65,74,00,2d,31,00,4e,6f,72,74,68,72,65,6e,64,28,45,75,72,6f,70,65,29,00,66,77,67,67,2e,73,65,72,76,65,67,61,6d,65,2e,6f,72,67,00,37,00,46,57,47,47,28,55,2e,53,2e,20,54,65,78,61,73,29,00,00');
fwrite($fh, '');
fwrite($fh, '[HKEY_LOCAL_MACHINE\Software\Battle.net\Configuration]');
fwrite($fh, '"Game Data Port"=dword:000046e9');
echo $ip;
fclose($fh);
[/code]

As you can see, thereis also some code in there that is supposed to set $ip equal to the last octet of the client ip address, as that is a piece if info that I will be using to determine what clients are on what ports. It creates the .reg file in the downloads folder based on the client's session id, and I will have a cron job that purges these every hour or so. However the problem I can't get around is that the entire file is on one line.

The ip splitter also doesn't work, but I'm not as worried about that. That I can probably figure out... I hope...
Link to comment
https://forums.phpfreaks.com/topic/34084-fwrite-and-line-endings-big-headache/
Share on other sites

Okay, I have the script to the point where it will put line endings in... for the browser at least.
When I edit it in notepad, however, there are no line endings at all.
[code]
session_start();
$fn = 'downloads/'.session_id().'.reg';
$fh = fopen($fn, 'w') or die("can't open file");
$ip = $REMOTE_ADDR;
echo $ip;
$ipb = explode(".",$ip);
$ipb = explode(".",$ip);
$ipb = explode(".",$ip);
fwrite($fh, "REGEDIT4\n");
fwrite($fh, "\n");
fwrite($fh, "[HKEY_CURRENT_USER\Software\Battle.net\Configuration]\n");
fwrite($fh, "\"Battle.net Gateways\"=hex(7):31,30,30,31,00,30,31,00,75,73,77,65,73,74,2e,62,61,74,74,6c,65,2e,6e,65,74,00,38,00,55,2e,53,2e,20,57,65,73,74,00,75,73,65,61,73,74,2e,62,61,74,74,6c,65,2e,6e,65,74,00,36,00,55,2e,53,2e,20,45,61,73,74,00,61,73,69,61,2e,62,61,74,74,6c,65,2e,6e,65,74,00,2d,39,00,41,73,69,61,00,65,75,72,6f,70,65,2e,62,61,74,74,6c,65,2e,6e,65,74,00,2d,31,00,45,75,72,6f,70,65,00,66,77,67,67,2e,73,65,72,76,65,67,61,6d,65,2e,6f,72,67,00,37,00,55,2e,53,2e,20,54,65,78,61,73,00,00\n");
fwrite($fh, "\"Game Data Port\"=dword:000046e9\n");
fwrite($fh, "\n");
fwrite($fh, "[HKEY_CURRENT_USER\Software\Blizzard Entertainment\Warcraft III]");
fwrite($fh, "\"Battle.net Gateways\"=hex(7):31,30,30,31,00,30,30,00,75,73,77,65,73,74,2e,62,61,74,74,6c,65,2e,6e,65,74,00,38,00,4c,6f,72,64,61,65,72,6f,6e,28,55,2e,53,2e,20,57,65,73,74,29,00,75,73,65,61,73,74,2e,62,61,74,74,6c,65,2e,6e,65,74,00,36,00,41,7a,65,72,6f,74,68,28,55,2e,53,2e,20,45,61,73,74,29,00,61,73,69,61,2e,62,61,74,74,6c,65,2e,6e,65,74,00,2d,39,00,4b,61,6c,69,6d,64,6f,72,28,41,73,69,61,29,00,65,75,72,6f,70,65,2e,62,61,74,74,6c,65,2e,6e,65,74,00,2d,31,00,4e,6f,72,74,68,72,65,6e,64,28,45,75,72,6f,70,65,29,00,66,77,67,67,2e,73,65,72,76,65,67,61,6d,65,2e,6f,72,67,00,37,00,46,57,47,47,28,55,2e,53,2e,20,54,65,78,61,73,29,00,00\n");
fwrite($fh, "\n");
fwrite($fh, "[HKEY_LOCAL_MACHINE\Software\Battle.net\Configuration]\n");
fwrite($fh, "\"Game Data Port\"=dword:000046e9\n");
echo $ip;
fclose($fh);
[/code]

This script will need to have windows line endings in the resulting file or the registry editor will complain, I believe.
Also, that $REMOTE_ADDR variable appears to give nothing. I'm guessing it's a global variable that isn't available anymore. Where can I find the ip address of the client requesting the page in the $SERVER superglobal (I'm guessing that's where I can find it)?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.