質問

This might be a rather weird question, but when I log in to my webpage, it works, even if I write it all in CAPITALS. Anyway, I've made a "kill" function on the page (Role playing game where you can kill others). And I've tried to make a simple function to make you unable to kill yourself. Here is the code:

        if ("$killuser"=="$user") {
        echo "You can't kill yourself!";
        }

$user = The current user logged in, trying to use the function

$killuser = The input in the HTML form (the user you are trying to kill)

The problem is, if my username is "User", and I attempt to kill "User", it says I can't kill myself, but if I write it in capitals, or not EXACTLY as it's stored in the database, it won't, and I'll be able to kill myself. By the way, the username table in the database is a VARCHAR and collation is utf8_general_ci.

役に立ちましたか?

解決

Convert both variables to lower-case and then compare it:

if ( strtolower($killuser) == strtolower($user)) {
    echo "You can't kill yourself!";
}

Alternatively, you could also use strcasecmp():

if (strcasecmp($killuser, $user) == 0) {
    echo "You can't kill yourself!";
}

他のヒント

if (strtolower($killuser) == strtolower($user)) {
    echo "You can't kill yourself!";
}

just make everything lowercase and you won't have an issue.

Try using strcasecmp

if (strcasecmp($killuser, $user) == 0) {
    echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}

try this without quotes

if ($killuser == strtolower($user)) {

This will make it in lower caps

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top