Jump to content

[SOLVED] Trying to create a simple login function


TheJoey

Recommended Posts

<?php 
session_start();
$usernamematch = $_POST['username'];
$passwordmatch = $_POST['password'];

$fp = fopen("registrationinfo/info.txt","a");

$array = explode(':', $fp);

$username = trim($array[0]); //user-name
$password = trim($array[2]); //password

if ($username == $usernamematch && $password == $passwordmatch)
{
$_SESSION['loginsuccessfull'] = true;
header('location indexsuccessfull.php');
} else {
header('location wronginfo.php');
}

?>

 

but at the moment its doing nothing

just a blank page.

im not sure how to work with txt files this way.

 

any help is much appricatied

Link to comment
Share on other sites

Should i remove the $fp = $fp = fopen("registrationinfo/info.txt","a");

 

??

well with the modified code i still didnt get anything.

<?php 
session_start();
$usernamematch = $_POST['username'];
$passwordmatch = $_POST['password'];

$fp = fopen("registrationinfo/info.txt","a");
$fileData = fread($fp,filesize("registrationinfo/info.txt"));
$array = explode(':', $fileData);

$username = trim($array[0]); //user-name
$password = trim($array[1]); //password

if ($username == $usernamematch & $password == $passwordmatch)
{
$_SESSION['loginsuccessfull'] = true;
header('location indexsuccessfull.php');
} else {
header('location wronginfo.php');
}

?>

Link to comment
Share on other sites

also, can I point out that because you're using a file with the extension .txt if somebody gueses the location and name of this file then they will be able to see all of your usernames/password in their browser in plain text, not very secure at all.

 

you'll need a htaccess file in the same directory with the following in it (Google htaccess):

 

IndexIgnore *.txt

Link to comment
Share on other sites

im still getting a a empty page...

 

ive tried fixing it with soloution's and i keep changing it and its always a blank page

 

heres the complete code.

<?php 
session_start();
$usernamematch = $_POST['username'];
$passwordmatch = $_POST['password'];

$fp = fopen("registrationinfo/info.txt","a+");
$fileData = fread($fp,filesize("registrationinfo/info.txt"));
fclose($fp);
$array = explode(':', $fileData);
$username = trim($array[0]); //user-name
$password = trim($array[1]); //password

if ($username == $usernamematch & $password == $passwordmatch)
{
$_SESSION['loginsuccessfull'] = true;
header('location indexsuccessfull.php');
} else {
header('location wronginfo.php');
}

?>

Link to comment
Share on other sites

ok, do me a favour, change to the following:

 

$fp = fopen("registrationinfo/info.txt","a+");
$fileData = fread($fp,filesize("registrationinfo/info.txt"));
print_r($fileData);
fclose($fp);

 

You will get the headers error again but ignore that and paste the printed data to this thread.

Link to comment
Share on other sites

print_r($username);

print_r($usernamematch);

print_r($password);

print_r($passwordmatch);

 

wont work for me.

What do you mean "wont work for me"? What are you getting?

 

could it be that $usernamematch == $username ?

insted of $username == $usernamematch

By the rules of logic, A = B is the same as B = A.

So $usernamematch == $username and $username == $usernamematch are identical

Link to comment
Share on other sites

Modify

print_r($username);
print_r($usernamematch);
print_r($password);
print_r($passwordmatch);

to

echo '$username=';
print_r($username);
echo '<br />$usernamematch=';
print_r($usernamematch);
echo '<br />$password=';
print_r($password);
echo '<br />$passwordmatch=';
print_r($passwordmatch);
echo '<br />';

That way, you can see whether it's the values you're reading from the file that are wrong, or if it's the $_POST data that isn't right

Link to comment
Share on other sites

so its $_POST that is causing the issues...

ill be having a look.

$_POST['Variable'] is the correct way to post isnt it?

It is the correct way, and remember that the Variable is case-sensitive... it must match the name in your html form exactly.

Try putting print_r($_POST) at the top of your script.

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.