我的网站有一个post方法表单就像下面:
<html>
<body>
<form action="login.php" method="post">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>
如果有人调用login.php直接传递如下URL中的参数(强制get方法),这也会起作用吗?
www.mysite.com/login.php?username=123456&password=123456
这取决于login.php
…是用$_POST
、$_GET
还是$_REQUEST
?
不建议使用$_GET
作为登录表单,这是非常糟糕的做法。
您可以独立访问它们。
if(isset($_POST['username'])){
$username = $_POST['username'];
}
else{
$username = $_GET['username'];
}
$_GET用于登录并不理想。这个函数将从$_GET或$_POST中获取值。
function get($name){
if(isset($_POST[$name])){
return $_POST[$name];
}else if(isset($_GET[$name])){
return $_GET[$name];
}
return null;
}
对于密码使用$_GET并不理想,因为任何人都可以在浏览器中看到它,无论如何,这很容易做到。您所要做的就是将表单标签更改为以下内容:
<form action="login.php" method="get">
你可以看到方法现在是get而不是post。在php中使用$_GET
而不是$_POST
如果你希望你的php脚本的选项使用POST或GET,你总是可以检查是否设置了GET,如果是使用GET,否则检查是否有POST。因此,使用表单将使用POST(更安全),而在url中将使用GET。试试下面的代码:
<?php
if(isset($_GET['username']) && isset($_GET['password'])){
$username = $_GET['username'];
$password = $_GET['password'];
$submitted = true;
}
else if(isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
$submitted = true;
}
if($submitted){
// Do your code here..
// $username will give the username and $password will give the password.
}
?>
希望有帮助!