[英]simulate web interaction with python php session_start() doesn't work
我有以下 html 頁面
<?php session_start(); ?>
<html>
<body>
<?php
echo '<!-- cookies -->';
foreach($_COOKIE as $k => $v) {
echo '<!-- ' . $k . '=>' . $v . ' -->';
}
?>
</body>
</html>
當我使用https://daley.ws/file1.php查看頁面時,會產生以下頁面源代碼
<html>
<body>
<!-- cookies --><!-- PHPSESSID=>22u20jsi288vtk5epser56d2bn7leb6n --></body>
</html>
以下 python 代碼在我的 mac 上運行:
#!/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
import urllib.request
REQ_HOST = "daley.ws"
DALEY_WS = 'https://daley.ws'
DALEY_WS_FILE1 = '{}/file1.php'.format(DALEY_WS)
DALEY_WS_FILE2 = '{}/file2.php'.format(DALEY_WS)
if __name__ == "__main__":
print(DALEY_WS_FILE1)
req = urllib.request.Request(DALEY_WS_FILE1,\
unverifiable=True, origin_req_host=REQ_HOST,\
method='POST')
with urllib.request.urlopen(req) as response:
data = response.read().decode()
print('data: {}'.format(data))
並產生以下輸出:
https://daley.ws/file1.php
data: <html>
<body>
<!-- cookies --></body>
</html>
為什么 PHPSESSID cookie 不存在? 以及如何修復 python 代碼以便頁面產生正確的輸出。
Python代碼沒有問題。
當您第一次在瀏覽器中加載頁面時,cookie 不在源代碼中。 但是當您刷新它時(第二個請求,但使用Cookie:
)它在源代碼中。
它不存在於 HTML 中,因為 PHP 會話 cookie 不在$_COOKIE
,除非您首先使用 HTTP 標頭Cookie:
發送 cookie。 嘗試 PHP 中的session_id()
或 Python 中的response.getheader("Set-Cookie")
以獲取當前會話 cookie。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.