【问题标题】:PHP server refuses to set cookie (not output issue)PHP服务器拒绝设置cookie(不是输出问题)
【发布时间】: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, '/');。让我知道回应
  • 您的代码存在一些重大的安全漏洞。
  • 除了偷工减料以强制应用程序运行的明显影响之外,还有哪些其他安全漏洞?

标签: php http cookies


【解决方案1】:

看起来您没有将 path 参数设置为 set-cookie。在这种情况下,cookie 只会设置为您的current directory

设置 cookie 的基本语法。

setcookie(name,value,expire,path,domain,secure,httponly);

如果路径设置为“/”,cookie 将在整个域中可用。 更多详情php setcookie

【讨论】:

  • 我曾经这样做过。它没有用。加上这个所属的文件已经在网站的根目录中,所以 root = current。
  • 好吧,我也遇到了同样的问题,但用上面的解决方案解决了。设置它们后尝试print_r($_COOKIE);,并检查不同页面上的浏览器设置。
猜你喜欢
  • 2013-07-31
  • 2014-12-03
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多