phpcode Posted December 19, 2007 Share Posted December 19, 2007 I'm storing peoples usernames and passwords in a php file but I've got a problem: <?php $username=$_POST['Username']; $password=$_POST['Password']; $file = "members/" . $username . ".php"; function Login() { file_get_contents($file); if ($password == $pass) { echo "Logged in"; } else { echo "Invalid username and/or password!"; } } if (file_exists($file)) { Login(); } else { echo "Invalid username and/or password!"; } ?> The file name is the users account name and the password is inside the php file. I use file_get_contents to get the password but I've got a problem. Whenever someone enters their account information everything works apart from checking the users password no matter what password they enter it says they logged in, can anyone see whats wrong with the code above? Contents of the users file: <?php $pass = "nxHLS6kM"; $ip = "*"; ?> Quote Link to comment Share on other sites More sharing options...
trq Posted December 19, 2007 Share Posted December 19, 2007 file_get_contents returns a string, you never capture this string. All round your logic here is pretty floored, you'll want to use include. Quote Link to comment Share on other sites More sharing options...
phpcode Posted December 19, 2007 Author Share Posted December 19, 2007 I've already tired that but I get this error: Warning: include() [function.include]: Failed opening '' for inclusion (include_path='.;C:\php5\pear') i Quote Link to comment Share on other sites More sharing options...
Pancake Posted December 19, 2007 Share Posted December 19, 2007 I've already tired that but I get this error: Warning: include() [function.include]: Failed opening '' for inclusion (include_path='.;C:\php5\pear') i That means that you specified the file location incorrectly :\ Quote Link to comment Share on other sites More sharing options...
phpcode Posted December 19, 2007 Author Share Posted December 19, 2007 I didn't look at the code below it checks if the file exists then calls the function if it does. Quote Link to comment Share on other sites More sharing options...
haku Posted December 19, 2007 Share Posted December 19, 2007 Your method is both fairly un-secure as well as space consuming on the hard drive on which you are doing it. Why don't you use a database? Quote Link to comment Share on other sites More sharing options...
phpcode Posted December 19, 2007 Author Share Posted December 19, 2007 Because I cannot work mysql: <?php $location = "localhost"; $username = "root"; $password = "******"; $database = "test"; $conn = mysql_connect("$location","$username","$password"); if (!$conn) die ("Could not connect MySQL"); mysql_select_db($database,$conn) or die ("Could not open database"); mysql_query("CREATE TABLE users ( user VARCHAR(20), pass VARCHAR(20))"); $insert = "INSERT INTO users (firstname,surname,location) VALUES ($username,$password)"; mysql_query($insert) or die ("Could not add data to the table"); ?> And it says "Could not add data to the table" but it can create the database. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 Because I cannot work mysql: <?php $location = "localhost"; $username = "root"; $password = "******"; $database = "test"; $conn = mysql_connect("$location","$username","$password"); if (!$conn) die ("Could not connect MySQL"); mysql_select_db($database,$conn) or die ("Could not open database"); mysql_query("CREATE TABLE users ( user VARCHAR(20), pass VARCHAR(20))"); $insert = "INSERT INTO users (firstname,surname,location) VALUES ($username,$password)"; mysql_query($insert) or die ("Could not add data to the table"); ?> And it says "Could not add data to the table" but it can create the database. You didnt provide the query with a location. $insert = "INSERT INTO users (firstname,surname,location) VALUES ('$username','$password','$location')"; Quote Link to comment Share on other sites More sharing options...
phpcode Posted December 19, 2007 Author Share Posted December 19, 2007 Doesn't work. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 Doesn't work. try mysql_query($insert) or die (mysql_error()); Quote Link to comment 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.