Login

General discussions related to php

Moderators: macek, egami, gesf

websak123
New php-forum User
New php-forum User
Posts: 1
Joined: Tue Aug 07, 2012 2:29 am

Login

Postby websak123 » Tue Aug 07, 2012 2:34 am

Hello can anyone help i have created a login script where by it reads from a text file. I have created a login in user and password. When i go to login it comes up with username and password do not exist but they do exist. I Can see this in the text file. Can anyone help me please???

Code: Select all

<?php
session_start(); // start session

$pagetitle = "Login Page";

if (isset ($_POST['submit'])) {

   //$_SESSION['LoggedIn'] = false;
   $file = fopen('users.txt', 'rb');
   while ($line = fgetcsv($file,100,",")) {
   
   if (($line[0] == $_POST['username']) AND ($line[1] == ($_POST['password']) ) ) {
      $_SESSION['LoggedIn'] = true; // set login session successful!
      }
    }

if ($_SESSION['LoggedIn']) {
   $_SESSION['username'] = $_POST['username'];
   header("Location: content3.php"); //once logged in redirect to content page
   exit;

} else {
   echo '<br>Sorry there seems to be a problem your username and password did not match!';
   
   }
}
      
include_once('template/header.inc.php');

?>
       
       <h1>Login</h1>
      <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
         Username: <input type="text" name="username">
         <br /><br />
         Password: <input type="password" name="password">
         <br /><br />
         
         <input type="submit" name="submit" value="Login">
         <p>Not a user please register here <a href="registration.php">Register</a></p>
      </form>
      
<? include_once('template/footer.inc.php'); ?>

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Login

Postby Nullsig » Tue Aug 07, 2012 4:03 am

There isn't an 'rb' modifier for fopen. That means more than likely your file isn't even being opened and read.

Your lack of error trapping would be why you have had difficulty finding the error.

You can check this by just changing this line:

Code: Select all

$file = fopen('users.txt', 'rb');


To This:

Code: Select all

if($file = fopen('users.txt', 'rb')){

}else{
     echo "Your file failed to open and you should feel bad about that.";
}


Return to “PHP General”

Who is online

Users browsing this forum: Google [Bot] and 2 guests