FlyingIsFun1217 Posted April 19, 2008 Share Posted April 19, 2008 Hey! What I'm wondering is how (or if there is a way) to save a variable into a php file. For example, let's say a php file is as such: <?php $varToEdit = ""; ?> Now, what would I have to do to make the file end up looking like this: <?php $varToEdit = "newsavedvalue"; ?> Thanks again for your help! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/ Share on other sites More sharing options...
chigley Posted April 19, 2008 Share Posted April 19, 2008 If you're in PHP 5, you can use file_put_contents(). If not, fopen() and fwrite() will do the job. Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/#findComment-521329 Share on other sites More sharing options...
FlyingIsFun1217 Posted April 19, 2008 Author Share Posted April 19, 2008 Alright, thanks for the tips FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/#findComment-521345 Share on other sites More sharing options...
FlyingIsFun1217 Posted May 29, 2008 Author Share Posted May 29, 2008 Alright, I'm reviving a dead topic I know, but I think I've got it done, and I want to make sure while I wait for my server to come back online. <?php echo '<center>'; echo '<h1><u>Edit Access Password</u></h1>'; echo '<form action="changePassword.php" method="post">'; echo 'Enter in a new access password:'; echo '<br>'; echo '<input type="password" name="newPassword1" style="width: 25%;">'; echo '<br>'; echo '<br>'; echo 'Enter in new access password again:'; echo '<br>'; echo '<input type="password" name="newPassword2" style="width: 25%;">'; echo '<br>'; echo '<br>'; echo 'Enter old access password:'; echo '<br>'; echo '<input type="password" name="oldPassword" style="width: 20%;">'; echo '<input type="submit" value="Change" style="width: 5%;">'; echo '</form>'; echo '</center>'; ?> <?php include('fakerPassword.php'); $firstNewPassword = $_POST['newPassword1']; $secondNewPassword = $_POST['newPassword2']; $enteredOldPassword = $_POST['oldPassword']; if($firstNewPassword == $secondOldPassword) { if(md5($enteredOldPassword) == $encryptedPass) { $inputFileContents = fread('fakerPassword.php'); $lengthOfOld = strlen($enteredOldPassword); $oldPassPosition = strpos($inputFileContents, $enteredOldPassword); //Now we have a new updated file content variable, holding all text needed to be written to new file... substr_replace($inputFileContents, $firstNewPassword, $oldPassPosition, $lengthOfOld); $newOpenPointer = fopen('fakerPassword.php','w'); if(fwrite($newOpenPointer, $inputFileContents)) { echo '<center>'; echo 'Access password successfully changed!'; echo '<br>'; echo '<a href="javascript:history.go(-1)">Return to "Password Edit" form</a>.'; echo '</center>'; fclose($newOpenPointer); } else { echo '<center>'; echo 'ERROR! Password not changed!'; echo '<br>'; echo 'MAKE SURE THAT "password.php" STILL EXISTS ON THE SERVER! THIS FILE IS VITAL TO THE SYSTEM!'; echo '</center>'; } } else { echo '<center>'; echo 'ERROR! One or more of the passwords entered were invalid!'; echo '<br>'; echo '<a href="javascript:history.go(-1)">Go back and try again</a>.'; echo '</center>'; } else { echo '<center>'; echo 'ERROR! Different new password(s) entered!'; echo '<br>'; echo '<a href="javascript:history.go(-1)">Go back and try again</a>.'; echo '</center>'; } ?> Anything in here that would render it lame? I know that there is the unsafe practice of opening and clearing the file (I think it does anyway), but it's alright for now as long as it works. Thanks! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/#findComment-552788 Share on other sites More sharing options...
FlyingIsFun1217 Posted May 29, 2008 Author Share Posted May 29, 2008 After echo-ing some of the variables ($inputFileContents and $oldPassPosition [which relies on the first]), that the file isn't being read quite the way I want it to. Basically, if the file consists of "hello World!", I want to have echo $inputFileContents; to give me 'hello World!'. Is this not what fread does? Thanks! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/#findComment-552904 Share on other sites More sharing options...
FlyingIsFun1217 Posted May 29, 2008 Author Share Posted May 29, 2008 Does anybody know how I can go about copying the contents of a file into memory (a variable)? Thanks! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/#findComment-552964 Share on other sites More sharing options...
DarkWater Posted May 29, 2008 Share Posted May 29, 2008 $file = file_get_contents("test.txt"); Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/#findComment-552966 Share on other sites More sharing options...
FlyingIsFun1217 Posted May 29, 2008 Author Share Posted May 29, 2008 Yeah, I saw that right afterward, but it seems that I'm still not getting the desired results. The substr_replace result is just giving me what I previously had. $inputFileContents = file_get_contents('fakerPassword.php'); echo 'Input: '.$inputFileContents; $lengthOfOld = strlen($enteredOldPassword); echo 'Old Password Length: '.$lengthOfOld; $oldPassPosition = strpos($inputFileContents, $enteredOldPassword); echo 'Position in file of old password: '.$oldPassPosition; //Now we have a new updated file content variable, holding all text needed to be written to new file... substr_replace($inputFileContents, $firstNewPassword, $oldPassPosition, $lengthOfOld); $newOpenPointer = fopen('fakerPassword.php','w'); if(fwrite($newOpenPointer, $inputFileContents)) { echo '<center>'; echo 'Access password successfully changed!'; echo '<br>'; echo '<a href="javascript:history.go(-1)">Return to "Password Edit" form</a>.'; echo '</center>'; fclose($newOpenPointer); } else { echo '<center>'; echo 'ERROR! Password not changed!'; echo '<br>'; echo 'MAKE SURE THAT "password.php" STILL EXISTS ON THE SERVER! THIS FILE IS VITAL TO THE SYSTEM!'; echo '</center>'; } } I'm a little new to the whole file editing thing, so I'm probably missing something really simple :/ FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/#findComment-552993 Share on other sites More sharing options...
FlyingIsFun1217 Posted May 30, 2008 Author Share Posted May 30, 2008 Bump. I'll be at my last day of school for the year today, so if anybody wants to bug hunt... or point out some stupid error of mine (I'm sure...), feel free Thanks so much! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/#findComment-553394 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 1, 2008 Author Share Posted June 1, 2008 Does anybody know why the above code does not output the new value? Is there actually a much more simplistic way of going about this approach? Thanks! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/#findComment-555182 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 1, 2008 Author Share Posted June 1, 2008 Alright, seems I've got it working; all I needed to do was assign substr_replace to a new variable. Thanks for your help guys! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/101864-solved-save-variable-in-php-file/#findComment-555189 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.