0

How to I check for existing username without using mysql in php?
I tried something like this but it isnt working. Please help. Thanks. Strictly no SQL...

     $name=$nme;
        $name = file("datatext/users.txt");
        $ic=file("datatext/users.txt");
        if (this.$nme== $name)
        {
        echo "this user exits, please log in";
        }
6
Contributors
8
Replies
54
Views
4 Years
Discussion Span
Last Post by diafol
0

This is not secure. You need to have the data in a php file, hashed and not readable unless given permission by another file. It should reside above the public root. This isn't recommended. Why can't you use MySQL?

if (this.$nme== $name)

Looks like js-php mashed. Not php anyway

Edited by diafol

0

If file contains a name on every new line then this will work
Try this one
hi.txt contains name like
xia
aix

$nm = "xia";
    $name = file("hi.txt");
    //$name is an array of name print_r $name to see what it contains
       foreach ($name as $test) {
            if (strcasecmp(trim($test),$nm) == 0) {
                echo "this user exits, please log in";
            }
        }

Edited by code_rum

2

No need to loop them all:

$nm = 'xia';
$names = file('hi.txt', FILE_IGNORE_NEW_LINES);
if (in_array($nm, $names))
{
    // user exists
}
else
{
    // not found
}

Edited by pritaeas

0

A small addition to priteas code.
For Case-insensitive

function in_array_case_insensitive($needle, $haystack) {
     return in_array( strtolower($needle), array_map('strtolower', $haystack) );
}
$nm = 'xia';
$names = file('hi.txt', FILE_IGNORE_NEW_LINES);
if (in_array_case_insensitive($nm, $names)) {
    //echo "user exists";
} else {
    //echo "not found";
}
0

If you are allowed to use a php to store the names instead of a txt file.....

users.php

<?php 

$users = array(
    'paulkd' => array(
        'firstname' => 'Paul',
        'location' => 'Preston'
    ),
    'xianamersu' => array(
        'firstname' => 'Rachel',
        'location' => 'Melbourne'
    )
);

?>

and to check....

<?php
include_once 'users.php';

$usernameToCheck = 'paulkd';

if(array_key_exists($usernameToCheck,$users)) {
    echo 'found';
} else {
    echo 'not found';
}
?>
0

If you are not allowed to use a SQL database, use a NoSQL database, your teacher won't be happy but you have done as he asked ;).

Here's another method:

<?php
$file_containing_names = 'file.txt';
$file_contents = file_get_contents($file_containing_names);
$name_to_search_for = 'annie';
$regex = '/' . $name_to_search_for . '/i';

if(preg_match($regex, $file_contents, $matches, PREG_OFFSET_CAPTURE))
{
    $match = $matches[0][0];
    $offset = $matches[0][1];

    echo '<p>Name <em>' . $match . '</em> was found at offset ' . $offset . '.</p>';
}
else
{
    //  The name was not found.
}

And, with pritaeas's example you could also report in which line the name was found, if there is one name per line:

$search_for = 'xia';
$names = file('hi.txt', FILE_IGNORE_NEW_LINES);

for($i = 0; $i<count($names); $i++)
{
    $username = strtolower($names[$i]); 

    if(stripos($username, $search_for)) // Make sure we do a case-insensitive check.
    {
        $has_results = true;
        echo '<p>Username found in line ' . ($i + 1) . '</p>';
        break;
    }
}

if(!$has_results)
{
    echo '<p>The username was not found.</p>';
}

Edited by minitauros

0

Hmm, this looks like a school project. Maybe it wopuld be best to steer the OP to a solution rather than to provide one?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.