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

You seem to assume that opening a file is the same as reading it.

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

should be something like:

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

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');
}

?>

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

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');
}

?>

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.

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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.