Jump to content

Php login


fonecave

Recommended Posts

hi guys im trying to create a script in my php page that validates a login from a text file

the text file reads like this

 

username

password

username

password and so on all different

 

i want the php page to take the user and password fields and check them against the pairs in the txt file

 

ive got soo far but i cant seem to get any further

 

if the login is valid i want to set a cookie called user with the value of the username for an hour

 

 

here is the code so far can anyone help

 

 

<?php

$check = "0";

if (isset ($_POST['login']))

{

 

$name=trim($_POST['name']);

$password=trim($_POST['password']);

if (empty($name) || empty($password))

{

echo '<form action="login.php" method="post"';

echo 'enctype="multipart/form-data">';

echo '<input type="text" name="user" /><br>';

echo '<br>';

echo '<input type="password" name="password" />'; echo '<br>';

echo '       '; echo '       '; echo '  <input type="submit" ';

echo 'name="login" value="Login" />';

echo '</form>';

echo "<font color='red'>Please Enter Username and Password!</font>";

                          }

 

else

        {

echo "hi";

$file=fopen("userlogincheck21071985.txt","r") ;

     

while ( !feof($file) && $check == "0")

{

$f_user = trim(fgets($file));

$f_password = trim(fgets($file));

 

if($f_user == $user && $f_password == $password)

    {

echo 'login succesful';

      }

else

    {

echo 'login failed';  }

}    } 

}

else

{

echo '<form action="login.php" method="post"';

echo 'enctype="multipart/form-data">';

echo '<input type="text" name="user" /><br>';

echo '<br>';

echo '<input type="password" name="password" />'; echo '<br>';

echo '       '; echo '       '; echo '  <input type="submit" ';

echo 'name="login" value="Login" />';

echo '</form>';

}

 

?>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

basically i want the code to check the text file to see if the posted username and passwords match like i have tried to do and if they do set a cookie as i said earlier i am just at a dead end i cant get any of the code to work.. any chance anyone could sort it out for me?

 

 

Link to comment
Share on other sites

Set this code up as the would do for an .htpasswd file (as those are most efficient in any scenario).

 

username:password or username|password

 

Then, simply use $line = fgets($file) to go line by line

list($username,$password) = explode('[DELIMITER]',$line)..

 

And if $username = $log-in info, then check passwords, etc...

Link to comment
Share on other sites

im new to this and that means nothing to me!!

 

i want to open the file

 

and do something like

 

$user = fgets($file)

$pass = fgets($file)

 

if ($user == $_POST['user'] && $pass == $_POST['password'])

 

set a cookie

 

but i need to loop through the text file so that it checks every 2 lines

 

 

Link to comment
Share on other sites

Get the file formatted as such:

 

user/pass file

user1:pass1
user2:pass2

 

The php file:

function check_user($check_user,$check_pass){
$filename = "some_file.txt";
$file = fopen($filename,'r');
while($line = fgets($file)){//loop through line by line
list($username,$password) = explode(":",$line);//break up each line into the username and password
if($username == $check_user){//we found the corresponding user, let's check the password
if($password == $check_pass){//it matches
setcookie("logged_in",true,time()+3600);
return true;
} else {
echo "I'm sorry, the username/password combination did not match.";
return false;
}
}
}
return false;//always return false by default if we reach here somehow
}

 

Just run the function like so:

 

check_user("bob","mypassword");

Link to comment
Share on other sites

i have this code that seems to work fine if login is correct it prints line "login Successful" otherwise "invalid login" in both fields arent filled in "Please enter username and password!"

 

However where i have the successful login i want to set a cookie but it wont let me me because the php script isnt in the head of the page! what do i do, if i move it up into the head will it still work properly or can i set the cookie in the head if the login was successful below?

 

<?php

if (isset ($_POST['login']))

{
$username=trim($_POST['username']);
$pass=trim($_POST['pass']);
if (empty($username) || empty($pass))
{
echo '<form action="login.php" method="post"'; 
echo 'enctype="multipart/form-data">';
echo '<input type="text" name="username" /><br>';
echo '<br>';
echo '<input type="password" name="pass" />'; echo '<br>';
echo '       '; echo '       '; echo '  <input type="submit" ';
echo 'name="login" value="Login" />';
echo '</form>';
echo "<font color='red'>Please Enter Username and Password!</font>";

}

else


{

$passok = "0";

$file=fopen("userlogincheck21071985.txt","r") ;
       
while ( !feof($file))
{
$f_user = trim(fgets($file));
$f_password = trim(fgets($file));
if ($f_user == $username && $f_password == $pass)
{
$passok = "1";
}
}


echo '<form action="login.php" method="post"'; 
echo 'enctype="multipart/form-data">';
echo '<input type="text" name="username" /><br>';
echo '<br>';
echo '<input type="password" name="pass" />'; 
echo '<br>';
echo '       '; echo '       '; echo '  <input type="submit" ';
echo 'name="login" value="Login" />';
echo '</form>';
if ($passok =="0")
{
echo "<FONT color='red'>Ivalid Login</font>";
}
if ($passok =="1")
{
echo "<FONT color='red'>Login Successful</font>";
}
}

}
else
{
echo '<form action="login.php" method="post"'; 
echo 'enctype="multipart/form-data">';
echo '<input type="text" name="username" /><br>';
echo '<br>';
echo '<input type="password" name="pass" />'; 
echo '<br>';
echo '       '; echo '       '; echo '  <input type="submit" ';
echo 'name="login" value="Login" />';
echo '</form>';
}




?>

Link to comment
Share on other sites

Set the cookie before output

so move the script to the start of the file and see code below comment

 

<?php

if (isset ($_POST['login']))

{
$username=trim($_POST['username']);
$pass=trim($_POST['pass']);
if (empty($username) || empty($pass))
{
echo '<form action="login.php" method="post"'; 
echo 'enctype="multipart/form-data">';
echo '<input type="text" name="username" /><br>';
echo '<br>';
echo '<input type="password" name="pass" />'; echo '<br>';
echo '       '; echo '       '; echo '  <input type="submit" ';
echo 'name="login" value="Login" />';
echo '</form>';
echo "<font color='red'>Please Enter Username and Password!</font>";

}

else


{

$passok = "0";

$file=fopen("userlogincheck21071985.txt","r") ;
       
while ( !feof($file))
{
$f_user = trim(fgets($file));
$f_password = trim(fgets($file));
if ($f_user == $username && $f_password == $pass)
{
$passok = "1";
//ADD COOKIE HERE
setcookie("login", $username, time()+3600); //whatever!
}
}


echo '<form action="login.php" method="post"'; 
echo 'enctype="multipart/form-data">';
echo '<input type="text" name="username" /><br>';
echo '<br>';
echo '<input type="password" name="pass" />'; 
echo '<br>';
echo '       '; echo '       '; echo '  <input type="submit" ';
echo 'name="login" value="Login" />';
echo '</form>';
if ($passok =="0")
{
echo "<FONT color='red'>Ivalid Login</font>";
}
if ($passok =="1")
{
echo "<FONT color='red'>Login Successful</font>";
}
}

}
else
{
echo '<form action="login.php" method="post"'; 
echo 'enctype="multipart/form-data">';
echo '<input type="text" name="username" /><br>';
echo '<br>';
echo '<input type="password" name="pass" />'; 
echo '<br>';
echo '       '; echo '       '; echo '  <input type="submit" ';
echo 'name="login" value="Login" />';
echo '</form>';
}




?>

Link to comment
Share on other sites

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.