hamman247
hamman247

Reputation: 11

splfileobject not working on ubuntu 16.04 apache webserver with php7.0

I have been making a website that has a user-account-loggin system that is based on accessing .txt files in php. I originally tested much of the code on a free web-hosting service and got the user-account-loggin system functional, but when I tried to move it over to my own personal apache web server the functionality of some of my php code seems to have been lost and I'm not sure why. Specifically, I have narrowed it down to the splfileobject() function returning no value for the variable $passfile. Am I missing some php library that is necessary to do this? When I looked it, up it said that php7.0 had splfileobject(), and its other connected functions, as part of the standard php library.

Here is some of my code

<?php
$username = $_POST["username"];
$password = $_POST["password"];

$account = file('usernames.txt');
$accountfix = str_replace("\n", "", $account);
$filepoint = array_search($username, $accountfix);

if ($filepoint !== false) {

    $passfile = new SplFileObject('passwords.txt', 'r+');

    $passfile->seek($filepoint);

    $potentialpass = str_replace("\n", "", $passfile);

// check password
if ($password === $potentialpass) {
$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
$_SESSION["loggedin"] = true;
$_SESSION["filepoint"] = $filepoint;
print "Logged in succesfully!";
}
else{
    print "Incorrect Password";
}
}
else {
 print "Incorrect Username.";
}

?>

For some reason opening a file like this works fine.

$account = file('textfiles/usernames.txt');

However, I want to be able to search to a specific point in a password file the has parallel data to the username file to cut down on computation time and had to use splfileobject to do so.

Thankyou

Upvotes: 1

Views: 103

Answers (1)

hamman247
hamman247

Reputation: 11

I fixed the problem, apparently The web server needed to be given permission to read and write on .txt files. I never received any errors regarding this. I was able to give it permission to do so with the following commands into the linux terminal.

sudo chown -R www-data /var/www
sudo chmod -R u=rwx /var/www

Many thanks to beno1990 at ubuntuforums.org Heres the thread I found it on, https://ubuntuforums.org/showthread.php?t=1089334

Upvotes: 0

Related Questions