imarockstar Posted December 4, 2008 Share Posted December 4, 2008 Inmy script I am trying to create and write to a new php file .. everything works great until I try to put in what I want to write ... I am trying to write PHP code to a php file ... this is what I am trying to write to the pho file // Change the 4 variables below $yourName = 'change this to your name'; $yourEmail = 'you@yourdomain.com'; $yourSubject = 'change to your subject line'; $referringPage = 'http://www.yourdomain.com/contact.php'; // No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want function cleanPosUrl ($str) { return stripslashes($str); } if ( isset($_POST['sendContactEmail']) ) { $to = $yourEmail; $subject = $yourSubject.': '.$_POST['posRegard']; $message = cleanPosUrl($_POST['posText']); $headers = "From: ".cleanPosUrl($_POST['posName'])." <".$_POST['posEmail'].">\r\n"; $headers .= 'To: '.$yourName.' <'.$yourEmail.'>'."\r\n"; $mailit = mail($to,$subject,$message,$headers); if ( @$mailit ) { header('Location: '.$referringPage.'?success=true'); } else { header('Location: '.$referringPage.'?error=true'); } } but since its code its throwing errors .. how can I put the above into a string and write it to a file ? thx Quote Link to comment https://forums.phpfreaks.com/topic/135553-solved-help-with-writing-to-a-file/ Share on other sites More sharing options...
premiso Posted December 4, 2008 Share Posted December 4, 2008 Your main issue is going to be the use of both single and double quotes. Stick to one. That way you can put the whole thing into a string properly without having to escape the characters. Chances are the errors are being thrown cause of " or ' single quotes. I would escape the double quotes around the \r\n since those need the double quotes. Let me know if that fixes your issue. Quote Link to comment https://forums.phpfreaks.com/topic/135553-solved-help-with-writing-to-a-file/#findComment-706150 Share on other sites More sharing options...
imarockstar Posted December 4, 2008 Author Share Posted December 4, 2008 this is my error .. also .what is the correct way to escape a character ... Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/franklin/public_html/sites/rssnewsite/install/step_03.php on line 32 Quote Link to comment https://forums.phpfreaks.com/topic/135553-solved-help-with-writing-to-a-file/#findComment-706152 Share on other sites More sharing options...
premiso Posted December 4, 2008 Share Posted December 4, 2008 <?php $writeToFile = "// Change the 4 variables below $yourName = 'change this to your name'; $yourEmail = 'you@yourdomain.com'; $yourSubject = 'change to your subject line'; $referringPage = 'http://www.yourdomain.com/contact.php'; // No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want function cleanPosUrl ($str) { return stripslashes($str); } if ( isset($_POST['sendContactEmail']) ) { $to = $yourEmail; $subject = $yourSubject.': '.$_POST['posRegard']; $message = cleanPosUrl($_POST['posText']); $headers = 'From: '.cleanPosUrl($_POST['posName']).' <'.$_POST['posEmail'].\">\r\n\"; $headers .= 'To: '.$yourName.' <'.$yourEmail.'>'.\"\r\n\"; $mailit = mail($to,$subject,$message,$headers); if ( @$mailit ) { header('Location: '.$referringPage.'?success=true'); } else { header('Location: '.$referringPage.'?error=true'); } }"; ?> That should allow you to write to a file. You use \ to escape certain characters. So to escape the double quote its \" as seen above. Quote Link to comment https://forums.phpfreaks.com/topic/135553-solved-help-with-writing-to-a-file/#findComment-706153 Share on other sites More sharing options...
imarockstar Posted December 4, 2008 Author Share Posted December 4, 2008 this is line 32 if ( isset($_POST['sendContactEmail']) ) Quote Link to comment https://forums.phpfreaks.com/topic/135553-solved-help-with-writing-to-a-file/#findComment-706157 Share on other sites More sharing options...
imarockstar Posted December 4, 2008 Author Share Posted December 4, 2008 here is my full script .. to which i am gettng that error for line 32 <?php $cfname = isset($_POST['cfname']) ? trim($_POST['cfname']) : ''; $cfemail = isset($_POST['cfemail']) ? trim($_POST['cfemail']) : ''; $cfsubject = isset($_POST['cfsubject']) ? trim($_POST['cfsubject']) : ''; $submit = $_POST['submit']; $flag = $_POST['flag']; $query = "dbc"; $dolla = "$"; if ( isset($_POST['submit']) & $cfname >= '1' & $cfemail >= '1' & $cfsubject >= '1' ) { $myFile = "../includes/settings2.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<?php // Change the 4 variables below $yourName = 'change this to your name'; $yourEmail = 'you@yourdomain.com'; $yourSubject = 'change to your subject line'; $referringPage = 'http://www.yourdomain.com/contact.php'; // No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want function cleanPosUrl ($str) { return stripslashes($str); } if ( isset($_POST['sendContactEmail']) ) { $to = $yourEmail; $subject = $yourSubject.': '.$_POST['posRegard']; $message = cleanPosUrl($_POST['posText']); $headers = 'From: '.cleanPosUrl($_POST['posName']).' <'.$_POST['posEmail'].\">\r\n\"; $headers .= 'To: '.$yourName.' <'.$yourEmail.'>'.\"\r\n\"; $mailit = mail($to,$subject,$message,$headers); if ( @$mailit ) { header('Location: '.$referringPage.'?success=true'); } else { header('Location: '.$referringPage.'?error=true'); } } ?> "; fwrite($fh, $stringData); fclose($fh); //echo "test the output : yes it worked"; header("Location: step_04.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135553-solved-help-with-writing-to-a-file/#findComment-706163 Share on other sites More sharing options...
rhodesa Posted December 4, 2008 Share Posted December 4, 2008 <?php $cfname = isset($_POST['cfname']) ? trim($_POST['cfname']) : ''; $cfemail = isset($_POST['cfemail']) ? trim($_POST['cfemail']) : ''; $cfsubject = isset($_POST['cfsubject']) ? trim($_POST['cfsubject']) : ''; $submit = $_POST['submit']; $flag = $_POST['flag']; $query = "dbc"; $dolla = "$"; if ( isset($_POST['submit']) & $cfname >= '1' & $cfemail >= '1' & $cfsubject >= '1' ) { $myFile = "../includes/settings2.php"; $fh = fopen($myFile, 'w') or die("can't open file"); ob_start(); ?> // Change the 4 variables below $yourName = 'change this to your name'; $yourEmail = 'you@yourdomain.com'; $yourSubject = 'change to your subject line'; $referringPage = 'http://www.yourdomain.com/contact.php'; // No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want function cleanPosUrl ($str) { return stripslashes($str); } if ( isset($_POST['sendContactEmail']) ) { $to = $yourEmail; $subject = $yourSubject.': '.$_POST['posRegard']; $message = cleanPosUrl($_POST['posText']); $headers = 'From: '.cleanPosUrl($_POST['posName']).' <'.$_POST['posEmail'].\">\r\n\"; $headers .= 'To: '.$yourName.' <'.$yourEmail.'>'.\"\r\n\"; $mailit = mail($to,$subject,$message,$headers); if ( @$mailit ) { header('Location: '.$referringPage.'?success=true'); } else { header('Location: '.$referringPage.'?error=true'); } } <?php $stringData = "<?php\n".ob_get_clean()."\n?>"; fwrite($fh, $stringData); fclose($fh); //echo "test the output : yes it worked"; header("Location: step_04.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135553-solved-help-with-writing-to-a-file/#findComment-706173 Share on other sites More sharing options...
imarockstar Posted December 4, 2008 Author Share Posted December 4, 2008 i figured it out .. i had to escape al the $ signs ... thanks Quote Link to comment https://forums.phpfreaks.com/topic/135553-solved-help-with-writing-to-a-file/#findComment-706180 Share on other sites More sharing options...
premiso Posted December 4, 2008 Share Posted December 4, 2008 I forgot that you needed to escape the $ also. $stringData = "<?php // Change the 4 variables below $yourName = 'change this to your name'; $yourEmail = 'you@yourdomain.com'; $yourSubject = 'change to your subject line'; $referringPage = 'http://www.yourdomain.com/contact.php'; // No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want function cleanPosUrl ($str) { return stripslashes($str); } if ( isset(\$_POST['sendContactEmail']) ) { $to = $yourEmail; $subject = $yourSubject.': '.\$_POST['posRegard']; $message = cleanPosUrl(\$_POST['posText']); $headers = 'From: '.cleanPosUrl(\$_POST['posName']).' <'.\$_POST['posEmail'].\">\r\n\"; $headers .= 'To: '.\$yourName.' <'.\$yourEmail.'>'.\"\r\n\"; $mailit = mail(\$to,\$subject,\$message,\$headers); if ( @\$mailit ) { header('Location: '.\$referringPage.'?success=true'); } else { header('Location: '.\$referringPage.'?error=true'); } } ?> "; Should work. Or you could just change the ' to double like so: $stringData = '<?php // Change the 4 variables below $yourName = "change this to your name"; $yourEmail = "you@yourdomain.com"; $yourSubject = "change to your subject line"; $referringPage = "http://www.yourdomain.com/contact.php"; // No need to edit below unless you really want to. It"s using a simple php mail() function. Use your own if you want function cleanPosUrl ($str) { return stripslashes($str); } if ( isset($_POST["sendContactEmail"]) ) { $to = $yourEmail; $subject = $yourSubject.": ".$_POST["posRegard"]; $message = cleanPosUrl($_POST["posText"]); $headers = "From: ".cleanPosUrl($_POST["posName"])." <".$_POST["posEmail"].\">\r\n\"; $headers .= "To: ".$yourName." <".$yourEmail.">".\"\r\n\"; $mailit = mail($to,$subject,$message,\$headers); if ( @$mailit ) { header("Location: ".$referringPage."?success=true"); } else { header("Location: ".$referringPage."?error=true"); } } ?> '; Quote Link to comment https://forums.phpfreaks.com/topic/135553-solved-help-with-writing-to-a-file/#findComment-706183 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.