🔒 Closed Php switch statement

Status
Not open for further replies.

SUNRAI

Eternal Poster
PHP:
<?php

$username1 = "ben";
$password1 = "benpass";
$username2 = "tom";
$password2 = "tompass";
$username3 = "kim";
$password3 = "kimpass";

switch (isset($_POST['username']) && isset($_POST['password']))
{
    case "0": ($_POST['username'] == $username1 && $_POST['password'] == $password1)
        //first user
        echo "Authorization Accepted. <b>$username1</b> login successfully.";
        break;
     case "1": ($_POST['username'] == $username2 && $_POST['password'] == $password2)
        //second user
        echo "Authorization Accepted. <b>$username2</b> login successfully.";
        break;
     case "2": ($_POST['username'] == $username3 && $_POST['password'] == $password3)
        //third user
        echo "Authorization Accepted. <b>$username3</b> login successfully.";
        break;
     case "3":
        //if member 1,2,3 not found
        echo 'Authorization denied!';
    braeak;
 deafult:
    echo 'Please enter username and password to login';
}

?>

<html>

    <body>
        <form name="login" action="" method="post">
            <input type="text" name="username" />
            <input type= "password" name="password" />
            <input type= "submit" name="login" value="login" />
        </form >
    </body>
</html>
 
Umm.. If else yan ginawa mo. Sa switch case, variable lng ang nilalagay. Depende na sa variable kung ano nakalagay doon. Kung loob ng variable ay "3", doon siya mag execute sa [Case "3"]
 
ps: naayos ko na sir problema ko nalang yung code ko para sa authorization denied hindi luamalabas
Code:
<?php

$username1 = "ben";
$password1 = "benpass";
$username2 = "tom";
$password2 = "tompass";
$username3 = "kim";
$password3 = "kimpass";


switch (isset($_POST['username']) && isset($_POST['password']))
{
    case ($_POST['username'] == $username1 && $_POST['password'] == $password1):
        //first user
        echo "Authorization Accepted. <b>$username1</b> login successfully.";
        break;
     case  ($_POST['username'] == $username2 && $_POST['password'] == $password2):
        //second user
        echo "Authorization Accepted. <b>$username2</b> login successfully.";
        break;
     case ($_POST['username'] == $username3 && $_POST['password'] == $password3):
        //third user
        echo "Authorization Accepted. <b>$username3</b> login successfully.";
        break;
     case ($_POST['username'] == false && $_POST['password'] == false):
        //if member 1,2,3 not found
        echo 'Authorization denied!';
        break;
    default:
    echo 'Please enter username and password to login';
}

?>

<html>

    <body>
        <form name="login" action="" method="post">
            <input type="text" name="username" />
            <input type= "password" name="password" />
            <input type= "submit" name="login" value="login" />
        </form >
    </body>
</html>
 
<body>
<form name="login" action="" method="post">
<input type="text" id="username" name="username" />
<input type= "password" id="password" name="password" />
<input type= "submit" name="login" value="login" />
</form >
</body>
 
Status
Not open for further replies.

About this Thread

  • 4
    Replies
  • 363
    Views
  • 4
    Participants
Last reply from:
0xLouis

Online now

Members online
1,015
Guests online
1,751
Total visitors
2,766

Forum statistics

Threads
2,275,217
Posts
28,961,536
Members
1,232,669
Latest member
dhallak879
Back
Top