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

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.

Related

How to get Client System/PC Domain and PC Name using c# or jQuery or Javascript

IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
Environment.UserDomainName;
properties.DomainName;
System.Web.HttpContext.Current.Request.LogonUserIdentity.Name
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
I tried above Properties in C# but no luck.
It works fine on the local machine.
My Question:
I have hosted a web application in abc.com domain and url is abc.com, when I access this site outside of domain or from some other external domain computers (from clients system) how to get their(clients) system domain name using c#, Jquery or Javascript.
Any solution in c#, jQuery, JS?
Thanks in advance.
Based on this answer about JS solution: JavaScript - How to get the name of the current user
I can add following:
Browser is a kind of a sandbox, where you can't reach Win resources without user permissions.
If Your user logged in to the system, then its already in your domain.
try like this ,By this way you will get Hostname
string ipAddress = Request.UserHostAddress;
IPHostEntry entry = Dns.GetHostEntry(ipAddress);
string domainName = entry.HostName;
and for computer name you can do like this
var comutername= HttpContext.Current.Server.MachineName
and in your javascript you will get computer name by using ActiveXObject as
function GetClientInfo() {
var net = new ActiveXObject("wscript.network");
alert("Computer Name : " + net.ComputerName + "\n User Name : " + net.UserName + "\n Domain Name :"
+ net.UserDomain);
}

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');

JavaScript arguments being cached unintentionally

In my php file, I'm calling this jQuery plugin with
<script>$.notifications('resources/notifications.inc.php');</script>
I previously had the php file at:
resources/dashboard.notifications.php
and later moved it to what I have now, which is:
resources/notifications.inc.php
The issue I'm having is it's like caching the arguments and still showing the old notifications php file for the requestUrl. I've cleared my cookies and everything. What should I do? I have no idea where the old value is being stored.
Here's the plugin I'm calling.
$.extend({
notifications: function() {
Notifications.requestUrl = arguments[0];
console.log(Notifications.requestUrl + " and " + arguments[0]);
Notifications.interval = arguments[1] || 15000;
}
});
Finally the output I'm getting in the console. (The unexpected token in JSON is coming from the non-existent php file that it's trying to request)
See image
It appears that it was in fact being called a second time, in another file dashboard.js

Set Cookie Path

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

Categories

Resources