使cookie在子域中保持不变

时间:2015-04-23 12:56:00

标签: javascript cookies subdomain

我设置了一个cookie:

<script>
  document.cookie="cid1={{utm_campaign}}; path=/;"
</script>

{{}}是一个宏,是Google-Tag-Manager语法,请忽略它。

每当有人使用如下标记登陆example.com时,都会触发上述脚本:example.com/?utm_medium=test&utm_source=bla&utm_campaign=foo。我测试了它,当然,当我使用这些参数登陆主页时,cookie已经设置好了。

但访问者可以转到子域名dogs.example.com。当我查看控制台时,cookie cid1不再存在。

创建Cookie时是否可以更改设置,而不是将路径设置为&#34; /&#34;以便cookie穿过子域?

2 个答案:

答案 0 :(得分:5)

域名应该是.example.com,因此* .example.com可以访问它

var website_host = window.location.hostname.replace('www.', '');
document.cookie = "cid1={{utm_campaign}}; path=/;domain=."+website_host 

// to be something like this"cid1={{utm_campaign}}; path=/;domain=.example.com"

答案 1 :(得分:4)

您为此错过了domain-parameter。将域设置为Dim reason As String = "" Dim banner_action As String = "" Dim esr_link As String = "" Dim seen_message AS String = "" Dim last_updated AS String = "" Dim daysSinceUpdated As Integer Dim intra_user As String = "" Dim banner_flag As String = "" Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs) Dim sc As Web.HttpContext = Web.HttpContext.Current Dim updatePeriod As Integer = 90 Dim strConn As String = getStrConn(sc) Dim rst As DataView Dim strsql, last_updated, email_address As String Dim update_required As String = "" ' - Really don't know what this is for... so I have commented it out. ' - Response.Write("<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />hello no details") ' Check users last update to their email address strsql = "select last_updated, email, esr_link, intra_user, banner_action, seen_message from user_group where id = " & Session("u_id") & " AND (esr_link <> 5)" rst = GetDefaultView(strsql, strConn) If (rst.count > 0) Then ' Get details for new banner check for launching windows at startup If Not IsDBNull(rst(0)("banner_action")) Then banner_action = rst(0)("banner_action").ToString Else banner_action = "" End If ' Get details for seen message If Not IsDBNull(rst(0)("seen_message")) Then seen_message = rst(0)("seen_message").ToString Else seen_message = "" End If ' Gets banner_flag value If Not IsDBNull(rst(0)("banner_flag")) Then banner_flag = rst(0)("banner_flag").ToString Else banner_flag = "" End If ' Gets the ESR link value to check if the New User code needs to run If Not IsDBNull(rst(0)("esr_link")) Then esr_link = rst(0)("esr_link").ToString Else esr_link = "" End If ' Gets the intra_user value If Not IsDBNull(rst(0)("intra_user")) Then intra_user = rst(0)("intra_user").ToString Else intra_user = "" End If ' Grab users last_updated value if available If Not IsDBNull(rst(0)("last_updated")) Then last_updated = rst(0)("last_updated").ToString Else last_updated = "" End If ' Grab users email address if available If Not IsDBNull(rst(0)("email")) Then email_address = rst(0)("email").ToString email_address = Replace(email_address, "'", "") Else email_address = "" End If 'Checks the email address to see if it is valid If email_address <> "" Then If IsEmailValid(email_address) = False Then update_required = "true" reason = "Your current Email address is invalid please update it." & vbCrLf End If Else update_required = "true" reason = "You have not entered a current Email address please add one." & vbCrLf End If 'If it not empty then compare If last_updated <> "" Then last_updated = CDate(last_updated) daysSinceUpdated = DateDiff("d", last_updated, Now()) If daysSinceUpdated > updatePeriod Then update_required = "true" reason &= "Your details have expired please check them to ensure they are up-to-date" & vbCrLf End If 'If it empty then needs updating... Else update_required = "true" reason = "Your details have expired please check them to ensure they are up-to-date" & vbCrLf End If rst.Dispose() rst = Nothing Session("checked_details") = "true" If update_required = "true" Then reason = Server.UrlEncode(reason) 'response.write(reason) 'Response.Write("<scr" & "ipt language='javascript'>findTop().submitURL('/sorce/app_centre/launch_form.aspx?fhandle=User_Details_Amendment&elemid=" & Session("u_id") & "&reason=" & reason & "');</scr" & "ipt>") End If Else 'Response.Write("<br /><br /><br /><br /><br /><br />hello details") 'Response.Write("<scr" & "ipt language='javascript'>findTop().submitURL('/sorce/app_centre/launch_form.aspx?fhandle=User_Details_Amendment&elemid=" & Session("u_id") & "&reason=" & reason & "');</scr" & "ipt>") End If End Sub Function IsEmailValid(ByVal strEmail As String) As Boolean Dim strArray() As String Dim strItem As String Dim i As Integer Dim c As String Dim blnIsItValid As Boolean ' assume the email address is correct blnIsItValid = True ' split the email address in two parts: name@domain.ext strArray = Split(strEmail, "@") ' if there are more or less than two parts If UBound(strArray) <> 1 Then blnIsItValid = False IsEmailValid = blnIsItValid Exit Function End If ' check each part For Each strItem In strArray ' no part can be void If Len(strItem) <= 0 Then blnIsItValid = False IsEmailValid = blnIsItValid Exit Function End If ' check each character of the part ' only following "abcdefghijklmnopqrstuvwxyz_-." ' characters and the ten digits are allowed For i = 1 To Len(strItem) c = LCase(Mid(strItem, i, 1)) ' if there is an illegal character in the part If InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 And Not IsNumeric(c) Then blnIsItValid = False IsEmailValid = blnIsItValid Exit Function End If Next ' the first and the last character in the part cannot be . (dot) If Left(strItem, 1) = "." Or Right(strItem, 1) = "." Then blnIsItValid = False IsEmailValid = blnIsItValid Exit Function End If Next ' the second part (domain.ext) must contain a . (dot) If InStr(strArray(1), ".") <= 0 Then blnIsItValid = False IsEmailValid = blnIsItValid Exit Function End If ' check the length oh the extension i = Len(strArray(1)) - InStrRev(strArray(1), ".") ' the length of the extension can be only 2, 3, or 4 ' to cover the new "info" extension If i <> 2 And i <> 3 And i <> 4 Then blnIsItValid = False IsEmailValid = blnIsItValid Exit Function End If ' after . (dot) cannot follow a . (dot) If InStr(strEmail, "..") > 0 Then blnIsItValid = False IsEmailValid = blnIsItValid Exit Function End If ' finally it's OK IsEmailValid = blnIsItValid End Function ,使其可以从.example.com中的所有页面进行访问。

.example.com

此处复制:setting cross-subdomain cookie with javascript

  

; domain = domain(例如,&#39; example.com&#39;,&#39; .example.com&#39;(包括所有   子域名,&#39; subdomain.example.com&#39;)如果未指定,则默认为   当前文档位置的主机部分。

完整文档:https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie