BoltZ Posted November 1, 2008 Share Posted November 1, 2008 I am trying to do it for my forum installation that when the user clicks the button they go to install.php and it writes to connect.php which is a directory below the install directory and for some reason this code won't work $myFile = "../connect.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<?php\n $db = mysql_connect("localhost", "$_POST['username']", "$_POST['password']") or die("Could not connect.");\n if(!$db)\n die("no db");\n if(!mysql_select_db("$_POST['dbname']",$db))\n die("No database selected.");\n if(!get_magic_quotes_gpc())\n \n {\n $_GET = array_map('mysql_real_escape_string', $_GET); \n $_POST = array_map('mysql_real_escape_string', $_POST); \n $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);\n }\n else\n { \n $_GET = array_map('stripslashes', $_GET);\n $_POST = array_map('stripslashes', $_POST);\n $_COOKIE = array_map('stripslashes', $_COOKIE);\n $_GET = array_map('mysql_real_escape_string', $_GET);\n $_POST = array_map('mysql_real_escape_string', $_POST);\n $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);\n}\n?>"; fwrite($fh, $stringData); fclose($fh); Help plz? Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/ Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 I've been working on it and this is the updated code <?php /** * BBoardX 1.0 BETA * Copyright © 2008 BBoardX, All Rights Reserved * * Website: http://www.bboardx.com * License: http://www.bboardx.com/license * */ $myFile = "../connect.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<?php\n $db = mysql_connect("$_POST['req_db_host']", "$_POST['username']", "$_POST['password']") or die("Could not connect.");\n $admin= $_POST['admin'];\n $admin_email= $_POST['admin_email'];\n $admin_password = $_POST['admin_password'];\n if(!$db)\n die("no db");\n if(!mysql_select_db("$_POST['database']",$db))\n die("No database selected.");\n if(!get_magic_quotes_gpc())\n \n {\n $_GET = array_map('mysql_real_escape_string', $_GET); \n $_POST = array_map('mysql_real_escape_string', $_POST); \n $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);\n }\n else\n { \n $_GET = array_map('stripslashes', $_GET);\n $_POST = array_map('stripslashes', $_POST);\n $_COOKIE = array_map('stripslashes', $_COOKIE);\n $_GET = array_map('mysql_real_escape_string', $_GET);\n $_POST = array_map('mysql_real_escape_string', $_POST);\n $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);\n}\n?>"; fwrite($fh, $stringData); fclose($fh); Header ('Location runsql.php'); ?> currently I have a Parse error: syntax error, unexpected T_VARIABLE in /home/ericr/public_html/test/install/install.php on line 14 error Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680004 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 Ok some errors were because I had " in there so I escaped all those and Now I have this code <?php /** * BBoardX 1.0 BETA * Copyright © 2008 BBoardX, All Rights Reserved * * Website: http://www.bboardx.com * License: http://www.bboardx.com/license * */ $myFile = "../connect.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<?php\n $db = mysql_connect(\"$_POST['req_db_host']\", \"$_POST['username']\", \"$_POST['password']\") or die(\"Could not connect.\");\n $admin= $_POST['admin'];\n $admin_email= $_POST['admin_email'];\n $admin_password = $_POST['admin_password'];\n if(!$db)\n die("no db");\n if(!mysql_select_db(\"$_POST['database']\",$db))\n die(\"No database selected.\");\n if(!get_magic_quotes_gpc())\n \n {\n $_GET = array_map('mysql_real_escape_string', $_GET); \n $_POST = array_map('mysql_real_escape_string', $_POST); \n $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);\n }\n else\n { \n $_GET = array_map('stripslashes', $_GET);\n $_POST = array_map('stripslashes', $_POST);\n $_COOKIE = array_map('stripslashes', $_COOKIE);\n $_GET = array_map('mysql_real_escape_string', $_GET);\n $_POST = array_map('mysql_real_escape_string', $_POST);\n $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);\n}\n?>"; fwrite($fh, $stringData); fclose($fh); Header ('Location runsql.php'); ?> the error i have now is Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/ericr/public_html/test/install/install.php on line 14 Pleaseeee someone help meee Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680010 Share on other sites More sharing options...
Flames Posted November 1, 2008 Share Posted November 1, 2008 you also need to escape all ' symbols as well using \ and in mysql queryies i believe you need to enclose $_POST['variables'] with {} e.g. {$_POST[\'variables\']} Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680019 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 Ok thanks Ill do that stuff then post back here the new code //edit heres the new code and with new errors ;] <?php /** * BBoardX 1.0 BETA * Copyright © 2008 BBoardX, All Rights Reserved * * Website: http://www.bboardx.com * License: http://www.bboardx.com/license * */ $myFile = "../connect.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<?php\n$db = mysql_connect(\"{$_POST[\'req_db_host\']}\", \"{$_POST[\'username\']}\", \"{$_POST[\'password\']}\") or die(\"Could not connect.\");\n$admin= {$_POST[\'admin\']};\n$admin_email={$_POST[\'admin_email\']};\n$admin_password={$_POST[\'admin_password\']};\nif(!$db)\ndie(\"no db\");\nif(!mysql_select_db(\"{$_POST[\'database\']}\",$db))\ndie(\"No database selected.\");\nif(!get_magic_quotes_gpc())\n{\n$_GET = array_map(\'mysql_real_escape_string\', $_GET); \n$_POST = array_map(\'mysql_real_escape_string\', $_POST);\n$_COOKIE = array_map(\'mysql_real_escape_string\', $_COOKIE);\n }\nelse\n{\n$_GET = array_map(\'stripslashes\', $_GET);\n$_POST = array_map(\'stripslashes\', $_POST);\n$_COOKIE=array_map(\'stripslashes\', $_COOKIE);\n$_GET = array_map(\'mysql_real_escape_string\', $_GET);\n$_POST = array_map(\'mysql_real_escape_string\', $_POST);\n$_COOKIE = array_map(\'mysql_real_escape_string\', $_COOKIE);\n}\n?>"; fwrite($fh, $stringData); fclose($fh); Header ('Location runsql.php'); ?> Extremely sorry if its hard to read. I am not sure I did the {} right on the $_POST stuff so If you could review that also just in case the errors are this Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/ericr/public_html/test/install/install.php on line 12 Parse error: syntax error, unexpected T_STRING, expecting ']' in /home/ericr/public_html/test/install/install.php on line 16 Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680028 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 Use single quotes $stringData ='<?php ...'; In double quotes all variable names are replaced with their values. You might also try heredoc Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680032 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 But i Want the variable names to be replaced with their values. I am creating a configure file for the database for these forum installation. Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680035 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 i relized what u were talking about so i changed the " to ' and then it didnt give me an error execpt it gave me this page when I reloaded the page Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. Should I do a " instead of ' now? Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680043 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 You have: $stringData = "<?php\n$db = mysql_connect(\"{$_POST[req_db_host]}\", \"{$_POST[username]}\", \"{$_POST[password]}\") or die(\"Could not connect.\");..."; I understand you want $_POST['req_db_host'] to have its value, but you surely don't want $db here to be replaced. So: $stringData = '<?php\n $db = mysql_connect('.$_POST["req_db_host"].','.$_POST["username"].','.$_POST["password"].') or die("Could not connect");' Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680047 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 Thanks. Here is the updated code <?php /** * BBoardX 1.0 BETA * Copyright © 2008 BBoardX, All Rights Reserved * * Website: http://www.bboardx.com * License: http://www.bboardx.com/license * */ $myFile = "../connect.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = '<?php\n $db = mysql_connect('.$_POST["req_db_host"].','.$_POST["username"].','.$_POST["password"].') or die("Could not connect");' $admin= {$_POST[\'admin\']};\n$admin_email={$_POST[\'admin_email\']};\n$admin_password={$_POST[\'admin_password\']};\nif(!$db)\ndie(\"no db\");\nif(!mysql_select_db(\"{$_POST[\'database\']}\",$db))\ndie(\"No database selected.\");\nif(!get_magic_quotes_gpc())\n{\n$_GET = array_map(\'mysql_real_escape_string\', $_GET); \n$_POST = array_map(\'mysql_real_escape_string\', $_POST);\n$_COOKIE = array_map(\'mysql_real_escape_string\', $_COOKIE);\n }\nelse\n{\n$_GET = array_map(\'stripslashes\', $_GET);\n$_POST = array_map(\'stripslashes\', $_POST);\n$_COOKIE=array_map(\'stripslashes\', $_COOKIE);\n$_GET = array_map(\'mysql_real_escape_string\', $_GET);\n$_POST = array_map(\'mysql_real_escape_string\', $_POST);\n$_COOKIE = array_map(\'mysql_real_escape_string\', $_COOKIE);\n}\n?>"; fwrite($fh, $stringData); fclose($fh); Header ('Location runsql.php'); ?> Error this time is Parse error: syntax error, unexpected T_VARIABLE in /home/ericr/public_html/test/install/install.php on line 15 Isnt PHP great?? Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680050 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 It is. It tells you exactly where you did mistake. $admin=... is outside your quote. Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680053 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 I thought I did this right but apparenlty i didnt? <?php /** * BBoardX 1.0 BETA * Copyright © 2008 BBoardX, All Rights Reserved * * Website: http://www.bboardx.com * License: http://www.bboardx.com/license * */ $myFile = "../connect.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = '<?php\n $db = mysql_connect(\'.$_POST["req_db_host"].\',\'.$_POST[\"username"].\',\'.$_POST[\"password\"].\') or die(\"Could not connect\");\'\n $admin= {$_POST[\'admin\']};\n$admin_email={$_POST[\'admin_email\']};\n$admin_password={$_POST[\'admin_password\']};\nif(!$db)\ndie(\"no db\");\nif(!mysql_select_db(\"{$_POST[\'database\']}\",$db))\ndie(\"No database selected.\");\nif(!get_magic_quotes_gpc())\n{\n$_GET = array_map(\'mysql_real_escape_string\', $_GET); \n$_POST = array_map(\'mysql_real_escape_string\', $_POST);\n$_COOKIE = array_map(\'mysql_real_escape_string\', $_COOKIE);\n }\nelse\n{\n$_GET = array_map(\'stripslashes\', $_GET);\n$_POST = array_map(\'stripslashes\', $_POST);\n$_COOKIE=array_map(\'stripslashes\', $_COOKIE);\n$_GET = array_map(\'mysql_real_escape_string\', $_GET);\n$_POST = array_map(\'mysql_real_escape_string\', $_POST);\n$_COOKIE = array_map(\'mysql_real_escape_string\', $_COOKIE);\n}\n?>"; fwrite($fh, $stringData); fclose($fh); Header ('Location runsql.php'); ?> Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680077 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 Ok... before you go too far... Why are you even trying to do this? I mean, wouldn't it be easier to save just an ini file with database credentials, that could later be loaded into a script? Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680083 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 Its for forum software installlation. I need them to enter the form then it writes it all to connect.php then it redirects them to a page where it will say congratulations and all that, your queries were done blah blah. and then it will give directions on what to do after that. But ne way about the code.. Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680087 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 How about this? $stringData = "<?php\\n %%db = mysql_connect({$_POST['req_db_host']},{$_POST['username']},{$_POST['password']) or die('Could not connect');\\n %%admin= {$_POST['admin']};\\n %%admin_email={$_POST['admin_email']};\\n %%admin_password={$_POST['admin_password']};\\n if(!%%db)\\n die('no db');\\n if(!mysql_select_db({$_POST['database']},%%db))\\n die('No database selected.');\\n if(get_magic_quotes_gpc())\\n {\\n %%_GET = array_map('stripslashes', %%_GET);\\n %%_POST = array_map('stripslashes', %%_POST);\\n %%_COOKIE=array_map('stripslashes', %%_COOKIE);\\n }\\n %%_GET = array_map('mysql_real_escape_string', %%_GET);\\n %%_POST = array_map('mysql_real_escape_string', %%_POST);\\n %%_COOKIE = array_map('mysql_real_escape_string', %%_COOKIE);\\n ?>"; $stringData = str_replace("%%","$"); Seems that forum is eating backslashes Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680092 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 It wrote to the connect.php file with no errors except the connect.php file contains this only <?php\n$db = mysql_connect(\"{$_POST['req_db_host']}\", \"{$_POST['username']}\", \"{$_POST['password']}\") or die(\"Could not connect.\");\n$admin= {$_POST['admin']};\n$admin_email={$_POST['admin_email']};\n$admin_password={$_POST['admin_password']};\nif(!$db)\ndie(\"no db\");\nif(!mysql_select_db(\"{$_POST['database']}\",$db))\ndie(\"No database selected.\");\nif(!get_magic_quotes_gpc())\n{\n$_GET = array_map('mysql_real_escape_string', $_GET); \n$_POST = array_map('mysql_real_escape_string', $_POST);\n$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);\n }\nelse\n{\n$_GET = array_map('stripslashes', $_GET);\n$_POST = array_map('stripslashes', $_POST);\n$_COOKIE=array_map('stripslashes', $_COOKIE);\n$_GET = array_map('mysql_real_escape_string', $_GET);\n$_POST = array_map('mysql_real_escape_string', $_POST);\n$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);\n}\n?> and all that \n is causing error. Why? n how to fix Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680100 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 don't write them Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680103 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 what do u mean "dont write them" Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680104 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 Throw all "\n"s away from $stringData = "...."; and see what happens. Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680105 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 Theres not really \n's there. In your code there is just this, it smianly just n there plain and then the comment style \\n there in the beginning. whats up with that. Do i take out all the n's and the \\n? <?php /** * BBoardX 1.0 BETA * Copyright © 2008 BBoardX, All Rights Reserved * * Website: http://www.bboardx.com * License: http://www.bboardx.com/license * */ $myFile = "../connect.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<?php\\n %%db = mysql_connect({$_POST['req_db_host']},{$_POST['username']},{$_POST['password']) or die('Could not connect');n %%admin= {$_POST['admin']};n %%admin_email={$_POST['admin_email']};n %%admin_password={$_POST['admin_password']};n if(!%%db)n die('no db');n if(!mysql_select_db({$_POST['database']},%%db))n die('No database selected.');n if(get_magic_quotes_gpc())n {n %%_GET = array_map('stripslashes', %%_GET);n %%_POST = array_map('stripslashes', %%_POST);n %%_COOKIE=array_map('stripslashes', %%_COOKIE);n }n %%_GET = array_map('mysql_real_escape_string', %%_GET);n %%_POST = array_map('mysql_real_escape_string', %%_POST);n %%_COOKIE = array_map('mysql_real_escape_string', %%_COOKIE);n ?>"; $stringData = str_replace("%%","$"); fwrite($fh, $stringData); fclose($fh); Header ('Location runsql.php'); ?> Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680108 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 The forum here just ate my \ before each n And there should be one \ before each n (that is, each n marking end of line ) Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680111 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 It's not writing any thinng at all into the connect.php file. Like at all Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680112 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 My... this is tough... I think I'm gonna do some trying by myself... Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680114 Share on other sites More sharing options...
BoltZ Posted November 1, 2008 Author Share Posted November 1, 2008 You want all the files so you can play around with it some? well I wannaa know why its not writing Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680117 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 Attached is the file that works here. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/130988-fwrite-function/#findComment-680118 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.