SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast mildfoam's Avatar
    Join Date
    May 2010
    Location
    Australia.
    Posts
    56
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    need help login with notepad based

    hi all,

    i have to try to check whether my login information is correct or not, it's just contain username and password only. here is my config.txt content :

    user1, user2, user3
    and here is my html index file :

    and here is my code to check login info.

    everything is correct to check if the username was exists in text file but when I type wrong username (another user beside in notepad) it's just show me blank page cause I didn't know what code should i type in there

    what should i do ?
    Object Oriented Programming Fans Boy ?

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Instead of using for and while loop to match username you should just use in_array() instead.

    Example:
    PHP Code:
    if (!in_array($username$users))
    {
        echo 
    "$username is NOT in our records";
        exit;


  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    When u type wrong username, $username == $users[$i] will return false, hence ur while loop will not be executed. Thus u got blank page.

    An alternative without using array,

    PHP Code:

    if(preg_match("/$username/",$content)){
        echo 
    "You are : " $username;
    }else{
        echo 
    "$username not in record.";

    http://www.phpf1.com/tutorial/php-re...xpression.html
    I've never been born, nor will I die.
    I am just a passer-by who stumbled across a planet called Earth,
    for a short period of visiting......

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by leelong View Post
    When u type wrong username, $username == $users[$i] will return false, hence ur while loop will not be executed. Thus u got blank page.

    An alternative without using array,

    PHP Code:

    if(preg_match("/$username/",$content)){
        echo 
    "You are : " $username;
    }else{
        echo 
    "$username not in record.";

    http://www.phpf1.com/tutorial/php-re...xpression.html
    You cannot use your preg_match() example for this match as it will match any characters in user as well as number 1, 2, and 3 for the username.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tom8 View Post
    You cannot use your preg_match() example for this match as it will match any characters in user as well as number 1, 2, and 3 for the username.
    sorry, i didn't read TS codes in detail.
    I've never been born, nor will I die.
    I am just a passer-by who stumbled across a planet called Earth,
    for a short period of visiting......

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    If you use this

    PHP Code:
    $file fopen('config.txt''r');
    $content fgets($file);
    $users array_map('trim'explode(','$content)); 
    people can have spaces in their username if they want

    Also, instead of fopen...fgets I'd use file_get_contents, since you want the whole file anyway.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    corrected,

    PHP Code:
    $file fopen('config.txt''r');
    $content str_replace(' '''fgets($file));
    $users explode(','$content);

    $content str_replace(',''|'$content);

    print_r($users);
    echo 
    '<br /><br />';

    $username $_POST['username'];
    $password $_POST['password'];

    if (
    preg_match("/^($content)$/"$username) === 1) {
        echo 
    "You are : " $username;
    } else {
        echo 
    "$username not in record.";

    I've never been born, nor will I die.
    I am just a passer-by who stumbled across a planet called Earth,
    for a short period of visiting......

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    @leelong ; You need to preg_quote that, otherwise it won't work with certain characters in a username (e.g. a forward slash). Also, why do you prefer this over in_array? In general in_array is a faster than using a regular expressions.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    @leelong ; You need to preg_quote that, otherwise it won't work with certain characters in a username (e.g. a forward slash). Also, why do you prefer this over in_array? In general in_array is a faster than using a regular expressions.
    Normally we don't allow those special characters in username, so not quite an issue.

    Looking at the first post, this thread is about learning PHP rather than "I get paid" for this project so help me, after all who on earth will use notepad based login system?

    Of course, in_array will be the choice. IMHO, providing other approach will be beneficial for learning purpose, nothing else.
    I've never been born, nor will I die.
    I am just a passer-by who stumbled across a planet called Earth,
    for a short period of visiting......


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •