User Authentication

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Xelmepa
Forum Commoner
Posts: 41
Joined: Sat Aug 24, 2002 3:02 pm
Location: Athens, Greece
Contact:

User Authentication

Post by Xelmepa »

Hey,
I am doing a user authentication script.
I am trying to see whether the given from a form username and password exist in any pair of a text file.

text file:
xelmepa::12345
nickp::54321

How can I see if the submitted username and password exist in one pair?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

One way could be to use file() to read the text file into an array, then use foreach() and explode() to separate the username and password and in_array() to check to see if the entered values exist as a pair.

Mac
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

<?
$username = $_POST&#1111;'username']; // user input
$password = $_POST&#1111;'password']; // user input
$array = file('thetextfile.txt');
foreach($array as $value)&#123;
  $temp = explode("::", $value);
  if($username != $temp&#1111;'0'] || $password != $temp&#1111;'1'])&#123;
  die('Invalid Username/Password');
  &#125;
&#125;
// user is logged in
?>
User avatar
Xelmepa
Forum Commoner
Posts: 41
Joined: Sat Aug 24, 2002 3:02 pm
Location: Athens, Greece
Contact:

Post by Xelmepa »

I just did the following and the $_POST variables appear to be empty for some reason...

I used tabs (\t) instead of :: this time as a divider between username and pswd and newlines between sets of un and pwd.

Code: Select all

<?php
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	$fp = fopen("db.txt", "r");
	
while ($userinfo = fscanf ($fp, "%s\t%s\n")) &#123;
    list ($uname, $pwd) = $userinfo;
    if (($user_name == $uname) && ($password == $pwd)) &#123;
echo "succesful login!";
&#125;
&#125;
fclose($fp);
?>
[/quote]
User avatar
Xelmepa
Forum Commoner
Posts: 41
Joined: Sat Aug 24, 2002 3:02 pm
Location: Athens, Greece
Contact:

Post by Xelmepa »

Anyone got a clue why the $_POST array is empty?
I am using method=post
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

are you aware of this -> viewtopic.php?t=511
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Coco wrote:are you aware of this -> viewtopic.php?t=511
that has nothing to do with his question.
Xelmepa wrote:Anyone got a clue why the $_POST array is empty?
I am using method=post
Try $HTTP_POST_VARS
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you are using PHP 4.0.6 or below use $HTTP_POST_VARS (this array is also available on PHP > 4.0.6), if you are using PHP 4.1 or above $_POST should work.

Mac
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

If $_POST[""]; and $HTTP_POST_VARS[""]; are "empty" or will not work, then maybe there is something wrong in your PHP or server configuration?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Yes, such as 'track-vars' in php ini
User avatar
Xelmepa
Forum Commoner
Posts: 41
Joined: Sat Aug 24, 2002 3:02 pm
Location: Athens, Greece
Contact:

Post by Xelmepa »

Yea I figured after I asked it in here. Thanks though.
And yes I am aware of that topic pretty much, that's why I am using the POST vars ;)
Post Reply