Set Cookie Path - javascript

I have a javascript cookie which works although I have pages in different directories. The cookie set on the default page has a path "/" while the cookie set in another directory has a path "/example"
I know many people have asked this many times but I can't do it by myself. I made a cookie code that actually works from the past three months. I don't want to mess it up. Can anyone help me add a set path "/" to my cookie code? Thanks in advance.
My code is:
<script language="JavaScript">
function SetCookie(cookieName,cookieValue) {
var today = new Date();
var expire = new Date();
var nDays=365
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
</script>

Indicate the path by adding the following to the end of the cookie string in function "SetCookie":
"; path=/";
http://www.quirksmode.org/js/cookies.html#doccookie

Related

how to create a cookie in nodejs socket.io

im trying to create a cookie while using socket.io. I am able to read cookies but cannot find any information on how to create one.
socket.on('test', async function(){
// view set cookies (works!)
console.log('cookies', socket.handshake.headers.cookie);
// create cookie here...
});
thank you
you could set a cookie like you could do it in the mdn documentation with setcookies() and read it with the cookies api
You can do it the same way as document.cookie, but instead of:
document.cookie = ";mycookie=myval";
you do:
socket.handshake.headers.cookie = ";mycookie=myval; expires=cookieDate; path=cookiePathOnWebsite"
for example:
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = date.toGMTString();
socket.on('test',async function(){
socket.handshake.headers.cookie = ";foo=bar; expires="+date+";path=/"
});
would set a cookie at the main page of your website, with foo defined as bar!

Adding a cookie in Shopify

I am trying to add a value in cookie in my store. Javascript is not an option because the content of the cookie needs to be invisible on the client-side page source.
In a PHP server it's done using a code like this:
<?php
$cookie_name = "PHPsecret";
$cookie_value = "The secret";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
Can someone please help me acheive this in Shopify? Even if it can be done with some work-around trick such as manipulating the cart attribiute etc. it's okay. Let me know how.
Much appreciated.
I needed to pass a Shopify variable, {{ cart.item_count }} to a subdomain using a cookie. For me, adding this in Javascript at the bottom of theme.liquid did the trick:
<script>
var nowCart = new Date();
var timeCart = nowCart.getTime();
var expireTimeCart = timeCart + 1000*36000;
nowCart.setTime(expireTimeCart); // " + nowCart.toUTCString() + "
document.cookie = "_count={{ cart.item_count }};domain=.example.com;expires=;SameSite=none;Secure=true";
</script>
</body>
</html>
In Shopify Liquid, there is to the best of my knowledge no way to "hide" a cookie. In fact, anyone who knows how to open their dev tools would be able to read your cookie either way...does it really need to stay a secret (consider adding it to a liquid variable instead)?
The next best option I can recommend is to put your Javascript into a separate .js file in the assets folder of your theme.liquid and include it in your theme like so, {{ "cookie-code.js" | asset_url | script_tag }}.
Once again, if the user can open dev tools, they could still read your cookie or look at your javascript file. Maybe clarify in more detail what exactly it is that you're trying to achieve?

Cookies not being created JS

When running my web page the cookies are not being created I have been using IE as I know that chrome does not load cookies on local web pages.
I am loading the JS as
<script language="javascript" src="script.js" type="text/javascript"></script>
the script to set the cookies which is in the script.js file is
function SetCookie(name,val) {
document.cookie = name + "=" + val + ';';
}
and in my file for the main page I have set the cookie as shown
SetCookie('July',July);
When I run the code the cookie is undefined.
The simplest way to create a cookie is to assign a string value to the document.cookie object
document.cookie = "key1=value1;key2=value2;expires=date";
So, either July has to be a variable or you'll have to pass it as a string.
var July = 'July';
SetCookie('July', July);
OR
SetCookie('July', 'July');

Cookie set with javascript, not recognised with PHP (same domain and path)

These are my cookies:
My cookies
I'm running my website from my own computer using a WAMP server. I access my main page from http://127.0.0.1/Zulaijen/, and this is the javascript funcion to set the cookies (User and Session):
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "; expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + expires;
}
When I read them using javascript, it works fine. I get my session. Then I go to another PHP page named 'uploader.php' with this code:
if(!isset($_COOKIE['TestCookie']))
setcookie("TestCookie", "Hello World!", time()+3600);
print_r($_COOKIE);
echo("Session: " . $_COOKIE['Session'] . "<br/>User: " . $_COOKIE['User'] . "<br/>");
And the result is:
Array ( [TestCookie] => Hello World! )
Notice: Undefined index: Session in D:\wamp\www\Zulaijen\uploader.php
on line 30
Notice: Undefined index: User in D:\wamp\www\Zulaijen\uploader.php on line 30
Which means my PHP code is not reading the cookies I set with javascript. It only reads the one I set with my PHP code (TestCookie). And I don't understand why. They are within the same domain and the same path.
You should try setting cookie path. Could be that the cookie paths for PHP and JavaScript isn't matching, hence the cookie will not be shared between the two.
JavaScript cookie path:
How do I set path while saving a cookie value in JavaScript?
PHP coookie path (see path section):
http://php.net/manual/en/function.setcookie.php
I finally found out what was causing the problem (by accident). It's very confusing, but it has a very simple solution.
In order to read the cookies from PHP, you must read them from the very beginning of the file.
Doing this:
<?php print_r($_COOKIE); ?>
At the very beginning of the file (before any HTML code) prints every cookie I set correctly as it should. Even if you set them from PHP, if you don't do it from the very beginning of the file you won't be able to get them.
The reason why I was able to read the one I was setting with PHP was simply because I was setting it right before reading it, or so it seems.

How to call a trusted function from acroform

I have been looking for an example of how to call a trusted function in acroforms and havent found any. I am trying to build a form for my company that will allow users to click a button and automatically have the form save to a folder on our server (eg: //SERVER1/Forms/). I found this code to test with and placed it in C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\Javascripts
//SaveAs Function1
var date = new Date();
var day = date.getDate();
var month = date.getMonth()+1;
var year = date.getFullYear();
var dateSigned = String(month) + String(day) + String(year);
var mySaveDoc = app.trustedFunction(function(doc,fileNam­e){
app.beginPriv();
var myPath = "C/test/" + fileName + "Agreement " + dateSigned + ".pdf";
//saveAs is the only privileged code that needs to be enclosed
doc.saveAs({cPath: myPath, bCopy: true, bPromptToOverwrite: false});
//doc.close();
app.endPriv();
});
Any help on making this work is greatly appreciated!!
I think the location of the application level script is correct; check whether you have other files in that folder; one of them would be a precompiled one, coming from Adobe.
Now, for calling the trusted function, well…, call it as you would call any other function:
mySaveDoc(this, fileName) ;
and that should do it.
However, there are a few issues I don't like that much in the application-level script:
The dateSigned variable and its bits and pieces will be defined and initialized when the application starts, and then keep their value. In other words, if you keep Reader running all the time, the date will not be updated. To get the current date all the time, you'd have to initialize the dateSigned variable within the function. AND, as you are in Acrobat JavaScript, you can use the util object for assembling the string.
Your script would then look like this:
var mySaveDoc = app.trustedFunction(function(doc, fileName){
app.beginPriv() ;
var dateSigned = util.printd("MMDDYYYY", new Date() ;
var myPath = "/C/test" + filename + "Agreement " + dateSigned + ".pdf" ;
doc.saveAs({cPath: myPath, bCopy: true, bPromptToOverwrite: false}) ;
app.endPriv() ;
}) ;
Note that there is als a slash at the beginning of the path (although I may be wrong on that; as I don't have access to a Windows machine, I can not verify it; if someone else would use Acrobat, open any file, and then run this.path() from the Console, then he could confirm the slash (or not)).

Categories

Resources