Bounty Posted August 7, 2010 Share Posted August 7, 2010 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 Quote Link to comment Share on other sites More sharing options...
radar Posted August 7, 2010 Share Posted August 7, 2010 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') Quote Link to comment Share on other sites More sharing options...
Bounty Posted August 7, 2010 Author Share Posted August 7, 2010 I dont get it...how to use that with php? Quote Link to comment Share on other sites More sharing options...
radar Posted August 7, 2010 Share Posted August 7, 2010 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. Quote Link to comment Share on other sites More sharing options...
Bounty Posted August 7, 2010 Author Share Posted August 7, 2010 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.. Quote Link to comment Share on other sites More sharing options...
radar Posted August 7, 2010 Share Posted August 7, 2010 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 */ Quote Link to comment Share on other sites More sharing options...
Bounty Posted August 7, 2010 Author Share Posted August 7, 2010 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"... Quote Link to comment Share on other sites More sharing options...
radar Posted August 7, 2010 Share Posted August 7, 2010 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 } Quote Link to comment Share on other sites More sharing options...
Bounty Posted August 8, 2010 Author Share Posted August 8, 2010 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? Quote Link to comment Share on other sites More sharing options...
Bounty Posted August 9, 2010 Author Share Posted August 9, 2010 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 Quote Link to comment Share on other sites More sharing options...
Bounty Posted August 9, 2010 Author Share Posted August 9, 2010 Nevermind about last post...i got it 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? Quote Link to comment Share on other sites More sharing options...
radar Posted August 9, 2010 Share Posted August 9, 2010 yeah where the comment is, thats where the page code belongs. Quote Link to comment Share on other sites More sharing options...
Bounty Posted August 10, 2010 Author Share Posted August 10, 2010 Finally solved Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.