1

I have a website which uses javascript cookies in redirecting if user is not logged in. But when you use iPad or iPhone to access my site you will not be able to go to landing page since IOS browser blocks/disable cookies by default.

I've searched for workarounds and found these set Cookie manually using HTTPCookiestorage

Where are an UIWebView's cookies stored?

But first, I don't know how to use it. I've read but still it's not clear to me on how i set the cookie to IOS browser so user can view my site.

Can anyone help/guide me on how to use it? Im using javascript on my code. and its only a website not an application.

Thanks a lot!

Community
  • 1
  • 1
  • why not use localStorage , sessionStorage or webSQL? – cocco Feb 21 '14 at 16:35
  • @cocco thanks for answering. i tried using localStorage. but I have 3 folders on my site which has 3 different logins and landing page. Can i use also 3 different localStorage for each? – user2999165 Feb 21 '14 at 16:41
  • @cocco here's my code which uses localStorage. I dont know much about webSQL and sessionStorage. Can you please check my code it should redirect if condition is met. which is not happening. Thanks again – user2999165 Feb 21 '14 at 16:44
  • i can't see the code... but you can create 3 different localStorage variables... sometimes i use localStorage in combination with JSON.parse & JSON.stringify .. so my localstorage value can contain objects and arrays.anyway i can't see your code. – cocco Feb 21 '14 at 16:47
  • index.html `` – user2999165 Feb 21 '14 at 16:48
  • I don't know how to use JSON. i pasted my code above. can you help me fix it?For i honestly don't know how to do it. Many thanks. – user2999165 Feb 21 '14 at 16:50

1 Answers1

0

Use localstorage for each dir...

This is what i mean:

var dir1=localStorage['dir1']? // if localstorage for dir1 exists
         JSON.parse(localStorage['dir1']): // use localstorage value else
         {count:1,pageLoadCount:1}; // set count to 1 and pagecount to 1

if(dir1.pageLoadCount==0){// will never happen :)
 //localStorage['dir1']=whatever set the storage to something before pagechange
 location.href="pathhere/loginios.html";
}else{
 dir1.pageLoadCount++;
 dir1.count=dir1.pageLoadCount;
 localStorage['dir1']=JSON.stringify(dir1);// set localStorage
}

then similar/same script for dir2

EDIT

var defaultInfo=[
 {folder:'folder1',count:1,pageLoadCount:1},
 {folder:'folder2',count:1,pageLoadCount:1},
 {folder:'folder3',count:1,pageLoadCount:1},
],
data=localStorage['data']?JSON.parse(localStorage['data']):defaultInfo;//load

console.log(data);
//do stuff


localStorage['data']=JSON.stringify(data);//store

DEMO

http://jsfiddle.net/qcuhD/1/

EDIT

login

localStorage['data']=JSON.stringify({logedIn:1})

loginCheck

if(localStorage['data']){
 var data=JSON.parse(localStorage['data']);
 if(data.logedIn==1){
  //OK
 }else{
  // not logged in
 }
}else{
 // not logged in
}
cocco
  • 16,442
  • 7
  • 62
  • 77
  • thank you for sharing it with me. I will try it now and get back to you. can you please hold on for me? shall i make `['dir1']` the folder? like `['www.mysite.com/folder1']` – user2999165 Feb 21 '14 at 16:59
  • you could but ... use a simple variable name like folder1 – cocco Feb 21 '14 at 16:59
  • im haveing an error in this line `{count:1,pageLoadCount:1};` – user2999165 Feb 21 '14 at 17:06
  • var dir1=localStorage['dir1']?JSON.parse(localStorage['dir1'])||{count:1,pageLoadCount:1}; should be one line. – cocco Feb 21 '14 at 17:08
  • `var folder1 = "http://example.com/test/"; var dir1=localStorage['folder1']?JSON.parse(localStorage['folder1'])||{count:1,pageLoadCou‌​nt:1}; if(dir1.pageLoadCount==0){ //localStorage['dir1']={whatever}???? location.href="http://example.com/test/login.html"; } dir1.pageLoadCount++; dir1.count=dir1.pageLoadCount; localStorage['dir1']=JSON.stringify(dir1);` – user2999165 Feb 21 '14 at 17:10
  • here is how exactly i did it. i still get an error on dreamweaver on that line – user2999165 Feb 21 '14 at 17:11
  • var dir1=localStorage['folder1']?JSON.parse(localStorage['folder1']):{count:1,pageLoadCou‌nt:1}; my fault. it's : not || – cocco Feb 21 '14 at 17:15
  • Hi cocco. i'm sorry im still having error. i tried it like this `var dir1=localStorage['folder1']?JSON.parse((localStorage['folder1'])||{count:1,pageLoadCou‌​nt:1});` it has no error. i tried to load it. and when i access it on iPad, i'm coming to the landing page directly. im not being redirected to login – user2999165 Feb 21 '14 at 17:24
  • json.parse and stringify are here to convert strings to objects and arrays and back. you need to store data inside objects or arrays ... so i'm showing you how you can achieve this. create a default object... then when the page loads do whatever changes or calculation on that object and then store it back as localstorage.but you need to write the code... i don't know what you wanna do.btw you need to understand how arrays and objects work and how to access them. – cocco Feb 21 '14 at 17:28
  • play with that example i just posted , change it and see whats happening ... use chrome so you have easy access to the localstorage values. – cocco Feb 21 '14 at 17:30
  • dir1 is our object here right? what i want to do is when i access my www.example.com/folder1/index.html it will redirect me to log in page if the user is not logged. I'm sorry if i giving you a hard time to guide me. please have patience with me. – user2999165 Feb 21 '14 at 17:36
  • for security reasons you should not do that...anyway i write you an example. – cocco Feb 21 '14 at 17:37
  • i'm using chrome and safari on testing. since my main purpose is to be able to access the site in IOS browser without using cookies. can you please help me more in the codes? it's my first time t do this. thanks a lot – user2999165 Feb 21 '14 at 17:38
  • sry ... but yeah.. i can't learn you the whole javascript language. read more about it and use google.i posted you now 3 approaches and you don't understand none of them... it's not so easy if you just started.And i also can't write you the whole code.this site is made for simple questions and answers. hire a developer if you want someone who writes you the whole code. – cocco Feb 21 '14 at 17:48
  • i will try now your third example. in the second example i make it like this. `var folder1 = "http://example.com/test/"; var defaultInfo=[ {folder:'folder1',count:1,pageLoadCount:1}, {folder:'folder2',count:1,pageLoadCount:1}, {folder:'folder3',count:1,pageLoadCount:1}, ], data=localStorage['data']?JSON.parse(localStorage['data']):defaultInfo;//load console.log(data); if(data.pageLoadCount==0){ location.href="http://example.com/test/login.html"; } data.pageLoadCount++; data.count=dir1.pageLoadCount; localStorage['data']=JSON.stringify(data);` but still not going to the login page. – user2999165 Feb 21 '14 at 17:52
  • i'm sorry i appreciate your help. i cant hire a developer. this is for our school project. please dont give up on me i really need help – user2999165 Feb 21 '14 at 17:52
  • the loginCheck, shall i have another page for that? or can i just place it on my index? thanks – user2999165 Feb 21 '14 at 17:55
  • it worked now :) this is what i did with your example. in login.html i put the code you give on the last part of the code – user2999165 Feb 21 '14 at 18:01
  • in index.html i edited your answer. please see. that's what worked. from your codes. thanks a lot. 1 last question, if im going to use the same codes for my 2 other folders with login, what else will i need to edit aside from the path or links. thanks a lot – user2999165 Feb 21 '14 at 18:06