You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Christopher Deeley <ch...@gmail.com> on 2005/12/28 01:55:37 UTC
[users@httpd] Form Login Script
I am trying to use the following script to verify my users against a text
file where each line is in the format username:password.
This is my script below. It worked fine until I started adding more users to
the text file and it wouldn't recognise them. It will only show as a valid
user if it matches the last entry in the text file.
<?PHP
session_start();
$usr = $_POST['username'];
$pass = $_POST['password'];
$filename = 'users.txt';
$fp = fopen( $filename, 'r' );
$file_contents = fread( $fp, filesize( $filename ) );
fclose( $fp );
$lines = explode ( "\n", $file_contents );
foreach ( $lines as $line ){
list( $username, $password ) = explode( ':', $line );
}
if($username !== $usr){
echo 'invalid username';
die;
}
if ( ( $usr == $username ) &&
( $pass == $password ) ) {
echo 'Valid User. Login Successful';
}
else{
echo 'Invalid Password';
}
?>
Re: [users@httpd] Form Login Script
Posted by Boysenberry Payne <bo...@humaniteque.com>.
Try:
<?php
session_start();
function response( $text ) {
echo $text;
return;
}
$bad_user = "Invalid User Name";
$bad_pass = "Invalid Password";
$good_log = "Valid User. Login Successful";
$usr = $_POST['username'];
$pass = $_POST['password'];
$valid_match = "/([a-z0-9-_])+/i";
if( !preg_match( $valid_match, $usr ) ) {
return response( $bad_user );
} elseif( !preg_match( $valid_match, $pass ) ) {
return response( $bad_pass );
}
$users = file_get_contents( "users.txt" );
$results = array();
$valid_match = "/($usr):($pass)?/";
if( preg_match( $valid_match, $users, $results ) ) {
if( !$results[ 2 ] ) {
return response( $bad_pass );
} else {
return response( $good_log );
}
} else {
return response( $bad_user );
}
?>
Boysenberry
boysenberrys.com | habitatlife.com | selfgnosis.com
On Dec 27, 2005, at 6:55 PM, Christopher Deeley wrote:
> I am trying to use the following script to verify my users against a
> text file where each line is in the format username:password.
>
> This is my script below. It worked fine until I started adding more
> users to the text file and it wouldn't recognise them. It will only
> show as a valid user if it matches the last entry in the text file.
>
>
> <?PHP
> session_start();
> $usr = $_POST['username'];
> $pass = $_POST['password'];
>
> $filename = 'users.txt';
> $fp = fopen( $filename, 'r' );
> $file_contents = fread( $fp, filesize( $filename ) );
> fclose( $fp );
> $lines = explode ( "\n", $file_contents );
> foreach ( $lines as $line ){
> list( $username, $password ) = explode( ':', $line );
> }
>
> if($username !== $usr){
> echo 'invalid username';
> die;
> }
>
> if ( ( $usr == $username ) &&
> ( $pass == $password ) ) {
> echo 'Valid User. Login Successful';
> }
> else{
> echo 'Invalid Password';
>
> }
>
> ?>
>
>
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org