0

I am storing a cookie with multiple values from an asp.net page (VB). I am then trying to retrieve the values from the cookie in PHP and can't find a way to get the multiple values using PHP

$myRole is only returning "R" not "admin"

php full cookie var_dump($myCookie); is string(48) "Role=admin&User=myuser&Time=3/8/2017 8:08:43 PM"

ASP.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim sb As New StringBuilder()
     Dim cookie As HttpCookie
    cookie = Request.Cookies.Get("PHPAUTH")

    If (cookie Is Nothing) Then
        sb.Append("Cookie was not received from the client. ")
        sb.Append("Creating cookie to add to the response. <br/>")
        cookie = New HttpCookie("PHPAUTH")


        If Not Roles.IsUserInRole(User.Identity.Name, "admin") Then
            'cookie.Values("Role") = "not in role"
        Else
            cookie.Values("Role") = "admin"
            cookie.Values("User") = User.Identity.Name
        End If 

        cookie.Values("Time") = DateTime.Now.ToString()
        cookie.Expires = DateTime.Now.AddMinutes(10D)
        Response.Cookies.Add(cookie)
    Else
        sb.Append("Cookie retrieved from client. <br/>")
        sb.Append("Cookie Name: " + cookie.Name + "<br/>")
        sb.Append("Cookie User: " + cookie.Values("User") + "<br/>")
        sb.Append("Cookie Role: " + cookie.Values("Role") + "<br/>")
        sb.Append("Cookie Time: " + cookie.Values("Time") + "<br/>")
        sb.Append("Cookie Expiration Date: " & _
            cookie.Expires.ToString() & "<br/>")
    End If

    Label1.Text = sb.ToString()

End Sub

PHP:

$myCookie = $_COOKIE["PHPAUTH"];
$myRole = $myCookie["Role"];
var_dump($myCookie);
var_dump($myRole);
3
  • What does var_dump($_COOKIE["PHPAUTH"]); return? If it's empty, your cookie isn't getting set. Commented Mar 9, 2017 at 4:14
  • what is the output of echo "<pre/>";print_r($_COOKIE); at php end? Commented Mar 9, 2017 at 4:17
  • the output is in my question $myCookie Commented Mar 9, 2017 at 4:19

1 Answer 1

0

seems the cookie string needs to be parsed into an array:

PHP Set and Read a Cookie String

my solution was:

$myCookie = $_COOKIE["PHPAUTH"];
parse_str($myCookie, $prefs);
var_dump($prefs);
echo print_r($prefs['Role']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.