【发布时间】:2017-11-02 02:20:46
【问题描述】:
我正在参加一门课程,该课程让我使用公共服务器将我的项目上传到。问题是服务器拒绝运行 setcookie(),即使我笔记本电脑上的本地 MAMP 实例运行它。
此脚本位于它所属文件的开头。它前面没有 echo 语句或 HTML 标记。
我什么都试过了。我使用了输出缓冲,我尝试过使用会话但失败了。我什至注释掉了 die() 语句。奇怪的是,我使用的一个 cookie 测试脚本运行得很好。有什么办法可以解决这个我还没有想到的问题?
与此同时,我会寻找一种方法来共享服务器的设置。
<?php
$success = false;
$error = false;
$err_msg = "";
$values = array('username', 'password');
if(isset($_POST['submit'])) {
include 'connect.php';
/*
* Variable introduced: $connect
* Close $connect at the end of the script.
*/
// check for existence
foreach($values as $value) {
if(!isset($_POST[$value])) {
$error = true;
$err_msg .= "{$value} is empty!<br>\n";
}
}
if($error === false) {
// sanitize
foreach($values as $value) {
if(gettype($_POST[$value]) === "string") {
// NOTE: Reassignment of values in $_POST to sanitized output.
$_POST[$value] = filter_var($_POST[$value], FILTER_SANITIZE_STRING);
}
}
// validate
if( preg_match("/^(?!.*\.\.)(?!.*\.$)[^\W][\w.]{0,29}$/", $_POST['username']) == 0 ) {
$error = true;
$err_msg .= "Username is in the wrong format.<br>\n";
}
if( preg_match('/^\S{7,}$/', $_POST['password']) == 0 ) {
$error = true;
$err_msg .= "Password is in the wrong format.<br>\n";
}
if($error === false) {
//check username existence
$query = "select userID from users where username = '{$_POST['username']}'";
$result = mysqli_query($connect, $query);
if ( mysqli_num_rows($result) <= 0 ) {
$error = true;
$err_msg .= "Username does not exist.<br>\n";
}
if($error === false){
//Check passwords
$query = "select count(userID) as count from users where username = '{$_POST['username']}' and passkey = '{$_POST['password']}'";
$result = mysqli_query($connect, $query);
if(!$result){
die("Connection failed: " . mysqli_error($connect));
} elseif (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
if($row['count'] == 1){
$success = true;
$error = false;
/*** This is the failure point ***/
setcookie('username', $_POST['username'], time() + 86400);
} else {
$error = true;
$success = false;
$err_msg .= "Wrong password.<br>\n";
}
}
}
}
}
}
mysqli_close($connect);
}
?>
【问题讨论】:
-
试试
setcookie('username', $_POST['username'], time() + 86400, '/');。让我知道回应 -
您的代码存在一些重大的安全漏洞。
-
除了偷工减料以强制应用程序运行的明显影响之外,还有哪些其他安全漏洞?