Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
1536a682
Commit
1536a682
authored
Feb 27, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cookie, request, response WIP
parent
4ec96cd6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
8 deletions
+80
-8
Cookie.php
framework/web/Cookie.php
+16
-0
CookieCollection.php
framework/web/CookieCollection.php
+0
-0
Request.php
framework/web/Request.php
+40
-8
Response.php
framework/web/Response.php
+24
-0
No files found.
framework/web/Cookie.php
View file @
1536a682
...
...
@@ -48,4 +48,20 @@ class Cookie extends \yii\base\Object
* such as JavaScript, which can effectively help to reduce identity theft through XSS attacks.
*/
public
$httpOnly
=
false
;
/**
* Magic method to turn a cookie object into a string without having to explicitly access [[value]].
*
* ~~~
* if (isset($request->cookies['name'])) {
* $value = (string)$request->cookies['name'];
* }
* ~~~
*
* @return string The value of the cookie. If the value property is null, an empty string will be returned.
*/
public
function
__toString
()
{
return
(
string
)
$this
->
value
;
}
}
framework/web/CookieCollection.php
View file @
1536a682
This diff is collapsed.
Click to expand it.
framework/web/Request.php
View file @
1536a682
...
...
@@ -695,21 +695,53 @@ class Request extends \yii\base\Request
return
isset
(
$languages
[
0
])
?
$languages
[
0
]
:
false
;
}
/**
* Returns the cookie collection.
* The result can be used like an associative array. Adding {@link CHttpCookie} objects
* to the collection will send the cookies to the client; and removing the objects
* from the collection will delete those cookies on the client.
* @return CCookieCollection the cookie collection.
* Through the returned cookie collection, you may access a cookie using the following syntax:
*
* ~~~
* $cookie = $request->cookies['name']
* if ($cookie !== null) {
* $value = $cookie->value;
* }
*
* // alternatively
* $value = $request->cookies->getValue('name');
* ~~~
*
* @return CookieCollection the cookie collection.
*/
public
function
getCookies
()
{
if
(
$this
->
_cookies
!==
null
)
{
return
$this
->
_cookies
;
if
(
$this
->
_cookies
===
null
)
{
$this
->
_cookies
=
new
CookieCollection
(
$this
->
loadCookies
());
}
return
$this
->
_cookies
;
}
/**
* Returns the current cookies in terms of [[Cookie]] objects.
* @return Cookie[] list of current cookies
*/
protected
function
loadCookies
()
{
$cookies
=
array
();
if
(
$this
->
enableCookieValidation
)
{
$sm
=
Yii
::
app
()
->
getSecurityManager
();
foreach
(
$_COOKIE
as
$name
=>
$value
)
{
if
(
is_string
(
$value
)
&&
(
$value
=
$sm
->
validateData
(
$value
))
!==
false
)
{
$cookies
[
$name
]
=
new
CHttpCookie
(
$name
,
@
unserialize
(
$value
));
}
}
}
else
{
return
$this
->
_cookies
=
new
CCookieCollection
(
$this
);
foreach
(
$_COOKIE
as
$name
=>
$value
)
{
$cookies
[
$name
]
=
new
Cookie
(
array
(
'name'
=>
$name
,
'value'
=>
$value
,
));
}
}
return
$cookies
;
}
private
$_csrfToken
;
...
...
framework/web/Response.php
View file @
1536a682
...
...
@@ -161,4 +161,28 @@ class Response extends \yii\base\Response
Yii
::
app
()
->
end
();
}
}
/**
* Returns the cookie collection.
* Through the returned cookie collection, you add or remove cookies as follows,
*
* ~~~
* // add a cookie
* $response->cookies->add(new Cookie(array(
* 'name' => $name,
* 'value' => $value,
* ));
*
* // remove a cookie
* $response->cookies->remove('name');
* // alternatively
* unset($response->cookies['name']);
* ~~~
*
* @return CookieCollection the cookie collection.
*/
public
function
getCookies
()
{
return
\Yii
::
$app
->
getRequest
()
->
getCookies
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment