Jump to content

Making cookies


Bounty

Recommended Posts

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

<?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>

 

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.