Hi
I want to create a login screen which asks for username and passwor with change passsword facility.

My idea is to store username and password in a php file and update them.

Can anyone please tell me how to update a php file from another php file..


Thanx in advance

My idea is to store username and password in a php file and update them.

I'm not sure if this would be the best way to do it...

However, the basic functions you'll need to use are PHP's file functions. The simplest way to do it would be...

$fileContents = file_get_contents($filename);

//  Do something with the contents

file_put_contents($filename, $newFileContents);

file_get_contents() takes a filename (string) and returns the contents of the file in a string.
file_put_contents takes a filename (string) and contents (string) and writes them to the file.

For storing usernames and passwords, the best solution is probably to use a mysql database. You could read the databases section of Practical PHP Programming for an overview of mysql and the related php functions.

Another option would be to use an xml file to store the usernames and the hashed passwords (after they've been processed with md5 or crypt). This is a simpler option if you don't have access to or don't want to use a database.

PHP has some great built in functionality with xml now using SimpleXML. You could read the SimpleXML section of Practical PHP Programming for an overview on how to use it to read, write, and modify xml data.

Good luck,
- Walkere

'm not sure if this would be the best way to do it...

Exactly. Its not the best way. Its not safe. And, btw, file_put_contents only works if your php version is >= 5. Even I suggest you to use mysql database. But, if you can't have a database, then you can use a text file(or xml) as a 'database'.

I solved my problem like this..


changepwd.php

include("strings.php");
$checkpass=$pass;

$newpass=$_REQUEST['NewPwd'];

		$fn = "strings.php";
		$file = fopen($fn, "w+");
		$size = filesize($fn);     
		fwrite($file, "<?php "."$"."pass='".$newpass."';?>");
		fclose($file); 
		echo "Password changed successfully";

strings.php

<?php $pass='venkat';?>

Glad that you solved your problem. But still it isnt safe :( Anyways, All the best!

Glad that you solved your problem. But still it isnt safe :( Anyways, All the best!

Should be plenty safe. No one will be able to see the password details, because it will be parsed by the php interpreter. Just like how your mySQL user/password information is floating around somewhere in a config.php file...

Are you just saving one password in there? Or are there more users?

- Walkere

There will be only one user...
So that worked for me

There will be only one user...
So that worked for me

Ahh, I thought that you were going to store a bunch (or at least a handful of users).

If you tried to add more records to the file, it would get more complicated to parse the variable names, make sure you didn't use the same name twice, etc.

But for one user, I suppose it's simple enough. Definitely a strange way to do it, but if it works, who cares? Kudos for thinking outside the box.

- Walkere

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.