<?php // var_dump($_GET['action']); var_dump($_POST); if ($_GET['action'] == 'login') { if($_POST['name']=='admin' && $_POST['pwd']==123456){ setcookie('username',$_POST['name'],time()+3600*24); header('Location:index.php'); } else { echo '<script> alert("您输入了错误的用户名或密码!");</script>'; } } elseif ($_GET['action']=='logout') { setcookie('name','',time()-3600);//注销cookie } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户登录</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=login" method="post"> <fieldset> <legend>用户登陆:</legend> <label for="name">用户名:</label> <input type="text" name="name" id="name"/> <label for="pwd">密码:</label> <input type="password" name="pwd" id="pwd"/> <input type="submit" value="提交"> </fieldset> </form> </body> </html>
当输入admin 123 时,显示如下:
当输入admin 123456时,报错如下:
我的理解是既然错误的密码在点击提交时可以传递action值进入第一层的if语句后进行判断,为什么正确的用户名和密码在提交时却无法传递action值进入第一层if语句了,甚至post传值也失败了