Jump to content

Recommended Posts

I have created a login page where you can register, log in and log out. Here everything is saved on file (not MySQL or other databases). Everything works fine when I tested this in http://localhost/ but when I upload everything to a Web server, so that you can access them from "outside" then I get lots of error such as

 

 

Warning: fopen(userpwd.txt) [function.fopen]: failed to open stream: Permission denied in /afs/ltu.se/systemdata/www/students/somroy-0/gemensam.php on line 13

 

 

what can I do to fix this?

Link to comment
https://forums.phpfreaks.com/topic/219629-error-problem-php-help/
Share on other sites

I am so new with all of this. This is what I've done.

 

 

gemensam.php (common.php in English)

 

<?php

session_start();

function registerUser($user,$pass1,$pass2){
$errorText = '';

// Kontrollera lösenord
if ($pass1 != $pass2) $errorText = "Lösenorden är inte identiska!";
elseif (strlen($pass1) < 6) $errorText = "Lösenordet är för kort!";

// Kontrollera om användare redan existerar
$pfile = fopen("userpwd.txt","a+");
    rewind($pfile);

    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {
            $errorText = "Användarnamnet finns redan!";
            break;
        }
    }

    // Om allt är OK -> lagra användardata
    if ($errorText == ''){
	// Säkera lösenord sträng
	$userpass = md5($pass1);
    	
	fwrite($pfile, "\r\n$user:$userpass");
    }
    
    fclose($pfile);


return $errorText;
}

function loginUser($user,$pass){
$errorText = '';
$validUser = false;

// Kontrollera användare existerar	
$pfile = fopen("userpwd.txt","r");
    rewind($pfile);

    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {
            // Användaren finns, kolla lösenord
            if (trim($tmp[1]) == trim(md5($pass))){
            	$validUser= true;
            	$_SESSION['userName'] = $user;
            }
            break;
        }
    }
    fclose($pfile);

    if ($validUser != true) $errorText = "Ogiltigt användarnamn eller lösenord!";
    
    if ($validUser == true) $_SESSION['validUser'] = true;
    else $_SESSION['validUser'] = false;

return $errorText;	
}

function logoutUser(){
unset($_SESSION['validUser']);
unset($_SESSION['userName']);
}

function checkUser(){
if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
	header('Location: loggain.php');
}
}

?>

 

 

Loggain.php (login.php in English)

 

 

<?php
require_once('gemensam.php');

$error = '0';

if (isset($_POST['submitBtn'])){
// Inmatning
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
        
// Försök att logga in användaren
$error = loginUser($username,$password);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Inloggningssystem</title>
<link rel="stylesheet" type="text/css" href="stilmall.css" />
</head>
<body>
    
<?php if ($error != '') {?>
      <h1>Inloggning</h1>
     
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="loginform">
        <p>
         Användarnamn: <br /><input class="text" name="username" type="text"  /><br /><br />
          Lösenord:<br /> <input class="text" name="password" type="password" /><br /><br />
          <input class="text" type="submit" name="submitBtn" value="Login" />
         </p>
      </form>
                 
        <a href="registrera.php">Registrera</a>

      
<?php 
}   
    if (isset($_POST['submitBtn'])){

?>
     Resultat:</br>
     
     
<?php
if ($error == '') {
	echo "Welcome $username! <br/>Du är nu inloggad!<br/><br/>";
	echo '<a href="loggaut.php">Logga ut!</a>';
}


?>

<?php            
    }
?>

</body>   

 

 

Registrera.php ( register.php in English)

 

<?php
require_once('gemensam.php');

if (isset($_POST['submitBtn'])){
	// Get user input
	$username  = isset($_POST['username']) ? $_POST['username'] : '';
	$password1 = isset($_POST['password1']) ? $_POST['password1'] : '';
	$password2 = isset($_POST['password2']) ? $_POST['password2'] : '';
        
	// Try to register the user
	$error = registerUser($username,$password1,$password2);
}	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">

<html>
<head>
   <title>Inloggningssystem</title>
<link rel="stylesheet" type="text/css" href="stilmall.css" />
</head>
<body>
   
    
     <h1>Lägg till användare</h1>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="registerform">
      <p>
         Användarnamn:<br /> <input class="text" name="username" type="text"  /><br /><br />
          Lösenord:<br /> <input class="text" name="password1" type="password" /><br /><br />
          Bekräfta lösenord:<br /><input class="text" name="password2" type="password" /><br /><br />
          <input class="text" type="submit" name="submitBtn" value="Register" />
         </p>
      </form>

        <a href="loggain.php">Logga in om du redan är registrerad</a><br /><br />

<?php   
if ((!isset($_POST['submitBtn'])) || ($error != '')) {

?>

<?php
}
    if (isset($_POST['submitBtn'])){


?>
   Resultat:<br />
      
      
<?php

if ($error == '') {
	echo " Användare: $username har registrerats!<br/><br/>";
	echo ' <a href="loggain.php">Logga in här</a>';

}
else echo $error;

?>

<?php            
    }
?>


</body>   

 

 

loggaut.php (logout.php in English)

 

<?php
require_once('gemensam.php');
logoutUser();
header('Location: loggain.php');
?>	

 

 

 

 

This is all so new to me and I am doing it but don't know what I'm doing!

Warning: fopen(userpwd.txt) [function.fopen]: failed to open stream: Permission denied

 

the problem is probably with the permissions on the file userpwd.txt. you need to update the permissions on that file so that your script can open it. using an ftp program or other, set the permissions on the file to 777. chmod 777.

no it did not work and this is what I get

 

Warning: fopen(/data/apache/www/userpwd.txt) [function.fopen]: failed to open stream: Permission denied in /afs/ltu.se/systemdata/www/students/somroy-0/gemensam.php on line 13

 

Warning: rewind(): supplied argument is not a valid stream resource in /afs/ltu.se/systemdata/www/students/somroy-0/gemensam.php on line 14

 

Warning: feof(): supplied argument is not a valid stream resource in /afs/ltu.se/systemdata/www/students/somroy-0/gemensam.php on line 16

 

Warning: fgets(): supplied argument is not a valid stream resource in /afs/ltu.se/systemdata/www/students/somroy-0/gemensam.php on line 17

 

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.