Bounty Posted August 7, 2010 Share Posted August 7, 2010 Ok here are my 3 php pages: set.php <?php $Month = 2592000 + time(); $value = '1'; setcookie("cs_hpage", $value, $Month); ?> get.php <?php if (isset($_COOKIE["cs_hpage"])) { $cookie = $_COOKIE["cs_hpage"]; print("Received a cookie: ".$cookie."\n"); } else { print("Fail!.\n"); } ?> clear.php <?php setcookie ("cs_hpage", "", time()-60000); ?> It all works like charm but i dont know how to set if $cookie == 1 echo "Cookie here!" and if other then 1 echo "Fail!" All i managed to do is as you can see to echo the cookie :/ And question no.2: How can i set a cookie through a button?? (Like when pressed set cookie to value "1") Thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/210023-making-cookies/ Share on other sites More sharing options...
Bounty Posted August 7, 2010 Author Share Posted August 7, 2010 Someone? Please? Quote Link to comment https://forums.phpfreaks.com/topic/210023-making-cookies/#findComment-1096230 Share on other sites More sharing options...
wildteen88 Posted August 7, 2010 Share Posted August 7, 2010 It all works like charm but i dont know how to set if $cookie == 1 echo "Cookie here!" and if other then 1 echo "Fail!" All i managed to do is as you can see to echo the cookie :/ You'd use another if statement, get.php <?php if (isset($_COOKIE["cs_hpage"])) { $cookie = $_COOKIE["cs_hpage"]; if($cookie == '1') { print("Received a cookie: ".$cookie."\n"); } else { print("Fail"); } } else { print("Fail!.\n"); } ?> Or better yet merge the two together <?php if (isset($_COOKIE["cs_hpage"]) && $_COOKIE["cs_hpage"] == '1') { $cookie = $_COOKIE["cs_hpage"]; print("Received a cookie: ".$cookie."\n"); } else { print("Fail!.\n"); } ?> And question no.2: How can i set a cookie through a button?? (Like when pressed set cookie to value "1") Like so <?php if(isset($_GET['submit'])) { setcookie('button_cookie', '2', time()+3600); } ?> <form action="" method="post"> <input type="submit" name="submit" value="Create Cookie" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/210023-making-cookies/#findComment-1096233 Share on other sites More sharing options...
Bounty Posted August 7, 2010 Author Share Posted August 7, 2010 What about image or link instead of button? :/ Quote Link to comment https://forums.phpfreaks.com/topic/210023-making-cookies/#findComment-1096234 Share on other sites More sharing options...
wildteen88 Posted August 7, 2010 Share Posted August 7, 2010 <?php if(isset($_GET['setcookie']) && $_GET['setcookie'] == '1') { setcookie('cookie_name', 'cookie_value', time()+3600); } ?> <h1>Link</h1> <a href="?setcookie=1">Set Cookie</a> <h1>Image</h1> <a href="?setcookie=1"><img src="image.jpg" title="Set Cookie" /></a> Quote Link to comment https://forums.phpfreaks.com/topic/210023-making-cookies/#findComment-1096238 Share on other sites More sharing options...
Bounty Posted August 7, 2010 Author Share Posted August 7, 2010 What i mean by link is something like this: <h1>Link</h1> <a href=/nextpage.html action="?setcookie=1">Set Cookie</a> Quote Link to comment https://forums.phpfreaks.com/topic/210023-making-cookies/#findComment-1096240 Share on other sites More sharing options...
wildteen88 Posted August 7, 2010 Share Posted August 7, 2010 Anchor tags (<a></a>) don't have an action attribute. If you're wanting to set the cookie when the user clicks the link/image then you'll need to use Javascript for this. Here is a tutorial on using cookies with Javascipt. PHP will be able to read cookies set by javascript (and vise versa) Quote Link to comment https://forums.phpfreaks.com/topic/210023-making-cookies/#findComment-1096242 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.