Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
Necuima

Create A Cookie Failing In Included Script

Recommended Posts

Hi Larry,

 

I'm having an issue that I don't understand to do with setting cookies in JS.  I have re-read the section on cookies in the book (pp 358...).

 

If I have the JS code within the module that uses it (a PHP module it so happens) it works just fine.  But if I include that same code via <script type="text/javascript" src = its source></script> it does not set the cookie.

 

I have checked and the script does get loaded and executes when called - it just does not set the cookie. No errors are indicated in either the FF or IE.

 

I have Googled but can't find anything exactly related to this.

 

I have tried with Firefox V56 and IE11 and both exhibit the same behavior. (Running with XAMPP/Windows 7).

 

Do you have any insights into this behavior?

 

Thanks as always, Necuima.

Edited by Necuima

Share this post


Link to post
Share on other sites

Hmmm...I don't have any insights. It sounds like you've done your due diligence in debugging this. Do you want to share the code or is there a place online I can see it for myself?

Share this post


Link to post
Share on other sites

Hi Larry,

 

This is basically from quirksmode.org/js/cookies.html;

function (update_and_go(url)
{
var n = url.indexOf("&kk");
var g_pk = url.slice(n + 4);
var date = new Date();
date.setTime(date.getTime() + (1*24*60*60*1000); /* one day in milliseconds */
var expires = ; expires = "+date.toGMTString();
var name = "vbnefhwgfg";  /* just to obfuscate the name of the cookie */
document.cookie = name + "=" + g_pk + expires + "; path=/; domain=<?php echo $_SERVER['HTTP_HOST']; ?>";
alert(document.cookie);
etc....
}

If I include the code the alert shows blank but if I embed the code in the PHP module, the alert shows the cookie correctly.

 

Again, any insights will be most appreciated. 

 

I had the same issue as a year or so ago in that I could not copy and paste the code - I had to key it in line by line and I hope that I have not made any mistakes!

 

Thanks, Necuima

Edited by Necuima

Share this post


Link to post
Share on other sites

Hi Larry,

 

The typo is just in the post above - as mentioned, I had to key in the code above line by line and that's where I made the mistake that I was referring to.

 

In the executing code there is no superfluous opening bracket.

 

Again, I look forward to your comments.

 

Thanks, Necuima

P.S., I have ordered the 5th edition of PHP and MySQL :-)

Share this post


Link to post
Share on other sites

Ah! There are a number of other typos:

 

date.setTime(date.getTime() + (1*24*60*60*1000); /* one day in milliseconds */
Missing a closing parens before the semicolon.
var expires = ; expires = "+date.toGMTString();

A number of problems there including no assigned value in the first statement, the unnecessary quotation mark in the second, and the plus sign.

 

Thanks for ordering the book. Hope you like it!

Share this post


Link to post
Share on other sites

Hi Larry, thanks for getting back to me.

 

The first missing closing parenthesis is another of my typos when I was keying in the code above line by line. I don't know why I can't copy and paste into this forum. Maybe it is something to do with my Windows 7 environment.

 

Your second comment puzzled me too.  It is exactly as per the example in in the quirksmode reference. I 'fixed' it and now the code works OK both embedded and included.

 

It was a puzzlement to quote the King of Siam :-)

 

Hope that you get a good Easter break - here in Oz it is a 4 day long weekend.

 

Cheers, Necuima.

Edited by Necuima

Share this post


Link to post
Share on other sites

Hi again Larry,

 

FYI the 'fix' did not in the end work but no matter - I used a JS script from Github that worked.  (I virus-scanned the code before using it).

 

In case anyone else falls into the traps that I fell into, here's what I (re) learned (at my age re-learning is a frequent requirement:-)

 

The Github JS code enabled me to set a cookie by including it in my initial PHP script.  To enable that cookie to be accessible to PHP, another PHP module had to be called as the cookie data are transferred to PHP via the header (part of my re-learning).  I called the second PHP module via JS window.location.replace and that worked reliably.

 

Thanks again for your interest in this.

 

Cheers from Oz, Necuima.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...