【问题标题】:Stored cookie dissapearing after captive portal authentication强制门户身份验证后存储的 cookie 消失
【发布时间】:2019-05-16 23:19:44
【问题描述】:

强制性的“我是新手”。

我遇到了一个问题,即在强制门户登录后 cookie 似乎没有持续存在。 我在我们的网络服务器上设置了一个强制门户,在 2 个 cookie 上存储 mac 和唯一 ID,将它们重定向到其他地方进行身份验证,然后它们返回到我们的网络服务器,我们在其中查找该 uid 并返回一个唯一代码. 在windows电脑上测试它可以正常工作,但在android手机上却不行。 似乎在Android手机上我可以看到cookie已设置,在默认浏览器上你可以使用wifi热点,因为我已经让那个页面回显了这个值。 但是当我在 chrome 浏览器中手动输入 url 时,它显示 cookie 不再设置为任何值。 在 2 部 android 手机和我的 windows 笔记本电脑上进行了测试(再次正常工作)。 对此有何想法?

这可能看起来像一团糟。

这是他们登陆的第一页:

$sql = "select uid from table where mac =('$client_mac_address')";
$uidResult = $conn->query($sql);
if($uidResult->num_rows > 0) {
    while($row = mysqli_fetch_array($uidResult)) {
        $cookie_uid = "UID";
        echo $row['uid'];
        setcookie($cookie_uid,$row['uid'],time() + (86400 * 30),"/");
        if(!isset($_COOKIE[$cookie_uid])) {
            echo "Cookie named '" . $cookie_uid . "' is not set!";
        } else {
            echo "Cookie '" . $cookie_uid . "' is set!<br>";
            echo "Value is: " . $_COOKIE[$cookie_uid];
            echo $_COOKIE[$cookiename];
        }
    }
};

这是他们登陆的最后一页 我先得到他们的 MAC,然后用它来查找 uid:

<?php

$cookiename = "UID";
if(!isset($_COOKIE[$cookiename])) {
    echo "Cookie '" . $cookiename . "' isn't set <br>";
} else {
    echo "Cookie '" . $cookiename . "' is set <br>";
    echo "Value: " . $_COOKIE[$cookiename];
}

?>

<script>
function getCookie(UID) {
  var name = UID + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i <ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

function getCode(){
    var uid = getCookie("UID");
    if (uid == "") {
        return;
        console.log("empty");
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("divCode").innerHTML = this.responseText;
            console.log("on ready");
        }
    };
    xmlhttp.open("GET","getCode.php?q="+uid,true);
    console.log("open");
    xmlhttp.send();
    console.log("send");
    }           
}

</script>

转到服务器上的一个 php 文件来处理 get 请求:

$q = intval($_GET['q']);
echo "$q";
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if (!$conn) {
    die('can't connect: ' . mysqli_error($conn));
}
mysqli_select_db($conn,"table");
$sql="SELECT code FROM table WHERE uid = ".$q;
$result = mysqli_query($conn,$sql);

echo "<table>
<tr>
<th>Code</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['code'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($conn);

【问题讨论】:

    标签: javascript php android cookies portal


    【解决方案1】:

    Captive Portal 将 cookie 存储在沙盒环境中,它不与 android 和 ios 中的普通浏览器共享。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 2017-10-30
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      相关资源
      最近更新 更多