JayVee Posted November 25, 2008 Share Posted November 25, 2008 How would I go about rewriting this php in javascript? Its a simple cookie check with a redirect based on cookie value. else if (!isset($_COOKIE['legal'])) { header("Location: /index.php"); } else if($_COOKIE['legal'] == "no"){ header("Location: /index.php"); } Link to comment https://forums.phpfreaks.com/topic/134202-redirect-using-cookie/ Share on other sites More sharing options...
shutat Posted November 27, 2008 Share Posted November 27, 2008 if(document.cookie.length > 0 && document.cookie.substr(0, document.cookie.indexOf("=")) == cname) { if(document.cookie.substr(document.cookie.indexOf("=") + 1) == "no") { location.href = "index.php"; } else { // cookie data was something other than no } } else { location.href = "index.php"; // cookie wasn't set } I *think* that would do assuming the cookie format is just cname=no. Things to lookout for are indexOf is case sensitive, and it returns -1 if the match string isn't found. HTH Link to comment https://forums.phpfreaks.com/topic/134202-redirect-using-cookie/#findComment-700208 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.