Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a cookie with url, and reading it back

Status
Not open for further replies.

lagc

Programmer
Jan 21, 2009
79
GB
I have managed to gather some help and got some script working to create a cookie, and then read it back on another page and display the results.

Basically I create a cookie of the last hotel they looked at, then on their return the name of that hotel is displayed on the index page.

What i would also like to do, is grab the url, store that in the cookie to, then when displaying the name, the url is wrapped around it, so on clicking it takes the person back to that hotel.

All the code is below, the first part works fine, but i dont seem to be able to grab the url, and read it back without errors. As you will see, I have tried to use PHP, but its doesnt work out.

Code:
<body onload="createCookie('hotelcookie', '<?=$rows['Nom_Hot']?>', 365, '<? $url = $_SERVER['SERVER_NAME']; $page = $_SERVER['php_SELF']; echo "[URL unfurl="true"]http://".$url.$page;[/URL] ?>')">
<script type = "text/javascript">
function createCookie(name,value,days,url) { 
if (days) { 
var date = new Date(); 
date.setTime(date.getTime()+(days*24*60*60*1000)); 
var expires = "; expires="+date.toGMTString(); 
} 
else var expires = ""; 
document.cookie = name+"="+value+expires+url+"; path=/";
} 
</script>

And here is when I read it back

Code:
<body onload="readCookie('hotelcookie')">
<div id="messageBlock"></div>

<script type = "text/javascript">

function readCookie(name,url) { 
var nameEQ = name + "=";
var ca = document.cookie.split(';'); 
for(var i=0;i < ca.length;i++) { 
var c = ca[i]; 
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) {
hotel = c.substring(nameEQ.length,c.length);
//alert (hotel);
document.getElementById("messageBlock").innerHTML = "On your last visit you viewed "+"<a href=\"" + url + "\">" + hotel +"</a>";
return hotel;
}
} 
return null; // no display if no previous visit
} 

function eraseCookie(name) { 
    createCookie(name,"",-1); 
} 
</script>

Can anybody help me grab that url, store it then read it back through the id messageBlock in that div.

Cheers
 
Thanks for that, very useful.

From what I can see, I think there already a var set for the url called path. But according to the cookie, that var isnt picking up the url either in the first place.

I guess I will try now to get that var working, and see if I can pick up the url.

Cheers
 
No I just cant get it to bloody work.

I have trimmed it back to how it was, and all I need is the capture the url, store it in the cookie, then be able to read it back as a text link on another page. But I cant manage it.
Code:
<body onload="createCookie('hotelcookie', '<?=$rows['Nom_Hot']?>', 365)">
<script type = "text/javascript">
function createCookie(name,value,days) { 
if (days) { 
var date = new Date(); 
date.setTime(date.getTime()+(days*24*60*60*1000)); 
var expires = "; expires="+date.toGMTString(); 
} 
else var expires = ""; 
document.cookie = name+"="+value+expires+"; path=/";
}

and here is the read cookie page

Code:
<script type = "text/javascript">
function readCookie(name) { 
var nameEQ = name + "=";
var ca = document.cookie.split(';'); 
for(var i=0;i < ca.length;i++) { 
var c = ca[i]; 
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) {
hotel = c.substring(nameEQ.length,c.length);
//alert (hotel);
document.getElementById("messageBlock").innerHTML = "On your last visit you viewed "+"<a href=\"" + url here + "\">" + hotel +"</a>";
return hotel;
}
} 
return null; // no display if no previous visit
} 

function eraseCookie(name) { 
    createCookie(name,"",-1); 
} 
</script>

S.O.S
 
Take it a step at a time. First, put some alerts in at the top of the createCookie function to see what is actually being passed in:

Code:
function createCookie(name, value, days) {
   alert('Name is: [' + name + ']');
   alert('Value is: [' + value + ']');
   alert('Days is: [' + days + ']');

...

If the data you're passing in is not right, then that's the first things to correct. After all - GIGO!

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi Dan,

Work fine that code as it was, so I went on from there to capture the url and see if I mangaed to do that, and it worked fine too.

I used php to capture the url and checked with a alert and all good.

Code:
<body onload="createCookie('hotelcookie', '<?=$rows['Nom_Hot']?>', 365, '<?php print $_SERVER[REQUEST_URI]; ?>')">

<script type = "text/javascript">
function createCookie(name,value,days,url) {
   /*alert('Name is: [' + name + ']');
   alert('Value is: [' + value + ']');
   alert('Days is: [' + days + ']');
   alert('URL is: [' + url + ']'); */   
if (days) { 
var date = new Date(); 
date.setTime(date.getTime()+(days*24*60*60*1000)); 
var expires = "; expires="+date.toGMTString(); 
} 
else var expires = ""; 
document.cookie = name+"="+value+expires+url+"; path=/";
}

I tried looking for the cookie on my computer to see if it contained all the info, and it didnt contain the url.

So basically its failing at the second stage, whch is to transfer the captured url to the cookie.
 
The cookie doesnt seem to make sense in honesty, I will post the results below:

Code:
hotelcookieM/S Sunray[URL unfurl="true"]www.mysite.com/16005684189443005606128401754029982635*[/URL]

Th ename of the cookie is correct, the name of the hotel I grabbed at that time is correct, but the first part of the url is correct, but after the slash all those numbers are def not right, or is it right and it will be de-coded the other side.
 
I'd be escaping your value:

Code:
escape(value)

Also, isn't 'url' redundant, or should be merged with the cookie value? Or do you want to be using 'url' instead of path?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Well I couldnt get path to work, so I chose instead to go at it this way.
In honesty, Im a bit stuck and am trying anything.
 
So basically take url away and use path is it.

Code:
<body onload="createCookie('hotelcookie', '<?=$rows['Nom_Hot']?>', 365, '<?php print $_SERVER[REQUEST_URI]; ?>')">

<script type = "text/javascript">
function createCookie(name,value,days,path) {
   /*alert('Name is: [' + name + ']');
   alert('Value is: [' + value + ']');
   alert('Days is: [' + days + ']');
   alert('URL is: [' + path + ']'); */   
if (days) { 
var date = new Date(); 
date.setTime(date.getTime()+(days*24*60*60*1000)); 
var expires = "; expires="+date.toGMTString(); 
} 
else var expires = ""; 
document.cookie = name+"="+value+expires+"; path=/";
}
 
Absolutely. I'm not sure if it makes a difference or not, but also I never put spaces after my semicolons when setting a cookie, so try removing those (before path, and before expires) if you have no luck.

Also, you're still not escaping 'value'.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thanks for keeping in touch Dan,

I will crack on and see if I can work with it today.

When you say escape(value) can you explain this to me with the code please.

Cheers
 
Ok Ive made some decent progress, in transfering the captured url over and having it in an array. But Im now unsure how to get it out like another of the arrays contents to use as a url wrapping around the hotel name

Code:
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) { 

var c = ca[i];

while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) {
hotel = c.substring(nameEQ.length,c.length);

document.getElementById("messageBlock").innerHTML = "On your last visit you viewed "+"<a href=\"" + url + "\">" + hotel +"</a>";
return hotel;
}

The value I need out is stored in var c with the hotel name.

The while part of the code is able to draw out the hotel name to go in the messageBlock div, but I want a different value out and give it a var name of url so I can use that as the location url.

Hopefully somebody can help me out with this bit.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top