I am getting following cookie values on my home page.
vsettings=HiddenNotifications=%2C19%2C;
(referral)|utmcmd=referral|utmcct=/;
ASPSESSIONIDQCSBCCAD=LFLGDKACEGPCPFGBANHFFBMK;
ASPSESSIONIDSASDCCDA=OFEHPJACGHNKICDLFBOCJNNH;
ASPSESSIONIDQCSDDCBC=LGFPBDLCKHLIOLPMCAGJNFIM;
ASPSESSIONIDQCTACBAC=DCIPJDLCNDBEGEIIELCGFMDP;CustomerID;
ASPSESSIONIDSCTCAABD=LHPFFLFDEMIBIENBMGMLFIFP;
ASPSESSIONIDSAQDDDAD=HMCDMLFDFCLJCIBHMEHPNHME; s_cc=true;
__utmc=97358351; CustomerID=755B031ED8C016EC4F90D6F3127E776AE202DEF5BC6EB9F7A9BB19E49323BC3B;
ASPSESSIONIDQCSDBDAC=CFBDJEAAPJJGHMDGOCCHALHC;
ASPSESSIONIDSASCBDAD=FLGJHFAAJAJBICGIMOOJEFFF;Session%5FToken=C8F2A63C3BBA4E39BBF73B83C108D1C6
I want get the value of CustomerID cookie.
Using jquery I wrote this:
$.cookie('CustomerID',{ path:'/'});
But I am not getting the value for cookie - it is showing me blank.
How can I get the CustomerId cookie?
What you did is setting a cookie and not getting a cookie.
try var customerId = $.cookie('CustomerID');
or maybe you are trying to get the cookie from the root path but it's actually lives at a lower path..
Check out where you save the cookie!
Related
I have angular app and based on previous path name not page name i want to store the value either in cookie or local storage
Path name of first page is /content/drive.html/find-drive
Path name of second page is /content/drive.html/drive-results
When I am on the second page I want to store find-drive value in cookie
Similarly
Path name of first page is /content/drive.html/find-drive-advanced
Path name of second page is /content/drive.html/drive-results
When I am on second page i want to store the previous path location in my cookie or local storage.for this scenario i want to store"find-drive-advanced"
Note: document.referrer doesn't work, and window.parent.URl doesn't work
Use localStorage
Say that url1 = '/content/drive.html/find-drive', and url2 = '/content/drive.html/drive-results'
just save them like so:
localStorage.setItem('url1', url1);
localStorage.setItem('url2', url2);
to retrieve the value, just write
var url1 = localStorage.getItem('url1');
I'm trying to get a cookie I have set but all I'm getting is the PHPSESSID.
I set my cookie in a separate PHP page with:
setcookie("username", $sentname, time()+(60*60*24*30),NULL,NULL,NULL,false);
I can then find it through firefox settings.
When I try to access it using JavaScript on a different page I use:
<script>
$(document).ready(function(){
var cookie = document.cookie;
alert(cookie);
});
</script>
which then returns:
PHPSESSID=gvjsgfd8etlbdq43lndni3o0g4
It should return all the cookies, only "username" so far, I have set in a key paired string. I tried using the jquery plugin for cookies and it returned the same thing. I also couldn't find this problem elsewhere online.
Not sure if I should delete the question but one of the related links gave me the answer.
The cookie path must be set to '/' to be accessible from all subdomains. so I changed it too:
setcookie("username", $sentname, time()+(60*60*24*30),'/',NULL,NULL,false);
which gives me:
PHPSESSID=gvjsgfd8etlbdq43lndni3o0g4; username=asdf
There is post comment box on my site.
Posting comment is handled by javascript, js posts data to php script and php does the db related stuff and shows confirmation only if user is logged in if user isnt logged in then php gives not_loggedin response after receiving it js shows the bootstrap pop over box with link to login page.
So if user is not logged in then the javascript stores the entered comment in cookie so that after logging in user dont have retype the comment. Like this
document.cookie = id + "=" + input_text + "; ";
and after logging in the comment textarea is populated by reading the cookie which has the stored comment text..
everything up to this is working perfect but after inserting the comment in php i am trying to remove the cookie like this .,
setcookie($id, "", time()-3600);
print_r($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>');
but its still not removing cookie , when i reload the page , the comment textarea box i populated with the previously enetered comment which is read again from cookie.,
how do i solve this ?
i even tried displaying the cookie in php which is stored by js , lke this
//insert comment in db
//setcookie($id, "", time()-3600);
print_r($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>');
but it doesnt shows the cookie which is set by the js, it shows PHPSESSID cookie after posting comment.,
Array
(
[PHPSESSID] => c5rc6c8ggg24edg1v2o8hebb20
)
i am not trying to remove the PHPSESSID cookie.,
i am showing this on page using js . as
post_comment.php is another file in another directory on the same server .
what i am doing wrong ?
In simple words ,
js is setting cookie and php should remove cookie.
----------
update 1 :
i tried setting path while setting cookie in js like this
document.cookie = id + "=" + input_text + "; path=/";
and after posting cookie., now i get this.,
Array
(
[PHPSESSID] => c5rc6c8ggg24edg1v2o8hebb20
[4778] => this is comment
)
my php code is like this .,
//insert comment is db
setcookie($id, "", time()-3600);
printr($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>');
but cookie is still there.
update 2 :
this is very strange.,
if i try to set the cookie of the same name in php , one more cookie gets created.
my php code.
setcookie($id, "sdf", time()+36000);
printr($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>');
now when i check the broswers cookie manager , i see 2 cookies with the same name .,
but both having different content , the one which was set usng javascript have the comment enetered by user and another cookie which we set using php above , is having content "sdf".
i dont know how is this possible to be having2 cookies with the exact same names. ,
any clues ?
You need to make sure the all the
parameters (except name and time depending on the cookie.) are same while Setting Cookie in Javascript and while Removing Cookie in PHP
Parameters i.e. name,path (value and expire time can be different.)
for eg.
While setting cookie in javascript if you use it like this
document.cookie = id + "=" + input_text + " ; path=/";
you set the path to "/"
then while removing cookie in php you should specifically set like this.
//remove cookie.
setcookie($id, "", time()-36000 , "/");
I have set a session in PHP, which is creating a cookie: PHPSESSID...
I can detect this in Chrome & Opera by using document.cookie. However in Forefox, document.cookie also returns cookies set on the page by other domains, e.g. Google Analytics.
In PHP I am setting the sessions like:
session_start();
$_SESSION['source'] = &$ref['source'];
$_SESSION['term'] = &$ref['term'];
session_write_close();
I need to be able to detect if this session is set in Javascript, by locating the cookie. What is the best way to go about this?
At the moment I am just using:
document.cookie.indexOf( 'PHPSESSID' )
which seems like a bit of a botch.
The document.cookie property will return all the cookies. While your indexOf will work, it will break if your cookies actual data contains 'PHPSESSID'. It will also match the following cookie 'MYPHPSESSIDIDIT', as it contains your cookie name.
You could parse the cookies with the following function (not tested):
function getCookieValue(name)
{
// find cookie entry in middle?
var s=document.cookie,
c=s.indexOf("; "+name+"=");
if(c==-1)
{
// no, is it at the start?
c=s.indexOf(name+"=");
if(c!=0) return null;
}
// get length of value
var l=c+name.length+1,
e=s.indexOf(";",l);
// is it at the end?
if(e==-1) e-s.length;
// cut out the value
return s.substring(l,e);
}
Hope this helps
Use this Jquery plugin, it's so cool.
https://github.com/carhartl/jquery-cookie
You can use it like this way:
if($.cookie('PHPSESSID') != undefined){
//PHPSESSID exists
}
I have called one PHP page inside an iframe
I have stored some values in cookies. I want to read those cookies values from that page to other page.
I used jQuery to read the cookie.
var value = $.cookie('artname');
'artname' is the cookie name.
But it is displaying null because the cookie path is different. The path is /v/abcfile/frontend/.
But the path for the other cookies on the page i am trying to get is /.
I tried with this:
top.jQuery.cookie('artname');
But it's still showing me the same.
For path ii have tried with:
var value = $.cookie("artname", { path:'/v/vspfiles/frontend/' });
Its still showing me the null value.
How can I get value of cookie?
When you save your cookies, set path to "/". Cookie will be available on all pages
$.cookie('artname', 'value', { path:'/'});
This worked for me:
$.cookie.defaults = { path: '/' };
$.cookie("foo", "value" );