Jump to content

Making cookies with JS


Bounty

Recommended Posts

This is my original topic. Well after wildteen88 posted that i should use JS to make cookies by clicking a link. I used google to find a tutorial on how to do that but my tries wasn't successful...

So could you help me build a image button+setting cookie+switching to page2....?

 

Thanks :)

Link to comment
Share on other sites

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=/";
}

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) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}

 

createCookie('ppkcookie','testcookie',7)

 

var x = readCookie('ppkcookie1')

if (x) {

[do something with x]

}

 

eraseCookie('ppkcookie')

Link to comment
Share on other sites

that is javascript...  to create a cookie...  and then you can use php to read that cookie...  for an image to create a cookie, that's how you'll have to do it..  or rethink it, if someone is clicking on an image, its either doing something already or going somewhere..  perhaps once they get to where they're going is when PHP should set the cookie.

Link to comment
Share on other sites

The thing is that i got 2 pages.. one written in English and one on Serbian...so i want if person press a image-link that leads to Serbian page it should create cookie so every time after a person loads my site it gets redirected to serbian page...and if clicked back to English image-link the cookie gets deleted and it should run as every else site...

I hope you understood what i want to do ://

 

Thanks for helping..

Link to comment
Share on other sites

Yeah what I would do, is on your index.php or whatever...  is write the cookie using PHP $_COOKIE will do that for you...  but basically you would do something like

<a href="index.php?lang=english"><img src='' border='0' /></a>
<a href="index.php?lang=serbian"><img src='' border='0' /></a>

then in php do:

$_lang = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : '';
setcookie("TestCookie", $_lang, time()+60*60*24*365);  /* expire in year */

Link to comment
Share on other sites

I only need 2 pages original eng and sidepage srp. So i wanted to add a "checker" to eng page (if cookie is "srp" redirect if not continue) and two buttons that will change the value of cookie (1 and 0)...

 

I don't understand this linking "?lang=english"...

Link to comment
Share on other sites

that is so that you can pass the language to the php script to verify..

 

but then once you set the cookie you would do a redirect like this

// in english page

if($_COOKIE['TestCookie'] == 'Serbian') {

header("location:serbian/index.php");

} else {

// english page here

}

 

in serbian page:

 

if($_COOKIE['TestCookie'] == 'English') {

header('location: ../index.php');

} else {

// serbian page here

}

 

simple as that..

 

then of course, as  you pass the variable off the images ?lang=english or ?lang=serbian

 

you would need to do this in both pages as well

 

$_lang = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : '';

setcookie("TestCookie", $_lang, time()+60*60*24*365);  /* expire in year */

if($_lang == 'English') {

// redirect to english page

}

 

or in English page

 

$_lang = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : '';

setcookie("TestCookie", $_lang, time()+60*60*24*365);  /* expire in year */

if($_lang == 'Serbian') {

// redirect to serbian page

}

Link to comment
Share on other sites

I get it...just 1 thing..

// in english page

if($_COOKIE['TestCookie'] == 'Serbian') {

header("location:serbian/index.php");

} else {

// english page here

}

Where is "//english page here" couldn't there be something like "do nothing and keep loading the page"?

Or you think to insert whole <html></html> code inside of it?

Link to comment
Share on other sites

Can't something like this be done?

<?php
if($_COOKIE['TestCookie'] == 'srb') {
header("location:index2.php");
} else {
$Month = 2592000 + time(); 
$value = 'eng';
setcookie("lang", $value, $Month);
}
//and keep with page
?>

 

And can you write me a js code for image-link-cookie set?

Please?

Thanks :)

Link to comment
Share on other sites

Nevermind about last post...i got it :P

Just tell me how to do this:

 

// in english page

if($_COOKIE['TestCookie'] == 'Serbian') {

header("location:serbian/index.php");

} else {

// english page here

}

Should i insert whole page html instead of comment?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.