Jump to content

[SOLVED] Save variable in php file


FlyingIsFun1217

Recommended Posts

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

Link to comment
Share on other sites

  • 1 month later...

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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