0

I am trying to remove spaces from username while user logged in. for example if they have username like TEST BHA (there is space between TEST AND BHA)i want that username should be TESTBHA without space and it has to take lowercase while user logged in itself.

I have found some solutions but those are not working in my condition. Can anyone help me how to resolve this issue..

Here is my username:

 if (isset($_POST['signin'])) {

        global $DB;

         $username = str_replace(" ", "", $_POST['username']);
         $password = $_POST['password'];
         $sql = "SELECT * FROM {user}  where username = ?";

        if ($user = $DB->get_record_sql($sql, array($username))) {
            //echo $user->password;
            //exit;
            if (password_verify($password, $user->password)) {
                if ($password != 'changeme'){
                  if ($user->trackforums == 1) {
                    complete_user_login($user);
                    \core\session\manager::apply_concurrent_login_limit($user->id, session_id());
                    $userauth = get_auth_plugin($USER->auth);

                    $DB->set_field('user', 'firstaccess', date('YmdHis'), array('id' => $user->id));
                    $_SESSION['username'] = $user->username;
                    $_SESSION['firstname'] = $user->firstname;
                    $_SESSION['idnumber'] = $user->idnumber;
                    $_SESSION['id'] = $user->id;
                    $_SESSION['clientid'] = $user->clientid;
                    $_SESSION['maildigest'] = $user->maildigest;
                    $_SESSION['skype'] = $user->skype;
                    $_SESSION['can_access'] = true;
                    $_SESSION['mnethostid'] = 1;
                    $_SESSION['confirmed'] = 1;

                    if (!empty($_POST["remember"])) {
                        setcookie("member_login", $_POST["username"], time() + (10 * 365 * 24 * 60 * 60));
                        setcookie("password", $_POST["password"], time() + (10 * 365 * 24 * 60 * 60));
                    } else {
                        if (isset($_COOKIE["member_login"])) {
                            setcookie("member_login", "");
                        }
                        if (isset($_COOKIE["password"])) {
                            setcookie("password", "");
                        }
                    }

                    if ($user->idnumber == '3')
                        header('location:course.php');
                    elseif ($user->idnumber == '2')
                        header('location:course.php');
                    else
                        header('location:course.php');
                }

                else {
                    ?>
                    <div class="alert alert-danger">
                        <strong> Sorry, User has been Deactivated. Contact Administrator</strong>
                    </div>
                    <?php
                }
            }
                else {
                    ?>
                    <div class="alert alert-primary">
                        <strong>Please change your password! By clicking this link <a style="color:red" href="forgot-password.php">Click Here</a></strong>
                    </div>
                    <?php
                }
            } else {
                ?>
                <div class="alert alert-danger">
                    <strong> Sorry, wrong password.</strong>
                </div>
                <?php
            }
        } else {
            ?>
            <div class="alert alert-danger">
                <strong> Sorry, wrong username.</strong>
            </div>
            <?php
        }
    }
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
suresh
  • 439
  • 3
  • 18
  • 3
    Possible duplicate of [To strip whitespaces inside a variable in PHP](https://stackoverflow.com/questions/1279774/to-strip-whitespaces-inside-a-variable-in-php) – Drag and Drop Jul 24 '18 at 06:49
  • i have tried this $str=preg_replace('/\s+/', '', $str); but its not working in my case – suresh Jul 24 '18 at 06:50
  • Did you try the obvious thing? https://www.w3schools.com/php/func_string_str_replace.asp – sɐunıɔןɐqɐp Jul 24 '18 at 06:50
  • could you try to `html_entity_decode($string);` before the replace. this space could be an nbsp – Drag and Drop Jul 24 '18 at 06:55
  • may you use the long regex in the duplicate I linked. perhaps the "space" is not a space but one of the white char. – Drag and Drop Jul 24 '18 at 07:01
  • Check the second answer down in https://stackoverflow.com/questions/12837682/non-breaking-utf-8-0xc2a0-space-and-preg-replace-strange-behaviour/12838189#12838189 one of those should get it for you – Robbie Jul 24 '18 at 07:10
  • my problem is in if condition mainly as morgan told – suresh Jul 24 '18 at 07:24

3 Answers3

1

Here:

$username = preg_replace('/\s+/', '', $_POST['username']);
MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
1

you can try str_replace()

<?php 
$no_spaces = str_replace(' ', '', $_POST['username']);
Oleksandr Pobuta
  • 407
  • 5
  • 11
1

just use the str_replace function.

Example: $username = str_replace(" ", "", $_POST['username']);

It takes three arguments first is what to search for second is what to replace with and the third in what string you are searching for

  • Also don't forget to check if the super global is set with the isset() function or if you are using php 7 use the new isset operator. $username = $_POST['username'] ?? ''; – Rimvydas Tamošiūnas Jul 24 '18 at 06:55