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";
        }

Recommended Answers

All 8 Replies

Member Avatar for diafol

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

teacher doesn't want us using sql

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";
            }
        }

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
}

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";
}

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

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>';
}
Member Avatar for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.